|
01 package intermediate;
02
03 import java.awt.FlowLayout;
04 import java.awt.event.ActionEvent;
05 import java.awt.event.ActionListener;
06
07 import javax.swing.*;
08
09 public class GUIPanelExample extends JPanel
10 {
11 private JButton button;
12 private JTextArea text;
13
14 public GUIPanelExample()
15 {
16 text=new JTextArea(10, 40);
17 setLayout(new FlowLayout());
18 button=new JButton("Click me");
19 add(button);
20 add(new JScrollPane(text));
21 button.addActionListener(new React2());
22 }
23
24 class React2 implements ActionListener
25 {
26
27 public void actionPerformed(ActionEvent e)
28 {
29 text.append("Clicked");
30 }
31
32 }
33
34
35 /**
36 * @param args
37 */
38 public static void main(String[] args)
39 {
40 //other inner class
41 JPanel example1=new GUIPanelExample();
42 JPanel example2=new GUIPanelExample();
43 JFrame frame=new JFrame("Double it");
44 frame.getContentPane().setLayout(new FlowLayout());
45 frame.getContentPane().add(example1);
46 frame.getContentPane().add(example2);
47 frame.pack();
48 frame.setVisible(true);
49
50 /*JFrame frame=new JFrame("My simple window");
51 frame.setVisible(true);
52 JButton button=new JButton("Click me");
53 final JTextArea text=new JTextArea(10, 40);
54 frame.getContentPane().setLayout(new FlowLayout());
55 frame.getContentPane().add(button);
56 frame.getContentPane().add(new JScrollPane(text));
57 frame.pack();
58
59 //inner class version
60 class React implements ActionListener
61 {
62
63 public void actionPerformed(ActionEvent e)
64 {
65 text.append("Clicked");
66 }
67
68 }
69
70 button.addActionListener(new React());
71 */
72 //anonymous class version
73 /*button.addActionListener(
74 new ActionListener()
75 {
76 public void actionPerformed(ActionEvent arg0) {
77 text.append("Clicked");
78
79 }
80 });*/
81 }
82
83 }
|