This repository was archived by the owner on Dec 17, 2021. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoButtons.java
More file actions
Latest commit
65 lines (49 loc) · 1.84 KB
/
Copy pathTwoButtons.java
File metadata and controls
65 lines (49 loc) · 1.84 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
62
63
64
65
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassTwoButtons {
JFrameframe;
JLabellabel;
publicstaticvoidmain (String[] args) {
TwoButtonsgui = newTwoButtons();
gui.go();
}
publicvoidgo() {
frame = newJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButtonlabelButton = newJButton("Change Label");
labelButton.addActionListener(newLabelButtonListener());
JButtoncolorButton = newJButton("Change Circle");
colorButton.addActionListener(newColorButtonListener());
label = newJLabel("I'm a label");
MyDrawPaneldrawPanel = newMyDrawPanel();
frame.getContentPane().add(BorderLayout.SOUTH, colorButton);
frame.getContentPane().add(BorderLayout.CENTER, drawPanel);
frame.getContentPane().add(BorderLayout.EAST, labelButton);
frame.getContentPane().add(BorderLayout.WEST, label);
frame.setSize(420,300);
frame.setVisible(true);
}
classLabelButtonListenerimplementsActionListener {
publicvoidactionPerformed(ActionEventevent) {
label.setText("Ouch!");
}
} // close inner class
classColorButtonListenerimplementsActionListener {
publicvoidactionPerformed(ActionEventevent) {
frame.repaint();
}
} // close inner class
}
classMyDrawPanelextendsJPanel {
publicvoidpaintComponent(Graphicsg) {
g.fillRect(0,0,this.getWidth(), this.getHeight());
// make random colors to fill with
intred = (int) (Math.random() * 255);
intgreen = (int) (Math.random() * 255);
intblue = (int) (Math.random() * 255);
ColorrandomColor = newColor(red, green, blue);
g.setColor(randomColor);
g.fillOval(70,70,100,100);
}
}