-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_textfield_btn_label.java
More file actions
50 lines (39 loc) · 1.24 KB
/
03_textfield_btn_label.java
File metadata and controls
50 lines (39 loc) · 1.24 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
import javax.swing.*;
import java.awt.event.*;
public class GUIApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Cupertinii Swing GUI");
frame.setSize(1280,800);
frame.setLayout(null);
JLabel label = new JLabel();
label.setBounds(200, 200, 200, 50);
JTextField txt = new JTextField("Input a number");
txt.setBounds(200, 100, 200, 50);
JButton btn = new JButton("Calculate");
btn.setBounds(200, 150, 200, 50);
// This function will handle action like pressing enter in text field.
txt.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate(txt, label);
}
});
// Btn click
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
calculate(txt, label);
}
});
// add label and txt field to app frame.
frame.add(label);
frame.add(txt);
frame.add(btn);
frame.setVisible(true);
}
public static void calculate(JTextField txt, JLabel label) {
String textFieldData = txt.getText();
int input = Integer.parseInt(textFieldData);
int ouput = input * input;
label.setText("Square of " + input + " is " + ouput);
return;
}
}