-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButton.java
More file actions
59 lines (50 loc) · 1.51 KB
/
Copy pathButton.java
File metadata and controls
59 lines (50 loc) · 1.51 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Button extends JButton {
private String first;
private String second = "";
private boolean secondDisplayed = false;
private Color color;
private static Calculator calculator;
public Button(String text, Dimension size) {
this.first = text;
setText(first);
// there might be a better way to test if a string is an integer
try {
Integer.parseInt(this.first);
this.color = new Color(200, 200, 200);
} catch (NumberFormatException e) {
this.color = new Color(175, 175, 175);
}
setBackground(this.color);
setFocusable(false);
setFont(new Font(Font.MONOSPACED, Font.BOLD, 20));
setPreferredSize(size);
addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
onClick();
}
});
}
public String getFirst() {
return first;
}
public void setSecond(String second) {
this.second = second;
}
public void toggleSecond() {
this.secondDisplayed = !this.secondDisplayed;
if (second.length() > 0 && secondDisplayed) {
this.setText(second);
} else {
this.setText(first);
}
}
private void onClick() {
calculator.buttonClicked(this);
}
public static void setCalculator(Calculator calculator) {
Button.calculator = calculator;
}
}