-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyButton.java
More file actions
44 lines (37 loc) · 1.3 KB
/
MyButton.java
File metadata and controls
44 lines (37 loc) · 1.3 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
import javax.swing.*;
import java.awt.*;
public class MyButton extends JButton {
// Variables to store two states
private String textState1;
private String textState2;
private Color colorState1;
private Color colorState2;
private boolean isState1;
// Constructor
public MyButton(String text1, String text2, Color color1, Color color2) {
this.textState1 = text1;
this.textState2 = text2;
this.colorState1 = color1;
this.colorState2 = color2;
this.isState1 = true; // Start with state 1
// Set initial text and background color
setText(textState1);
setBackground(colorState1);
// Make sure the background color is visible
setOpaque(true);
setBorderPainted(false); // Disable the border painting
setFocusPainted(false); // Disable focus painting (removes the yellow outline)
setContentAreaFilled(true); // Ensures the button is filled with color
}
// Method to toggle the button's state
public void toggleState() {
if (isState1) {
setText(textState2);
setBackground(colorState2);
} else {
setText(textState1);
setBackground(colorState1);
}
isState1 = !isState1; // Toggle the state flag
}
}