-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjasondoesntknow.java
117 lines (102 loc) · 2.9 KB
/
jasondoesntknow.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
import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComponent;
import javax.swing.JButton;
import java.awt.EventQueue;
import java.io.*;
import javax.swing.AbstractAction;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.DefaultListModel;
import javax.swing.JList;
public class JasonDoesntKnow extends JFrame {
public JasonDoesntKnow() {
initUI();
}
private void initUI() {
var listModel = new DefaultListModel();
var textDisplay = new JList(listModel);
var field = new JTextField(20);
field.addActionListener(new ActionListener() {
@Override //what is this
public void actionPerformed(ActionEvent event) {
outputToFile(field.getText());
}
});
var pressA = new JButton("A");
pressA.addActionListener(new ActionListener() {
@Override //what is this
public void actionPerformed(ActionEvent event) {
updateDisplay(listModel, "Button A");
}
});
var pressB = new JButton("B");
pressB.addActionListener(new ActionListener() {
@Override //what is this
public void actionPerformed(ActionEvent event) {
updateDisplay(listModel, "Button B");
}
});
var pressC = new JButton("C");
pressC.addActionListener(new ActionListener() {
@Override //what is this
public void actionPerformed(ActionEvent event) {
updateDisplay(listModel, "Button C");
}
});
createLayout(field, pressA, pressB, pressC, textDisplay);
setTitle("Jason Doesn't Know");
setLocationRelativeTo(null); //centre window
setDefaultCloseOperation(EXIT_ON_CLOSE); //make X button work
}
private void outputToFile(String arg) {
PrintWriter out = null;
try {
out = new PrintWriter("./jasonout.txt");
out.println(arg);
} catch (IOException e) {
} finally {
if (out != null) {
out.close();
}
}
}
private void updateDisplay(DefaultListModel model, String arg) {
if(!model.isEmpty()) {
model.clear();
}
model.addElement(arg);
}
private void createLayout(JComponent... arg) {
var pane = getContentPane();
var gl = new GroupLayout(pane);
pane.setLayout(gl);
gl.setHorizontalGroup(gl.createParallelGroup()
.addComponent(arg[0],300,300,300)
.addGroup(gl.createSequentialGroup()
.addComponent(arg[1],100,100,100)
.addComponent(arg[2],100,100,100)
.addComponent(arg[3],100,100,100)
)
.addComponent(arg[4],300,300,300)
);
gl.setVerticalGroup(gl.createSequentialGroup()
.addComponent(arg[0],50,50,50)
.addGroup(gl.createParallelGroup()
.addComponent(arg[1],50,50,50)
.addComponent(arg[2],50,50,50)
.addComponent(arg[3],50,50,50)
)
.addComponent(arg[4],100,100,100)
);
pack(); //auto-size
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
var ex = new JasonDoesntKnow();
ex.setVisible(true);
});
}
}