一直在研究并查看许多答案,但没有运气。
public class GUIinput extends JFrame{
//Num is the JTextfield variable used for later.
public JTextField num;
public JButton button;
public void test(){
//JPanel
JPanel panel = new JPanel();
//Welcome JLabel..Contains the welcome message.
JLabel welcome = new JLabel("Welcome to my Grade Calculator program!");
panel.add(welcome);
//Instructions J Label on how to work the program.
JLabel instructions = new JLabel("Enter your Mark and Weight to find your average grade!");
panel.add(instructions);
//JLabel---How many different assignments do the user want to evaluate?
JLabel number = new JLabel("<html><br>Firstly, please enter how many assignments/exams you want to evaluate?</html>");
panel.add(number);
//JTextField assigner.
num = new JTextField(18);
panel.add(num);
//Enter button
button = new JButton("Enter");
panel.add(button);
//JFrame maker.
setTitle("Grade Calculator");
setSize(575,250);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
add(panel);
//This action listener opens the new class Gradeanalysis(). The next window.
button.addActionListener(new Gradeanalysis());
//Action listener for the Textfield in case the user presses the enter key.
num.addActionListener(new Gradeanalysis());
}
public static void main(String[] args) {
new GUIinput().test();
}
}
此类用于程序的第一个窗口。程序要求用户输入一个发送到另一个类的值。 (等级分析)。
public class Gradeanalysis extends GUIinput implements ActionListener{
@Override
public void actionPerformed (ActionEvent e){
int messageType = JOptionPane.PLAIN_MESSAGE;
/*ERROR IS HERE---*/JOptionPane.showMessageDialog(null,num.getText(),"fasfa",messageType);
JFrame frame = new JFrame("Grade Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel());
frame.pack();
frame.setVisible(true);
}
现在我剪掉了很多不需要的代码。所以程序应该做的是从第一个窗口获取用户输入,当用户单击enter按钮时,输入应该显示在MessageDialog屏幕上。不是新的JFrame。
现在我明白问题是因为星号的那一行通常是&#34; num.getText()&#34;部分。
如果我添加&#34; num = new JTextField();&#34;在该行之上,MessageDialog屏幕将为空白。我希望它显示上一屏幕的输入。
不知道如何解决这个问题。感谢所有帮助,谢谢。
答案 0 :(得分:3)
你的问题在于:
//This action listener opens the new class Gradeanalysis(). The next window.
button.addActionListener(new Gradeanalysis());
//Action listener for the Textfield in case the user presses the enter key.
num.addActionListener(new Gradeanalysis());
您添加了一个新的Gradeanalysis,它没有指向文本字段的指针,也没有初始化它们的字段。你应该改变你的动作监听器,使它在一个单独的类中,并传入它影响的字段
ActionListener listener = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int messageType = JOptionPane.PLAIN_MESSAGE;
JOptionPane.showMessageDialog(null, num.getText(), "fasfa", messageType);
JFrame frame = new JFrame("Grade Calculator");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JPanel());//dont have create main panel method
frame.pack();
frame.setVisible(true);
}
};
button.addActionListener(listener);
num.addActionListener(listener);
答案 1 :(得分:1)
从给定的代码中,不清楚Gradeanalysis
是否可以看到您的num
。您可以为此类创建一个新的构造函数,以便num
作为参数输入。像这样重做你的Gradeanalysis
课程
public class Gradeanalysis extends GUIinput implements ActionListener{
JTextField num;
public Gradeanalysis(JTextField num)
{
this.num = num;
}
//Implement the actionPerformed here as u have..
}
然后从代码中调用Gradeanalysis
,例如
button.addActionListener(new Gradeanalysis(num));
现在我要注意,执行的操作可能不像TextField所期望的那样有效,而swing application should only have one JFrame可以使其他人使用JDialogs等。