-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicJFrameWithColours.java
More file actions
executable file
·61 lines (46 loc) · 1.52 KB
/
BasicJFrameWithColours.java
File metadata and controls
executable file
·61 lines (46 loc) · 1.52 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
// Text fields help get input from the user
public class BasicJFrameWithColours implements ActionListener{
JFrame frame;
JPanel myPanel;
JTextField name;
JButton displayMessage;
JLabel textFieldPrompt, hello;
public BasicJFrameWithColours () {
frame = new JFrame ("Basic JFrame with Colours Example");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(300, 300));
frame.setLocation(200, 200);
myPanel = new JPanel ();
myPanel.setLayout (new GridLayout (0,2,5,10));
myPanel.setBackground (Color.white);
myPanel.setBorder (BorderFactory.createEmptyBorder (10,10,10,10));
textFieldPrompt = new JLabel ("Type in your name: ");
textFieldPrompt.setForeground(Color.red);
name = new JTextField (10);
name.setBackground (Color.pink);
name.setForeground (Color.darkGray);
displayMessage = new JButton ("Display Message");
displayMessage.setBackground (Color.yellow);
displayMessage.setForeground (Color.blue);
displayMessage.addActionListener (this);
hello = new JLabel (" ");
hello.setForeground (Color.green);
myPanel.add (textFieldPrompt);
myPanel.add (name);
myPanel.add (displayMessage);
myPanel.add (hello);
frame.add (myPanel);
frame.pack ();
frame.setVisible (true);
}
public void actionPerformed (ActionEvent event) {
String text = name.getText ();
hello.setText ("Hello " + text);
}
public static void main (String [] args){
new BasicJFrameWithColours ();
}
}