530 likes | 692 Views
This resource provides an overview of Java Swing and the JOptionPane class, which is essential for creating graphical user interfaces (GUIs) in Java. The tutorial covers the fundamentals of working with AWT (Abstract Windowing Toolkit) and Swing components, enabling developers to design interactive applications. Key topics include creating and configuring components such as buttons and panels, implementing event listeners, and utilizing different layout managers. Understanding JOptionPane facilitates user interaction through input dialogs, message dialogs, and confirmation dialogs in the GUI environment.
E N D
Lec09 :: Java Swing เรื่อง jOptionPane Nattapong Songneam http://www.siam2dev.com
AWT to Swing • AWT: Abstract Windowing Toolkit • import java.awt.* • Swing: new with Java2 • import javax.swing.* • Extends AWT • Tons o’ new improved components • Standard dialog boxes, tooltips, … • Look-and-feel, skins • Event listeners • API: • http://java.sun.com/j2se/1.3/docs/api/index.html
Swing Set Demo • J2sdk/demo/jfc/SwingSet2 • Many predefined GUI components
GUI Component API • Java: GUI component = class • Properties • Methods • Events JButton
Using a GUI Component • Create it • Instantiate object: b = new JButton(“press me”); • Configure it • Properties: b.text = “press me”; [avoided in java] • Methods: b.setText(“press me”); • Add it • panel.add(b); • Listen to it • Events: Listeners JButton
Anatomy of an Application GUI Internal structure GUI JFrame JFrame JPanel containers JPanel JButton JButton JLabel JLabel
Using a GUI Component 2 • Create it : สร้างออบเจ็กต์ • Configure it : กำหนดคุณสมบัติ • Add children (if container) • Add to parent (if not JFrame) • Listen to it orderimportant เรียงลำดับการทำงาน จากบนลงล่าง
Build from bottom up Listener • Create: • Frame • Panel • Components • Listeners • Add: (bottom up) • listeners into components • components into panel • panel into frame JButton JLabel JPanel JFrame
Code JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); press me
Application Code import javax.swing.*; class hello { public static void main(String[] args){ JFrame f = new JFrame(“title”); JPanel p = new JPanel(); JButton b = new JButton(“press me”); p.add(b); // add button to panel f.setContentPane(p); // add panel to frame f.show(); } } press me
Layout Managers • Automatically control placement of components in a panel • Why?
Layout Manager Heuristics FlowLayout GridLayout null none, programmer sets x,y,w,h Left to right, Top to bottom GridBagLayout BorderLayout CardLayout n c One at a time JButton w e s
Combinations JButton JButton JTextArea
Combinations JButton JButton JFrame n JPanel: BorderLayout c JPanel: FlowLayout JTextArea
Code: null layout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); JButton b = new JButton(“press me”); b.setBounds(new Rectangle(10,10, 100,50)); p.setLayout(null);// x,y layout p.add(b); f.setContentPane(p); press me
Code: FlowLayout JFrame f = new JFrame(“title”); JPanel p = new JPanel( ); FlowLayout L = new FlowLayout( ); JButton b1 = new JButton(“press me”); JButton b2 = new JButton(“then me”); p.setLayout(L); p.add(b1); p.add(b2); f.setContentPane(p); Set layout mgr before adding components press me then me
Applets JApplet • JApplet is like a JFrame • Already has a panel • Access panel with JApplet.getContentPane( ) import javax.swing.*; class hello extends JApplet { public void init(){ JButton b = new JButton(“press me”); getContentPane().add(b); } } contentPane JButton
Applet Methods • Called by browser: • init( ) - initialization • start( ) - resume processing (e.g. animations) • stop( ) - pause • destroy( ) - cleanup • paint( ) - redraw stuff (‘expose’ event)
Application + Applet Command line Browser import javax.swing.*; class helloApp { public static void main(String[] args){ // create Frame and put my mainPanel in it JFrame f = new JFrame(“title”); mainPanel p = new mainPanel(); f.setContentPane(p); f.show(); } } class helloApplet extends JApplet { public void init(){ // put my mainPanel in the Applet mainPanel p = new mainPanel(); getContentPane().add(p); } } // my main GUI is in here: class mainPanel extends JPanel { mainPanel(){ setLayout(new FlowLayout()); JButton b = new JButton(“press me”); add(b); } } JFrame JApplet or contentPane JPanel JButton
Applet Security • No read/write on client machine • Can’t execute programs on client machine • Communicate only with server • “Java applet window” Warning
โดยใช้ JOptionPane • คลาส JOptionPane เป็นคลาสที่อยู่ใน package javax.swing และใช้สำหรับการรับข้อมูลผ่านทางคีย์บอร์ด และแสดงข้อมูลทางจอภาพ ในการทำงานของ graphics mode หรือ GUI โดยจะแสดงออกมาในลักษณะของ popup window ที่เรียกว่า dialog box
ลักษณะของ dialog box ของ JOptionPane • InputDialog • MessageDialog • ConfirmDialog
Input Dialog • สร้างขึ้นจาก method ที่ชื่อ showInputDialog() ซึ่งจะ return ค่ากลับมาเป็น String • ตัวอย่างเช่น String response = JOptionPane.showInputDialog( null, “What is your name?”);
Arguments of showInputDialog() • ชื่อของ parent window • ข้อความที่ต้องการแสดงใน inputDialog • ชื่อหัวข้อ (title) (option) • ชนิดของ InputDialog (option) • ERROR_MESSAGE • INFORMATION_MESSAGE • PLAIN_MESSAGE • QUESTION_MESSAGE • WARNING_MESSAGE
Message Dialog • MessageDialog เป็น dialog box ที่ใช้สำหรับแสดงข้อความ หรือข้อมูลต่างๆ โดยถูกสร้างจาก method ชื่อว่า showMessageDialog() • ค่า argument ของ showMessageDialog() จะเหมือนกับ showInputDialog()
showMessageDialog() JOptionPane.showMessageDialog(null, "Hello..Java!!"); JOptionPane.showMessageDialog(null, "This program will terminate in 10 seconds", "Programtermination", JOptionPane.WARNING_MESSAGE);
Confirm Dialog • ConfirmDialog เป็น dialog box ที่มีปุ่มคำสั่ง Yes No และ Cancel ปรากฎด้วย โดยสร้างจาก method ชื่อ showConfirmDialog() • showConfirmDialog() จะ return ค่ากลับเป็น ตัวเลขจำนวนเต็ม (integer) โดยขึ้นกับปุ่มที่ผู้ใช้คลิก นั่นคือ ปุ่ม Yes จะมีค่าเท่ากับ 0 ปุ่ม No จะมีค่าเท่ากับ 1 และปุ่ม Cancel จะมีค่าเท่ากับ 2
Arguments of showConfirmDialog() • ชื่อของ parent window • ข้อความที่ต้องการแสดงใน inputDialog • ชื่อหัวข้อ (title (option) • ค่าคงที่ของปุ่ม Yes No และ Cancel ที่ต้องการแสดง ซึ่งมี 2 แบบ คือ (option) • YES_NO_CANCEL_OPTION • YES_NO_OPTION • ชนิดของ InputDialog ซึ่งจะถูกกำหนดโดยค่าคงที่ต่อไปนี้ (option) • ERROR_MESSAGE • INFORMATION_MESSAGE • PLAIN_MESSAGE • QUESTION_MESSAGE • WARNING_MESSAGE
showConfirmDialog() int s = JOptionPane.showConfirmDialog(null, ”Are you ready to quit? "); int s = JOptionPane.showConfirmDialog( null, "Are you ready to quit?", "Exit confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
ฝึกปฎิบัติตัวอย่าง Input1 ถึง Input14.java
Class Input1 import javax.swing.JOptionPane; public class Input1 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); System.exit(0); } }
Class Input2 import javax.swing.JOptionPane; class Input2 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: ", "Data Input",JOptionPane.ERROR_MESSAGE); System.exit(0); } }
Class Input3 import javax.swing.JOptionPane; class Input3 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: ", "Data Input",JOptionPane.INFORMATION_MESSAGE); System.exit(0); } }
Class Input4 import javax.swing.JOptionPane; class Input4 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: ", "Data Input",JOptionPane.PLAIN_MESSAGE); System.exit(0); } }
Class Input5 import javax.swing.JOptionPane; class Input5 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: ", "Data Input",JOptionPane.WARNING_MESSAGE); System.exit(0); } }
Class Input6 import javax.swing.JOptionPane; class Input6 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: ", "Data Input",JOptionPane.QUESTION_MESSAGE); System.exit(0); } }
Class Input7 import javax.swing.JOptionPane; class Input7 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); JOptionPane.showMessageDialog(null, "Hello! "+data); System.exit(0); } }
Class Input8 import javax.swing.JOptionPane; class Input8 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); JOptionPane.showMessageDialog(null,data, " Input Name : ",JOptionPane.WARNING_MESSAGE); System.exit(0); } }
Class Input9 import javax.swing.JOptionPane; class Input9 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); JOptionPane.showMessageDialog(null,"Hello " + data, " Input Name : ",JOptionPane.WARNING_MESSAGE); System.exit(0); } }
Class Input10 import javax.swing.JOptionPane; class Input10 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); JOptionPane.showMessageDialog(null,"Hello " + data); System.exit(0); } }
Class Input11 import javax.swing.JOptionPane; class Input11 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); JOptionPane.showMessageDialog(null,"Hello " + data); int s = JOptionPane.showConfirmDialog(null,"Are you ready to quit? "); System.exit(0); } }
Class Input12 import javax.swing.JOptionPane; class Input12 { public static void main(String[] args) { String data; data = JOptionPane.showInputDialog(null,"Enter Name: "); JOptionPane.showMessageDialog(null,"Hello " + data); int s = JOptionPane.showConfirmDialog(null,"Are you ready to quit? "); JOptionPane.showMessageDialog(null,"no select = " +s ); System.exit(0); } }
Class Input13 import javax.swing.JOptionPane; class Input13{ public static void main(String[] args) { float score; double d; int mid_score,final_score,total_score; char ch; String data, message; message = " "; mid_score = 30 ; data = JOptionPane.showInputDialog("Enter final score: "); final_score = Integer.parseInt(data); total_score = mid_score+final_score; message = "Mid = " + mid_score +"Final = "+final_score+ " Total score = " + total_score; JOptionPane.showMessageDialog(null, message); System.exit(0); } }
Class Input14 import javax.swing.JOptionPane; class Input14{ public static void main(String[] args) { float salary,ot,debt,net; String data, message; data = JOptionPane.showInputDialog("Enter Your Salary: "); salary = Float.parseFloat(data); data = JOptionPane.showInputDialog("Enter Your OT: "); ot = Float.parseFloat(data); data = JOptionPane.showInputDialog("Enter Your debt : "); debt = Float.parseFloat(data); net = salary+ot-debt; message = "salary = "+ salary + " ot = "+ ot + "debt = " + debt + " net = "+net ; JOptionPane.showMessageDialog(null, message); System.exit(0); } }
import javax.swing.JOptionPane; public class FindAverage { public static void main ( String args[ ] ) { float x, y, average; String data, outText; data = JOptionPane.showInputDialog("Enter first value : "); x = Float.parseFloat(data); data = JOptionPane.showInputDialog("Enter second value : "); y = Float.parseFloat(data); average = (x + y) / 2.0f; outText = "Average number between " + x + " and " + y + " is " + average; JOptionPane.showMessageDialog(null, outText); System.exit(0); } }