-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGUI.java
More file actions
78 lines (63 loc) · 2.46 KB
/
GUI.java
File metadata and controls
78 lines (63 loc) · 2.46 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
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class GUI extends JFrame implements ActionListener {
private static JFrame frame;
private static JTextField cityText;
private static JLabel directionLabel;
private static JLabel optionLabel;
private static JLabel cityTemperature;
private static JLabel cityCondition;
private static JLabel cityHumidity;
private static JLabel cityWindSpeed;
private static JButton displayButton;
GUI () {
}
public static void main(String[] args) {
frame = new JFrame("Weather App");
cityText = new JTextField(12);
directionLabel = new JLabel("Enter a city");
optionLabel = new JLabel("Weather Info: ");
cityTemperature = new JLabel("");
cityCondition = new JLabel("");
cityHumidity= new JLabel("");
cityWindSpeed = new JLabel("");
displayButton = new JButton("Submit");
GUI graphicInterface = new GUI();
displayButton.addActionListener(graphicInterface);
JPanel panel = new JPanel();
panel.add(cityText);
panel.add(directionLabel);
panel.add(displayButton);
panel.add(optionLabel);
panel.add(cityTemperature);
panel.add(cityCondition);
panel.add(cityHumidity);
panel.add(cityWindSpeed);
frame.add(panel);
frame.setSize(250, 250);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String city = e.getActionCommand();
if (city.equals("Submit")) {
try {
String[] weatherInfo = new String[4];
if (weatherInfo[0] == null) {
cityTemperature.setText("That city does not exist");
}
for (int i = 0; i < 4; i++) {
weatherInfo[i] = GetWeather.getWeather(cityText.getText())[i];
}
optionLabel.setText(cityText.getText() + "'s Weather Info: ");
cityTemperature.setText("It is " + weatherInfo[0]);
cityCondition.setText("The condition is " + weatherInfo[1]);
cityHumidity.setText("The humidity is " + weatherInfo[2] + "%");
cityWindSpeed.setText("The wind speed is " + weatherInfo[3] + "mph");
} catch (Exception exception) {
exception.printStackTrace();
}
cityText.setText("");
}
}
}