-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.java
More file actions
129 lines (108 loc) · 3.15 KB
/
GUI.java
File metadata and controls
129 lines (108 loc) · 3.15 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
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
package WindowsSettings;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JComboBox;
public class GUI implements ActionListener {
private JLabel empty;
private JLabel server;
private JLabel message;
private JFrame frame;
private JPanel panel;
private List<String> words = new ArrayList<String>();
private JComboBox<String> cmdChoice;
private JButton button;
private DNS dns = new DNS();
public GUI() {
//Create new frame(visual)
frame = new JFrame();
/**
* Create new button with text "Click me".
* We can add functions to our buttons, this to
* specify that we use actionslistener from this class.
* Opens button not as field variable because ordinarly we
* want more than one button
*/
button = new JButton("Change DNS");
button.addActionListener(this);
/**
* Create new label with text "Number of clicks"
* have to be accessable for methods outside contructor,
* therefor the JLabel label is set as a field variable
*
*/
message = new JLabel();
empty = new JLabel();
server = new JLabel("Server");
List<String> routers = dns.routers();
String[] choice = new String[routers.size()];
for(int i = 0; i < routers.size(); i++) {
choice[i] = routers.get(i);
}
cmdChoice = new JComboBox<String>(choice);
cmdChoice.setSelectedIndex(choice.length-1);
cmdChoice.addActionListener(this);
/**
* Create new panel, which holds all of our different objects.
* Add our objects to the panel
*/
panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 40));
panel.setLayout(new GridLayout(3, 2));
panel.setBackground(Color.gray);
panel.add(server);
panel.add(empty);
panel.add(cmdChoice);
panel.add(button);
panel.add(message);
/**
* Adds our panel to our frame, so it can become visible
* Define default exit for panel and title for our frame
*/
frame.add(panel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("DNS Router");
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
new GUI();
}
@Override
public void actionPerformed(ActionEvent e) {
message.setText("");
if(e.getActionCommand().equals("Change DNS")) {
if(dns.DNSpath((String)cmdChoice.getSelectedItem())) {
try {
if(dns.change()) {
panel.remove(server);
panel.remove(cmdChoice);
empty.setText("SUCCESS");
button.setText("Exit");
}
else {
message.setText("ERROR");
}
}catch (IOException E) {
}
}else {
message.setText("ERROR");
}
}
else if(e.getActionCommand().equals("Exit")) {
System.exit(0);
}
}
}