forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson28.java
More file actions
131 lines (92 loc) · 3.09 KB
/
JavaLesson28.java
File metadata and controls
131 lines (92 loc) · 3.09 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
public class JavaLesson28 extends JFrame{
JButton button1, button2, button3, button4, button5;
String outputString = "";
public static void main(String[] args){
new JavaLesson28();
}
public JavaLesson28(){
// Create the frame, position it and handle closing it
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("My Sixth Frame");
/* FLOW LAYOUT
// Create a flow layout (Default)
JPanel thePanel = new JPanel();
// Define the flow layout alignment
// FlowLayout.RIGHT, FlowLayout.CENTER
thePanel.setLayout(new FlowLayout(FlowLayout.LEFT));
// You can also define the pixels that separate the components
// FlowLayout(alignment, horz gap, vertical gap)
button1 = new JButton("Button 1");
thePanel.add(button1);
END FLOW LAYOUT*/
/* BORDER LAYOUT */
JPanel thePanel = new JPanel();
thePanel.setLayout(new BorderLayout());
// Create buttons
button1 = new JButton("Button 1");
button2 = new JButton("Button 2");
button3 = new JButton("Button 3");
button4 = new JButton("Button 4");
button5 = new JButton("Button 5");
// If you put components in the same space the
// last one in stays and everything else goes
// EX.
// thePanel.add(button1, BorderLayout.NORTH);
// thePanel.add(button2, BorderLayout.NORTH);
// Only button2 shows
/*
thePanel.add(button1, BorderLayout.NORTH);
thePanel.add(button2, BorderLayout.SOUTH);
thePanel.add(button3, BorderLayout.EAST);
thePanel.add(button4, BorderLayout.WEST);
thePanel.add(button5, BorderLayout.CENTER);
*/
/* If you want more than one component to show
// up in the same part of a border layout put
// them in a panel and then add the panel to
// the border layout panel
JPanel thePanel2 = new JPanel();
thePanel2.add(button1);
thePanel2.add(button2);
thePanel.add(thePanel2, BorderLayout.NORTH);
*/
/* BOX LAYOUT */
Box theBox = Box.createHorizontalBox();
// You can also use Box theBox = Box.createVerticalBox();
/*
theBox.add(button1);
theBox.add(button2);
theBox.add(button3);
theBox.add(button4);
*/
// You can also separate the components with struts
/*
theBox.add(button1);
theBox.add(Box.createHorizontalStrut(4));
theBox.add(button2);
theBox.add(Box.createHorizontalStrut(4));
theBox.add(button3);
theBox.add(Box.createHorizontalStrut(4));
theBox.add(button4);
*/
// A rigid area gives you the option to space using
// horizontal and vertical spacing
theBox.add(button1);
theBox.add(button2);
// theBox.add(Box.createRigidArea(new Dimension(30, 20)));
// When you use a glue you position the components as
// far apart as possible while remaining on the screen
// There is also a createVerticalGlue
theBox.add(Box.createHorizontalGlue());
theBox.add(button3);
this.add(theBox);
// this.add(thePanel); // Don't use for BOX LAYOUT
this.setVisible(true);
}
}