【发布时间】:2015-03-17 23:41:36
【问题描述】:
好的。所以我会开始说这是一堂课。
现在,我在 GamePage 上显示了一堆按钮,允许我在按下这些按钮时打开多个不同的帧。所以。我的按钮都在工作,所有的框架都打开得很好。所以现在,在那些可以打开的框架内,我有一个 JTextArea。在这个 JTextArea 里面,我允许人们输入他们想要的内容,然后他们按下一个按钮,理论上它基本上会被发送回 actionPerformed 方法。
但是...我无法让它发挥作用。这是您需要的最低要求。
动作执行方法
public void actionPerformed(ActionEvent event)
{
if(event.getActionCommand().equals("M 100"))
{
math100();
}
if(event.getActionCommand().equals("Answer M 100"))
{
//THIS IS WHERE IF THEY PUSH THE BUTTON ON THE FRAME,
//IT WILL SEND THE JTEXTAREA TEXT BACK TO HERE. THEN IT WILL DISPLAY
//IF THE ANSWER IS CORRECT OR NOT. BUT I CANT GET THE TEXT TO BE
//SENT BACK HERE....
}
问题的方法 这假设将用户输入的 JTextArea 单词发送回 ActionPerformed 方法中的特定 if 语句。
public static void math100()
{
JFrame m100Frame = new JFrame("100 Point Math Question");
m100Frame.setSize(350,350);
m100Frame.setLocationRelativeTo(null);
JPanel pane = new JPanel();
m100Frame.setContentPane(pane);
JLabel question = new JLabel("<html><p><div WIDTH = 320><center>Round 1,291,293 to the nearest thousands, and round 8.472 to the nearest hundredth.</p><p>Put answers in box below, and have the word 'and' between the two answers.</center></width></div></html>");
JTextArea answerArea = new JTextArea("",10,25);
JButton answerButton = new JButton("Answer M 100");
answerButton.addActionListener(new GamePage());
pane.add(question);
pane.add(answerArea);
pane.add(answerButton);
m100Frame.setVisible(true);
m100Frame.toFront();
}
我想要它做的是让 JTextArea 在按下 MATH100 帧上的按钮后将该区域中的文本发送到 if 语句,然后检查他们输入的内容是对还是错.. .但我无法将文本发送到该 if 语句。
【问题讨论】:
-
我们真的需要所有这些代码来进一步帮助您吗?将其分解为一个最小的、可编译的示例来演示问题
-
请创建MCVE 帮助我们了解您的代码
-
是的,我把代码删减了一点。您真正需要的只是 ActionPerformed 和 math100() 方法,之后,我应该能够编写其余方法。它仍然会产生同样的问题,其中 is 不会将 JTextArea 中的文本发送到 if 语句。我如何对其进行编码才能做到这一点?
-
你需要某种对
JTextArea的引用,然后你可以简单地调用它的getText方法。更改GamePage以将JTextArea作为其构造函数中的参数之一