forked from cstrahan/aduni
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDialogTest.java
More file actions
106 lines (84 loc) · 2.72 KB
/
DialogTest.java
File metadata and controls
106 lines (84 loc) · 2.72 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
101
102
103
104
105
106
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("DialogTest");
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.
JTextField text = new JTextField(10);
// Menu items created by menu.add(), still need to keep them for handler
JMenuItem ditem;
// We are using button Listener verbatim. Only difference
// is that we changed our buttons above to JMenuItems
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
// check source, if green button, set color
if(e.getSource() == ditem){
// We do not seem to be able to create and re-use a
// dialog the way the book does! Must create new every time???
MyDialog dialog = new MyDialog(null);
dialog.show(); // popup dialog
// get data out
current = dialog.getCurrent();
}
else if(e.getSource() == text){
String s = text.getText();
if(s != null) msg = s; // install as new msg
}
repaint();
}
}
// Need myframe so we can add menubar
// (could do in main and pass in menubar instead)
MyPanel(JFrame myframe){
// Need to add menu stuff and add to frame (not panel)
JMenuBar mb = new JMenuBar();
myframe.setJMenuBar(mb);
// create menu and add to menuber
JMenu cmenu = new JMenu("Colors");
// Note: book is in error here, calls non-existant addMenu()
mb.add(cmenu);
// add() method on menu creates and adds menu items
ditem = cmenu.add("Popup Dialog");
// make one instance of listener and add to all
// works exactly the same as for buttons
ButtonHandler b = new ButtonHandler();
ditem.addActionListener(b);
text.addActionListener(b);
// add text field
add(text);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
// System.out.println("paintComponent");
g.setColor(current);
g.drawString(msg,50,100);
}
}
public class DialogTest{
// OK let's have our main() create a frame.
public static void main(String[] args){
MyFrame myframe = new MyFrame();
MyPanel mypanel = new MyPanel(myframe);
// Random stuff we just have to do (the book explains, kind of)
Container contentPane = myframe.getContentPane();
// add panel
contentPane.add(mypanel);
myframe.show();
}
}
/*
*/