-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicJFrameWithMouse.java
More file actions
executable file
·53 lines (41 loc) · 1.11 KB
/
BasicJFrameWithMouse.java
File metadata and controls
executable file
·53 lines (41 loc) · 1.11 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
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;
// Example with a button click
public class BasicJFrameWithMouse implements ActionListener{
JFrame frame;
JPanel myPanel;
JLabel label;
JButton button;
public BasicJFrameWithMouse () {
frame = new JFrame ("Basic JFrame With Mouse Example");
frame.setPreferredSize(new Dimension(200, 250));
frame.setLocation(200, 200);
myPanel = new JPanel ();
label = new JLabel ("Hello World!");
button = new JButton ("Hide");
button.setActionCommand ("Hide");
button.addActionListener(this);
myPanel.add (label);
myPanel.add (button);
frame.add (myPanel);
frame.pack ();
frame.setVisible (true);
}
public void actionPerformed (ActionEvent event) {
String eventName = event.getActionCommand();
if (eventName.equals ("Hide")) {
label.setText("");
button.setText("Show");
button.setActionCommand ("Show");
}
else {
label.setText ("Hello, World!");
button.setText ("Hide");
button.setActionCommand ("Hide");
}
}
public static void main (String [] args){
new BasicJFrameWithMouse ();
}
}