forked from rick2785/JavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaLesson29.java
More file actions
170 lines (123 loc) · 4.19 KB
/
JavaLesson29.java
File metadata and controls
170 lines (123 loc) · 4.19 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import javax.swing.*;
public class JavaLesson29 extends JFrame{
JButton but1, but2, but3, but4, but5, but6,
but7, but8, but9, but0, butPlus, butMinus,
clearAll;
JTextField textResult;
int num1, num2;
public static void main(String[] args){
new JavaLesson29();
}
public JavaLesson29(){
// Create the frame, position it and handle closing it
this.setSize(400,400);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Calculator");
JPanel thePanel = new JPanel();
// Create a Grid Layout with as many rows as needed
// and 3 columns. The last 2 parameters create a
// horizontal gap of 2 pixels and a vertical gap of
// 2 pixels.
// thePanel.setLayout(new GridLayout(0,3,2,2));
thePanel.setLayout(new GridBagLayout());
// You create a GridBagContraints object that defines
// defaults for your components
GridBagConstraints gridConstraints = new GridBagConstraints();
// Define the x position of the component
gridConstraints.gridx = 1;
// Define the y position of the component
gridConstraints.gridy = 1;
// Number of columns the component takes up
gridConstraints.gridwidth = 1;
// Number of rows the component takes up
gridConstraints.gridheight = 1;
// Gives the layout manager a hint on how to adjust
// component width (0 equals fixed)
gridConstraints.weightx = 50;
// Gives the layout manager a hint on how to adjust
// component height (0 equals fixed)
gridConstraints.weighty = 100;
// Defines padding top, left, bottom, right
gridConstraints.insets = new Insets(5,5,5,5);
// Defines where to place components if they don't
// fill the space: CENTER, NORTH, SOUTH, EAST, WEST
// NORTHEAST, etc.
gridConstraints.anchor = GridBagConstraints.CENTER;
// How should the component be stretched to fill the
// space: NONE, HORIZONTAL, VERTICAL, BOTH
gridConstraints.fill = GridBagConstraints.BOTH;
textResult = new JTextField("0",20);
// Defines the font to use in the text field
Font font = new Font("Helvetica", Font.PLAIN, 18);
textResult.setFont(font);
but1 = new JButton("1");
but2 = new JButton("2");
but3 = new JButton("3");
but4 = new JButton("4");
but5 = new JButton("5");
but6 = new JButton("6");
but7 = new JButton("7");
but8 = new JButton("8");
but9 = new JButton("9");
butPlus = new JButton("+");
but0 = new JButton("0");
butMinus = new JButton("-");
clearAll = new JButton("C");
thePanel.add(clearAll,gridConstraints);
gridConstraints.gridwidth = 20;
gridConstraints.gridx = 5;
thePanel.add(textResult,gridConstraints);
gridConstraints.gridwidth = 1;
gridConstraints.gridx = 1;
gridConstraints.gridy = 2;
thePanel.add(but1,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but2,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(but3,gridConstraints);
gridConstraints.gridx = 1;
gridConstraints.gridy = 3;
thePanel.add(but4,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but5,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(but6,gridConstraints);
gridConstraints.gridx = 1;
gridConstraints.gridy = 4;
thePanel.add(but7,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but8,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(but9,gridConstraints);
gridConstraints.gridx = 1;
gridConstraints.gridy = 5;
thePanel.add(butPlus,gridConstraints);
gridConstraints.gridx = 5;
thePanel.add(but0,gridConstraints);
gridConstraints.gridx = 9;
thePanel.add(butMinus,gridConstraints);
// Adding buttons using the Grid Layout
/*
thePanel.add(but1);
thePanel.add(but2);
thePanel.add(but3);
thePanel.add(but4);
thePanel.add(but5);
thePanel.add(but6);
thePanel.add(but7);
thePanel.add(but8);
thePanel.add(but9);
thePanel.add(butPlus);
thePanel.add(but0);
thePanel.add(butMinus);
*/
this.add(thePanel);
this.setVisible(true);
} // END OF JavaLesson29 CONSTRUCTOR
} // END OF JavaLesson29 CLASS