-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJButtonDemo.java
More file actions
57 lines (57 loc) · 1.46 KB
/
JButtonDemo.java
File metadata and controls
57 lines (57 loc) · 1.46 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*
<applet code = "JButtonDemo" width = 500 height = 750>
</applet>
*/
public class JButtonDemo extends JApplet {
JLabel jlab;
public void init(){
try{
SwingUtilities.invokeAndWait( //invokeLater()
new Runnable() { // anonymous inner class
public void run() {
makeGUI();
}
}
);
}catch(Exception exc){
System.out.println("Can't create because of " + exc);
}
}
private void makeGUI(){
//change to flow layout
setLayout(new FlowLayout()); // default for swing is border layout
JButton jb = new JButton("NORTH");
add(jb);
jb.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae){
jlab.setText("You selected " + ae.getActionCommand());
}
});
jb = new JButton("SOUTH");
add(jb);
jb.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae){ // Anonymous inner class
jlab.setText("You selected " + ae.getActionCommand());
}
});
jb = new JButton("EAST");
add(jb);
jb.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae){
jlab.setText("You selected " + ae.getActionCommand());
}
});
jb = new JButton("WEST");
add(jb);
jb.addActionListener( new ActionListener(){
public void actionPerformed(ActionEvent ae){
jlab.setText("You selected " + ae.getActionCommand());
}
});
jlab = new JLabel("Choose a direction");
add(jlab);
}
}