-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicJFrameWithLayouts.java
More file actions
executable file
·60 lines (46 loc) · 1.43 KB
/
BasicJFrameWithLayouts.java
File metadata and controls
executable file
·60 lines (46 loc) · 1.43 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
import javax.swing.*;
import java.awt.Dimension;
import java.awt.event.*;
// There are many different layout managers
public class BasicJFrameWithLayouts implements ActionListener{
JFrame frame;
JPanel myPanel;
JLabel label;
JButton button;
public BasicJFrameWithLayouts () {
frame = new JFrame ("Basic JFrame With Layout Example");
frame.setPreferredSize(new Dimension(200, 250));
frame.setLocation(200, 200);
myPanel = new JPanel ();
myPanel.setLayout (new BoxLayout (myPanel, BoxLayout.Y_AXIS));
myPanel.setBorder (BorderFactory.createEmptyBorder (20,20,20,20));
label = new JLabel ("Hello World!");
label.setAlignmentX (JLabel.CENTER_ALIGNMENT);
label.setBorder (BorderFactory.createEmptyBorder (20,50,20,50));
button = new JButton ("Hide");
button.setAlignmentX (JButton.CENTER_ALIGNMENT);
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 BasicJFrameWithLayouts ();
}
}