-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortingHat_second_GUI.java
181 lines (126 loc) · 4.52 KB
/
SortingHat_second_GUI.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.util.ArrayList;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
public class SortingHat_second_GUI implements ActionListener {
private JFrame frame ;
private JScrollPane scrollPane;
private JLabel label;
private JButton button;
private JTextField textField;
private JTextArea resultArea;
private ImageIcon icon;
private Font font;
private static int num_student;
private static int houseindex=0;
private ArrayList<ArrayList<String>> house;
public SortingHat_second_GUI(int num_student){
this.num_student = num_student;
house = new ArrayList<>();
for (int i = 0; i < 4; i++) {
house.add(new ArrayList<>());
}
//Frame
frame = new JFrame("4 King Sorting Hat");
frame.setSize(800, 700);
frame.setResizable(false);
//Image
icon = new ImageIcon("image2.png");
frame.setIconImage(icon.getImage());
// TextField
textField = new JTextField();
textField.setPreferredSize(new Dimension(100,30));
textField.setBounds(290,25,200,30);
// Label
label = new JLabel("Enter Name of student"){
// Back Group
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
try {
BufferedImage image = ImageIO.read(new File("image.png"));
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
} catch (IOException e) {
e.printStackTrace();
}
}
};
label.setVerticalAlignment(SwingConstants.TOP);
label.setHorizontalAlignment(SwingConstants.CENTER);
//Button
button = new JButton("Submit");
button.setBounds(350, 76, 80, 30);
button.addActionListener(this);
//TextArea
resultArea = new JTextArea(10,20);
resultArea.setEditable(false);
resultArea.setBounds(150,150,500,400);
resultArea.setBackground(Color.BLACK);
resultArea.setForeground(Color.white);
scrollPane = new JScrollPane(resultArea);
scrollPane.setBounds(150,150,500,400);
//Font and set
font = new Font("Arial", Font.BOLD, 16);
resultArea.setFont(font);
// Add and Other
frame.add(textField);
frame.add(button);
frame.add(scrollPane);
frame.add(label);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
//-------------------------//
// Action when press Button//
//------------------------//
public void actionPerformed(ActionEvent e) {
// Add to Arraylist
house.get(houseindex%4).add(textField.getText());
houseindex++;
if(num_student==1){
// ALL Invisible
button.setVisible(false);
textField.setVisible(false);
label.setText("List of Student");
label.setFont(font);
//result
Displayresult();
}
else{
num_student-=1;
label.setText(num_student+" Remaining");
textField.setText("");
}
}
// Divide people into 4 houses
public void Displayresult(){
resultArea.append("Pracha Chuen :\n");
for(int i=0;i<house.get(0).size();i++){
resultArea.append(i+1+". "+house.get(0).get(i)+"\n");
}
resultArea.append("--------------------------\n");
resultArea.append("\n");
resultArea.append("Indara :\n");
for(int i=0;i<house.get(1).size();i++){
resultArea.append(i+1+". "+house.get(1).get(i)+"\n");
}
resultArea.append("--------------------------\n");
resultArea.append("\n");
resultArea.append("Kanok Chiwa :\n");
for(int i=0;i<house.get(2).size();i++){
resultArea.append(i+1+". "+house.get(2).get(i)+"\n");
}
resultArea.append("--------------------------\n");
resultArea.append("\n");
resultArea.append("Boranapon :\n");
for(int i=0;i<house.get(3).size();i++){
resultArea.append(i+1+". "+house.get(3).get(i)+"\n");
}
resultArea.append("--------------------------\n");
resultArea.append("\n");
}
}