1 / 48

Lec09 :: Java Swing

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, …

shyla
Download Presentation

Lec09 :: Java Swing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lec09 :: Java Swing เรื่อง jOptionPane Nattapong Songneam http://www.siam2dev.com

  2. 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

  3. Swing Set Demo • J2sdk/demo/jfc/SwingSet2 • Many predefined GUI components

  4. GUI Component API • Java: GUI component = class • Properties • Methods • Events JButton

  5. 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

  6. Anatomy of an Application GUI Internal structure GUI JFrame JFrame JPanel containers JPanel JButton JButton JLabel JLabel

  7. Using a GUI Component 2 • Create it : สร้างออบเจ็กต์ • Configure it : กำหนดคุณสมบัติ • Add children (if container) • Add to parent (if not JFrame) • Listen to it orderimportant เรียงลำดับการทำงาน จากบนลงล่าง

  8. 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

  9. 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

  10. 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

  11. Layout Managers • Automatically control placement of components in a panel • Why?

  12. 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

  13. Combinations JButton JButton JTextArea

  14. Combinations JButton JButton JFrame n JPanel: BorderLayout c JPanel: FlowLayout JTextArea

  15. 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

  16. 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

  17. 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

  18. Applet Methods • Called by browser: • init( ) - initialization • start( ) - resume processing (e.g. animations) • stop( ) - pause • destroy( ) - cleanup • paint( ) - redraw stuff (‘expose’ event)

  19. 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

  20. Applet Security • No read/write on client machine • Can’t execute programs on client machine • Communicate only with server • “Java applet window” Warning

  21. Input

  22. โดยใช้ JOptionPane • คลาส JOptionPane เป็นคลาสที่อยู่ใน package javax.swing และใช้สำหรับการรับข้อมูลผ่านทางคีย์บอร์ด และแสดงข้อมูลทางจอภาพ ในการทำงานของ graphics mode หรือ GUI โดยจะแสดงออกมาในลักษณะของ popup window ที่เรียกว่า dialog box

  23. ลักษณะของ dialog box ของ JOptionPane • InputDialog • MessageDialog • ConfirmDialog

  24. Input Dialog • สร้างขึ้นจาก method ที่ชื่อ showInputDialog() ซึ่งจะ return ค่ากลับมาเป็น String • ตัวอย่างเช่น String response = JOptionPane.showInputDialog( null, “What is your name?”);

  25. Arguments of showInputDialog() • ชื่อของ parent window • ข้อความที่ต้องการแสดงใน inputDialog • ชื่อหัวข้อ (title) (option) • ชนิดของ InputDialog (option) • ERROR_MESSAGE • INFORMATION_MESSAGE • PLAIN_MESSAGE • QUESTION_MESSAGE • WARNING_MESSAGE

  26. showInputDialog()

  27. Message Dialog • MessageDialog เป็น dialog box ที่ใช้สำหรับแสดงข้อความ หรือข้อมูลต่างๆ โดยถูกสร้างจาก method ชื่อว่า showMessageDialog() • ค่า argument ของ showMessageDialog() จะเหมือนกับ showInputDialog()

  28. showMessageDialog() JOptionPane.showMessageDialog(null, "Hello..Java!!"); JOptionPane.showMessageDialog(null, "This program will terminate in 10 seconds", "Programtermination", JOptionPane.WARNING_MESSAGE);

  29. Confirm Dialog • ConfirmDialog เป็น dialog box ที่มีปุ่มคำสั่ง Yes No และ Cancel ปรากฎด้วย โดยสร้างจาก method ชื่อ showConfirmDialog() • showConfirmDialog() จะ return ค่ากลับเป็น ตัวเลขจำนวนเต็ม (integer) โดยขึ้นกับปุ่มที่ผู้ใช้คลิก นั่นคือ ปุ่ม Yes จะมีค่าเท่ากับ 0 ปุ่ม No จะมีค่าเท่ากับ 1 และปุ่ม Cancel จะมีค่าเท่ากับ 2

  30. 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

  31. 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);

  32. ฝึกปฎิบัติตัวอย่าง Input1 ถึง Input14.java

  33. 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); } }

  34. 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); } }

  35. 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); } }

  36. 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); } }

  37. 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); } }

  38. 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); } }

  39. 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); } }

  40. 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); } }

  41. 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); } }

  42. 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); } }

  43. 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); } }

  44. 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); } }

  45. 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); } }

  46. 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); } }

  47. 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); } }

  48. In NetBean6.0

More Related