Repository navigation
Expand file tree
/
Copy pathTextAdder.java
More file actions
62 lines (55 loc) · 1.66 KB
/
Copy pathTextAdder.java
File metadata and controls
62 lines (55 loc) · 1.66 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
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TextAdder extends JFrame implements ActionListener
{
private JLabel labelPlus, labelSum;
private JButton buttonEquals;
private JTextField text1, text2;
// _4______ + _________ = [ ]
public TextAdder()
{
//JFrame Stuff
setSize(500,100);
setTitle("Text Adder");
setDefaultCloseOperation(EXIT_ON_CLOSE);
//instantiate my JComponents
labelPlus = new JLabel("+", JLabel.CENTER);//overloaded constructor for label using alignment
labelSum = new JLabel ("", JLabel.RIGHT);//we can align center or right (default is left)
buttonEquals = new JButton ("=");
text1 = new JTextField("4");//overloaded constructor with pre-data
text2 = new JTextField();//default constructor with no data
//set up pane and layout
Container pane = this.getContentPane();
pane.setLayout(new GridLayout(1,5));
//added elements to the layout
pane.add(text1);
pane.add(labelPlus);
pane.add(text2);
pane.add(buttonEquals);
pane.add(labelSum);
//set up the action listener/button
buttonEquals.addActionListener(this);
//make it all visible
setVisible (true);
}
public void actionPerformed (ActionEvent e)
{
if (e.getActionCommand().equals("="))
{
int num1 = Integer.parseInt(text1.getText());
int num2 = Integer.parseInt(text2.getText());
int sum = num1 + num2;
labelSum.setText(String.valueOf(sum));
}
}
public static void main (String[] args)
{
TextAdder t = new TextAdder();
}
}