-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyFrame.java
More file actions
54 lines (42 loc) · 1.75 KB
/
MyFrame.java
File metadata and controls
54 lines (42 loc) · 1.75 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
// MyFrame.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// MyFrame class that extends JFrame
public class MyFrame extends JFrame implements ActionListener {
private MyButton[] buttons; // Array to hold multiple buttons
// Constructor
public MyFrame() {
// Set the title to the authors' names
super("Author: Kevin Lam & Allan Inma");
// Set the default close operation
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the background color to your favorite color
getContentPane().setBackground(Color.CYAN);
// Create a layout for the frame
setLayout(new FlowLayout());
setSize(400,200);
// Add multiple MyButton components
buttons = new MyButton[2]; // Create two buttons
buttons[0] = new MyButton("State 1", "State 2", Color.GREEN, Color.RED);
buttons[1] = new MyButton("ON", "OFF", Color.BLUE, Color.ORANGE);
// Add the buttons to the frame and set up action listeners
for (MyButton button : buttons) {
add(button); // Add button to the frame
button.addActionListener(this); // Connect ActionListener to buttons
}
// Pack the frame to fit the buttons
setVisible(true); // Make the frame visible
}
// Implement actionPerformed to toggle button state
@Override
public void actionPerformed(ActionEvent e) {
MyButton button = (MyButton) e.getSource(); // Get the button that was clicked
button.toggleState(); // Toggle the state of the button
}
// Main method to run the program
public static void main(String[] args) {
new MyFrame(); // Create an instance of MyFrame
}
}