-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathButtonRandomColor.java
More file actions
51 lines (38 loc) · 1.17 KB
/
ButtonRandomColor.java
File metadata and controls
51 lines (38 loc) · 1.17 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
package gui;
/**
* RUN:
* javac -cp .; gui/ButtonRandomColor.java && java -cp .; gui.ButtonRandomColor
* OUTPUT:
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import net.mindview.util.*;
public class ButtonRandomColor extends JFrame {
private static final int WIDTH = 300;
private static final int HEIGHT = 230;
private Random rand = new Random(47);
public ButtonRandomColor() {
ColorButton button = new ColorButton("Color button");
add(button);
}
private ActionListener bl = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof ColorButton) {
((ColorButton)e.getSource()).setBackground(new Color(rand.nextInt(0xFFFFFF)));
}
}
};
class ColorButton extends JButton {
public ColorButton(String label) {
super(label);
addActionListener(bl);
bl.actionPerformed(new ActionEvent(this, 0, ""));
}
}
public static void main(String[] args) {
SwingConsole.run(new ButtonRandomColor(), WIDTH, HEIGHT);
}
}