-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainFrame.java
94 lines (73 loc) · 2.86 KB
/
MainFrame.java
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
package newPanel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class MainFrame extends JFrame{
final private Font mainFont = new Font("Segoe print", Font.BOLD, 18);
JTextField tfFirstName, tfLastName;
JLabel lbWelcome;
public void initialize(){
//Form Panel
JLabel lbFirstName = new JLabel("First Name");
lbFirstName.setFont(mainFont);
tfFirstName = new JTextField();
tfFirstName.setFont(mainFont);
JLabel lbLastName = new JLabel("Last Name");
lbLastName.setFont(mainFont);
tfLastName = new JTextField();
tfLastName.setFont(mainFont);
JPanel formPanel = new JPanel();
formPanel.setLayout(new GridLayout(4, 1, 5, 5));
formPanel.add(lbFirstName);
formPanel.add(tfFirstName);
formPanel.add(lbLastName);
formPanel.add(tfLastName);
//Welcome label
lbWelcome = new JLabel();
lbWelcome.setFont(mainFont);
//Buttons Panel
JButton btnOK = new JButton("OK");
btnOK.setFont(mainFont);
btnOK.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String firstName = tfFirstName.getText();
String lastName = tfLastName.getText();
lbWelcome.setText("Hello "+ firstName +" "+ lastName);
}
});
JButton btnClear = new JButton("Clear");
btnClear.setFont(mainFont);
btnClear.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
tfFirstName.setText("");
tfLastName.setText("");
lbWelcome.setText("");
}
});
JPanel buttonsPanel = new JPanel();
buttonsPanel.setLayout(new GridLayout(1, 2, 5,5));
buttonsPanel.add(btnOK);
buttonsPanel.add(btnClear);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());//BorderLayout divides the panel into 5 parts: north,sout,east,west and center.
mainPanel.setBackground(new Color(128,128,255));
mainPanel.add(formPanel,BorderLayout.NORTH);
mainPanel.add(lbWelcome,BorderLayout.CENTER);
mainPanel.add(buttonsPanel,BorderLayout.SOUTH);
add(mainPanel);
setTitle("Welcome");
setSize(500,600);
setMinimumSize(new Dimension(300,400));
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String args[]){
MainFrame myFrame = new MainFrame();
myFrame.initialize();
}
}