forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetTest.java
More file actions
100 lines (77 loc) · 2.16 KB
/
WidgetTest.java
File metadata and controls
100 lines (77 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
/**
* Main class for game. Does window stuff and display
*/
class MyFrame extends JFrame{
public MyFrame(){
setTitle("WidgetTest");
setSize(200,200); // size in pixels
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel{
// Color and string are now instance vars so we can change them
Color current = Color.red;
String msg = "Hello World";
// button variables now instace vars.
// May as well init here.
JButton toggle = new JButton("Toggle");
JButton green = new JButton("Green");
JTextField text = new JTextField(10);
// Button listener. Now an inner class so we
// can access mypanel data.
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
// check source, if green button, set color
if(e.getSource() == green){
current = Color.green;
}
else if(e.getSource() == text){
String s = text.getText();
if(s != null) msg = s; // install as new msg
repaint();
}
else{ // do toggle stuff
if(current.equals(Color.red))
current = Color.blue;
else
current = Color.red;
}
repaint();
}
}
MyPanel(){
// make one instance of listener and add to all
ButtonHandler b = new ButtonHandler();
toggle.addActionListener(b);
green.addActionListener(b);
text.addActionListener(b);
// add buttons (and text field)
add(toggle);
add(green);
add(text);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
// System.out.println("paintComponent");
g.setColor(current);
g.drawString(msg,50,100);
}
}
public class WidgetTest{
// OK let's have our main() create a frame.
public static void main(String[] args){
MyFrame myframe = new MyFrame();
MyPanel mypanel = new MyPanel();
// Random stuff we just have to do (the book explains, kind of)
Container contentPane = myframe.getContentPane();
// add panel
contentPane.add(mypanel);
myframe.show();
}
}
/*
*/