所以我一直在玩GUI并为我的用户创建了一个相对不错的欢迎框架 所述框架有两个文本选项,理论上每个文本选项应打开一个新窗口。
其中一个文本框架完全符合它的设想,根据需要打开一个新窗口,但另一个文本框架没有。
以下是我的代码:
package gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Frame1Welcome extends JFrame {
private JLabel intro1 = new JLabel(
"Welcome to Twitter Farmer.");
private JLabel intro2 = new JLabel(
"In order to start farming up those tweets, first decide if you want to farm for a specific amount of time, or farm a certain amount of tweets.");
private JLabel intro3 = new JLabel(
"By specifying the amount of time you wish to farm for, Twitter Farmer will gather as many tweets as it can in said time.");
private JLabel intro4 = new JLabel(
"If you know how many you want however, Twitter Farmer will instead gather up the exact desired amount.");
private JLabel intro5 = new JLabel(
"Please enter the time in seconds, or the amount of tweets as whole number, and press the 'ENTER'-key.");
private JLabel tweets = new JLabel("Tweets:");
private JLabel time = new JLabel("Time:");
private JTextField twitterText = new JTextField(10);
private JTextField timeText = new JTextField(10);
private String inputText; // REMOVE ME
public Frame1Welcome(String title) {
super(title);
launchGUI.setMyDefaults(this, 800, 300);
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(0, 0, 10, 0);
JPanel northernPanel = new JPanel(new GridBagLayout());
this.add(northernPanel, BorderLayout.NORTH);
gbc.gridy = 0;
northernPanel.add(intro1, gbc);
gbc.gridy = 1;
northernPanel.add(intro2, gbc);
gbc.gridy = 2;
northernPanel.add(intro3, gbc);
gbc.gridy = 3;
northernPanel.add(intro4, gbc);
gbc.gridy = 4;
northernPanel.add(intro5, gbc);
JPanel southernPanel = new JPanel(new GridBagLayout());
this.add(southernPanel, BorderLayout.CENTER);
gbc.gridx = 0;
southernPanel.add(tweets, gbc);
gbc.gridx = 1;
southernPanel.add(twitterText, gbc);
gbc.gridy = 1;
gbc.gridx = 0;
southernPanel.add(time, gbc);
gbc.gridx = 1;
southernPanel.add(timeText, gbc);
twitterText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText = twitterText.getText(); // REMOVE ME
dispose();
System.out.println("TWEETS:" + inputText); // REMOVE ME
try {
new Frame3Tweets("Twitter Farmer - Tweet-farm by desired amount", Integer.parseInt(timeText.getText()));
} catch (NumberFormatException error) {
//ADD INVALID NUMBER WINDOW
}
}
});
timeText.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
inputText = timeText.getText(); // REMOVE ME
dispose();
System.out.println("TIME:" + inputText); // REMOVE ME
try {
new Frame2Time("Twitter Farmer - Tweet-farm over desired time", Integer.parseInt(timeText.getText()));
} catch (NumberFormatException error) {
//ADD INVALID NUMBER WINDOW
}
}
});
}
}
首先,所有评论只是提醒我稍后在代码中要做的事情。
其次,假设launchGUI.setMyDefaults(this, 800, 300);
只是我设置大小和可见性的方法,而且它适用于其他窗口。
第三,假设new Frame2Time
和new Frame3Tweets
都是有效的。
我的问题是:
为什么timeText.addActionListener
会创建一个新窗口,而twitterText.addActionListener
却没有?
在用户发出号码后,我已尝试让twitterText.addActionListener
制作其他窗口,而不会弹出任何此类窗口。
有人能告诉我我做错了什么,最好是如何解决它?
答案 0 :(得分:1)
通过采纳Maroun Maroun的建议,并添加error.printStackTrace()
我能够扣除我的问题:
try {
new Frame3Tweets("Twitter Farmer - Tweet-farm by desired amount", Integer.parseInt(timeText.getText()));
} catch (NumberFormatException error) {
error.printStackTrace();
}
该方法将timeText.getText()
作为参数,该参数为空,因此强制所有内容崩溃并烧毁。
应该使用tweetText.getText()
代替,它有一个值。
所以孩子们,请遵循Maroun Maroun的建议,不要将catch
块留空!