COMP2396
Loading...
Searching...
No Matches
TwoButtons.java
Go to the documentation of this file.
1import javax.swing.*;
2import java.awt.*;
3import java.awt.event.*;
4
5public class TwoButtons {
6 JFrame frame;
7 JLabel label;
8
9 public static void main(String[] args) {
10 TwoButtons gui = new TwoButtons();
11 gui.go();
12 }
13
14 public void go() {
15 frame = new JFrame();
16 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
17 JButton labelButton = new JButton("Change Label");
18 labelButton.addActionListener(new LabelListener());
19 JButton colorButton = new JButton("Change Circle");
20 colorButton.addActionListener(new ColorListener());
21 label = new JLabel("I'm a label");
22 MyDrawPanel2 drawPanel = new MyDrawPanel2();
23 frame.add(colorButton, BorderLayout.SOUTH);
24 frame.add(drawPanel, BorderLayout.CENTER);
25 frame.add(labelButton, BorderLayout.WEST);
26 frame.add(label, BorderLayout.EAST);
27 frame.setSize(500, 300);
28 frame.setVisible(true);
29 }
30
31 class LabelListener implements ActionListener {
32 public void actionPerformed(ActionEvent event) {
33 label.setText("Ouch!");
34 }
35 } // close inner class
36
37 class ColorListener implements ActionListener {
38 public void actionPerformed(ActionEvent event) {
39 frame.repaint();
40 }
41 } // close inner class
42}
static void main(String[] args)