-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyButton.java
More file actions
42 lines (36 loc) · 1.2 KB
/
MyButton.java
File metadata and controls
42 lines (36 loc) · 1.2 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
// File: MyButton.java
import javax.swing.*;
import java.awt.*;
// Uppgift 2: Create a button class with two states
public class MyButton extends JButton {
// Variables to hold state information
private final String state1Text;
private final String state2Text;
private final Color state1Color;
private final Color state2Color;
private boolean isState1;
// Constructor for the button with two states
public MyButton(String state1Text, String state2Text, Color state1Color, Color state2Color) {
this.state1Text = state1Text;
this.state2Text = state2Text;
this.state1Color = state1Color;
this.state2Color = state2Color;
this.isState1 = true;
// Initialize button with state 1
setText(state1Text);
setBackground(state1Color);
}
// Uppgift 2: Method to toggle between the two states
public void toggleState() {
if (isState1) {
// Switch to state 2
setText(state2Text);
setBackground(state2Color);
} else {
// Switch back to state 1
setText(state1Text);
setBackground(state1Color);
}
isState1 = !isState1;
}
}