Biblioteca Java - Blame information for rev 32

Subversion Repositories:
Rev:
Rev Author Line No. Line
32 mihai 1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package guidemo;
7  
8 import javax.swing.*;
9 import java.awt.event.*;
10  
11 public class TestTheDialog extends JFrame implements ActionListener {
12  
13     JButton myButton = null;
14  
15     public TestTheDialog() {
16         setTitle("Test Dialog");
17         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
18         myButton = new JButton("Test the dialog!");
19         myButton.addActionListener(this);
20         setLocationRelativeTo(null);
21         add(myButton);
22         pack();
23         setVisible(true);
24     }
25  
26     public void actionPerformed(ActionEvent e) {
27         if(myButton == e.getSource()) {
28             System.err.println("Opening dialog.");
29             CustomDialog myDialog = new CustomDialog(this, true, "Do you like Java?");
30             System.err.println("After opening dialog.");
31             if(myDialog.getAnswer()) {
32                 System.err.println("The answer stored in CustomDialog is 'true' (i.e. user clicked yes button.)");
33             }
34             else {
35                 System.err.println("The answer stored in CustomDialog is 'false' (i.e. user clicked no button.)");
36             }
37         }
38     }
39  
40     public static void main(String argv[]) {
41         TestTheDialog tester = new TestTheDialog();
42     }
43 }
44  
45 class CustomDialog extends JDialog implements ActionListener {
46     private JPanel myPanel = null;
47     private JButton yesButton = null;
48     private JButton noButton = null;
49     private boolean answer = false;
50     public boolean getAnswer() { return answer; }
51  
52     CustomDialog(JFrame frame, boolean modal, String myMessage) {
53         super(frame, modal);
54         myPanel = new JPanel();
55         getContentPane().add(myPanel);
56         myPanel.add(new JLabel(myMessage));
57         yesButton = new JButton("Yes");
58         yesButton.addActionListener(this);
59         myPanel.add(yesButton);      
60         noButton = new JButton("No");
61         noButton.addActionListener(this);
62         myPanel.add(noButton);      
63         pack();
64         setLocationRelativeTo(frame);
65         setVisible(true);
66     }
67  
68     public void actionPerformed(ActionEvent e) {
69         if(yesButton == e.getSource()) {
70             System.err.println("User chose yes.");
71             answer = true;
72             setVisible(false);
73         }
74         else if(noButton == e.getSource()) {
75             System.err.println("User chose no.");
76             answer = false;
77             setVisible(false);
78         }
79     }
80  
81 }