-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayoutManagersDemo.java
More file actions
97 lines (79 loc) · 2.92 KB
/
LayoutManagersDemo.java
File metadata and controls
97 lines (79 loc) · 2.92 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
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class LayoutManagersDemo {
public static void main(String[] args) {
SwingUtilities.invokeLater(LayoutManagersDemo::createAndShowGui);
}
private static void createAndShowGui() {
JFrame frame = new JFrame("Two Layout Managers Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel();
content.setLayout(new BoxLayout(content, BoxLayout.Y_AXIS));
content.add(createFlowLayoutPanel());
content.add(createGridBagLayoutPanel());
frame.setContentPane(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private static JPanel createFlowLayoutPanel() {
JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
panel.setBorder(BorderFactory.createTitledBorder("FlowLayout"));
JTextField textBox = new JTextField(12);
JButton button = new JButton("Submit");
JRadioButton radioButton = new JRadioButton("Option A");
JCheckBox checkBox = new JCheckBox("Enable");
JLabel label = new JLabel("Status: Ready");
panel.add(new JLabel("Name:"));
panel.add(textBox);
panel.add(button);
panel.add(radioButton);
panel.add(checkBox);
panel.add(label);
return panel;
}
private static JPanel createGridBagLayoutPanel() {
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("GridBagLayout"));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(6, 6, 6, 6);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel promptLabel = new JLabel("Name:");
JTextField textBox = new JTextField(12);
JButton button = new JButton("Submit");
JRadioButton radioButton = new JRadioButton("Option A");
JCheckBox checkBox = new JCheckBox("Enable");
JLabel label = new JLabel("Status: Ready");
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(promptLabel, gbc);
gbc.gridx = 1;
gbc.weightx = 1.0;
panel.add(textBox, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 0.0;
panel.add(button, gbc);
gbc.gridx = 1;
panel.add(radioButton, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
panel.add(checkBox, gbc);
gbc.gridx = 1;
panel.add(label, gbc);
return panel;
}
}