当我尝试让我的 JDialog 包含正确的信息(通过我的自定义对象 WeatherCard 输入)来显示时,它会在创建后立即消失。我错过了什么还是我只是太用力地捏造它?
public void printWeatherCard(WeatherCard w, JFrame controlFrame) throws MalformedURLException, IOException{
/* Displays a dialog box containing the temperature and location */
// BufferedImage img = ImageIO.read(new URL(w.imgSrc));
// ImageIcon icon = new ImageIcon(img);
// JOptionPane.showMessageDialog(controlFrame, "It is currently " + w.currentTemp + " \u00B0 F in " + w.location.city + ", " + w.location.state + ".\nCurrent humidity: " + w.currentHumidity/* +
//"%.\nChance of precipitation: " + w.chancePrecip + "%."*/, "Weather Update: " + w.location.zipCode, JOptionPane.INFORMATION_MESSAGE, icon);
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(0, 1));
Label lb = new Label("It is currently " + w.currentTemp + " \u00B0 F in " + w.location.city + ", " + w.location.state + ".\nCurrent humidity: " + w.currentHumidity);
panel.add(lb);
final JDialog dialog = new JDialog(controlFrame, "Weather Update: " + w.location.zipCode);
dialog.setSize(500,500);
dialog.add(panel);
dialog.pack();
Timer timer = new Timer(10000, new ActionListener() {
public void actionPerformed(ActionEvent e) {
dialog.setVisible(false);
dialog.dispose();
}
});
timer.setRepeats(false);
timer.start();
dialog.setVisible(true);
}