-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherAppUI.java
97 lines (84 loc) · 3.31 KB
/
WeatherAppUI.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
public class WeatherAppUI extends JFrame {
private JTextField cityTextField;
private JButton searchButton;
private JButton showForecastButton;
private JButton showDialogueButton;
private JTextArea resultTextArea;
public WeatherAppUI() {
setTitle("Weather App");
setSize(600, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new BorderLayout());
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new FlowLayout());
cityTextField = new JTextField(20);
searchButton = new JButton("Search");
showForecastButton = new JButton("Show Forecast");
showDialogueButton = new JButton("Show Dialogue");
resultTextArea = new JTextArea(15, 50);
resultTextArea.setEditable(false);
inputPanel.add(new JLabel("Enter City Name:"));
inputPanel.add(cityTextField);
inputPanel.add(searchButton);
inputPanel.add(showForecastButton);
inputPanel.add(showDialogueButton);
JScrollPane scrollPane = new JScrollPane(resultTextArea);
mainPanel.add(inputPanel, BorderLayout.NORTH);
mainPanel.add(scrollPane, BorderLayout.CENTER);
add(mainPanel);
setVisible(true);
searchButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cityName = cityTextField.getText().trim();
if (!cityName.isEmpty()) {
Call backend = new Call();
backend.check_weather_name(cityName);
resultTextArea.setText("");
} else {
JOptionPane.showMessageDialog(WeatherAppUI.this, "Please enter a city name");
}
}
});
showForecastButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cityName = cityTextField.getText().trim();
if (!cityName.isEmpty()) {
Call backend = new Call();
backend.show_forecast(cityName);
resultTextArea.setText("");
} else {
JOptionPane.showMessageDialog(WeatherAppUI.this, "Please enter a city name");
}
}
});
showDialogueButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String cityName = cityTextField.getText().trim();
if (!cityName.isEmpty()) {
Call backend = new Call();
backend.show_dialogue(cityName);
resultTextArea.setText("");
} else {
JOptionPane.showMessageDialog(WeatherAppUI.this, "Please enter a city name");
}
}
});
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new WeatherAppUI();
}
});
}
}