-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetGUI.java
124 lines (105 loc) · 3.32 KB
/
SetGUI.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/**
* GUI for the calculator, based on the former labs.
*
* @author jonasblome
* @author n-c0de-r
* @version 17.06.2021
*/
public class SetGUI implements ActionListener {
private SetEngine calc;
private JFrame frame;
private JTextField setA;
private JTextField setB;
private JTextField result;
public SetGUI(SetEngine engine) {
calc = engine;
makeFrame();
frame.setVisible(true);
}
/**
* Set the visibility of the interface.
*
* @param visible true if the interface is to be made visible, false otherwise.
*/
public void setVisible(boolean visible) {
frame.setVisible(visible);
}
private void makeFrame() {
frame = new JFrame("Set Calculator");
JPanel contentPane = (JPanel) frame.getContentPane();
contentPane.setLayout(new BorderLayout(8, 8));
contentPane.setBorder(new EmptyBorder(10, 10, 10, 10));
JPanel inputPanel = new JPanel(new GridLayout(2, 1));
setA = new JTextField("Input set A here");
inputPanel.add(setA, BorderLayout.NORTH);
setB = new JTextField("Type elements for B here");
inputPanel.add(setB, BorderLayout.SOUTH);
contentPane.add(inputPanel, BorderLayout.NORTH);
JPanel buttonPanel = new JPanel(new GridLayout(2, 3));
addButton(buttonPanel, "Union");
addButton(buttonPanel, "Intersection");
addButton(buttonPanel, "Subtraction");
addButton(buttonPanel, "Length Set A");
addButton(buttonPanel, "Length Set B");
addButton(buttonPanel, "Clear Fields");
contentPane.add(buttonPanel, BorderLayout.CENTER);
result = new JTextField();
contentPane.add(result, BorderLayout.SOUTH);
result.setEditable(false);
frame.pack();
}
/**
* Performs an action according to the String labeling a button.
*
* @param event The event causing the action, a button click.
*/
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
String A = setA.getText();
String B = setB.getText();
String str = "";
if (command.equals("Union")) {
str = calc.union(A, B);
} else if (command.equals("Intersection")) {
str = calc.intersection(A, B);
} else if (command.equals("Subtraction")) {
str = calc.subtraction(A, B);
} else if (command.equals("Length Set A")) {
str = "Length of Set A is: " + calc.length(A);
} else if (command.equals("Length Set B")) {
str = "Length of Set B is: " + calc.length(B);
} else if (command.equals("Clear Fields")) {
clear();
}
result.setText(str);
}
/**
* Add a button to the button panel.
*
* @param panel The panel to receive the button.
* @param buttonText The text for the button.
*/
protected void addButton(Container panel, String buttonText) {
JButton button = new JButton(buttonText);
button.addActionListener(this);
panel.add(button);
}
/**
* Clears all input and output fields.
*/
private void clear() {
setA.setText("Input set A here");
setB.setText("Type elements for B here");
result.setText("");
}
}