上述天气预报代码中第二个界面对应的java代码
时间: 2023-06-14 20:07:31 浏览: 55
以下是一个简单的天气预报应用程序的第二个界面的 Java 代码示例:
```java
import javax.swing.*;
import java.awt.*;
public class WeatherForecast2 extends JFrame {
public WeatherForecast2(String city, String temperature, String description) {
// 设置窗口标题
setTitle("天气预报");
// 设置窗口尺寸
setSize(400, 300);
// 创建面板
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
// 创建标签
JLabel cityLabel = new JLabel("城市:" + city);
JLabel temperatureLabel = new JLabel("温度:" + temperature);
JLabel descriptionLabel = new JLabel("天气:" + description);
// 设置字体
Font font = new Font("微软雅黑", Font.PLAIN, 20);
cityLabel.setFont(font);
temperatureLabel.setFont(font);
descriptionLabel.setFont(font);
// 添加标签到面板
panel.add(cityLabel);
panel.add(temperatureLabel);
panel.add(descriptionLabel);
// 添加面板到窗口
add(panel);
// 设置窗口可见
setVisible(true);
}
public static void main(String[] args) {
// 创建窗口
new WeatherForecast2("北京", "25℃", "多云");
}
}
```
这个代码创建了一个 JFrame 窗口,其中包含了一个面板和三个标签,用于显示城市、温度和天气描述。在 main 方法中创建了一个 WeatherForecast2 对象,向其传递了城市、温度和天气描述。在构造函数中,创建了面板和标签,并将它们添加到面板和窗口中。最后,设置窗口可见。