-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathCustomComponentTest.java
73 lines (59 loc) · 2.11 KB
/
CustomComponentTest.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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Tests the custom component classes MirrorText and StopWatchLabel
* by adding them both to a panel. The panel also contains a button
* that changes the text on the MirrorText component (and also on
* the button itself. This program also demonstrates how the layout
* of the panel is recomputed when the components are changed.
*/
public class CustomComponentTest extends JPanel {
/**
* The main routine simply opens a window that shows a CustomComponentTest panel.
*/
public static void main(String[] args) {
JFrame window = new JFrame("CustomComponentText");
CustomComponentTest content = new CustomComponentTest();
window.setContentPane(content);
window.setSize(420,150);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private MirrorText greet;
private StopWatchLabel timer;
private JButton changeText;
public CustomComponentTest() {
setLayout(new FlowLayout());
greet = new MirrorText("PLEASE LET ME OUT!");
greet.setBackground(Color.black);
greet.setForeground(Color.red);
greet.setFont( new Font("SansSerif", Font.BOLD, 30) );
add(greet);
timer = new StopWatchLabel();
timer.setBackground(Color.white);
timer.setForeground(Color.blue);
timer.setOpaque(true);
timer.setFont(new Font("Serif", Font.PLAIN, 20));
add( timer );
changeText = new JButton("Change Text in this Program");
add(changeText);
changeText.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (greet.getText().equals("PLEASE LET ME OUT!"))
greet.setText("Help!");
else
greet.setText("PLEASE LET ME OUT!");
if (timer.isRunning() == false)
timer.setText("Please click me.");
if (changeText.getText().equals("Change Back"))
changeText.setText("Change Text in this Program");
else
changeText.setText("Change Back");
}
} );
}
}