有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从JTextArea获取文本,并将其调用到ActionPerformed方法

好的。所以我要开始说,这都是在一节课上

现在,我在游戏页面上显示了一系列按钮,当按下这些按钮时,我可以打开多个不同的画面。所以我所有的按钮都在工作,所有的框架都打开得很好。现在,在那些可以打开的框架内,我有一个JTextArea。在JText区域内,我允许人们输入他们想要的内容,然后他们按下一个按钮,从理论上讲,它基本上会被发送回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();
}

我想让它做的是,在按下MATH100框架上的按钮后,JTextArea将该区域中的文本发送到if语句,然后检查它们输入的内容是否正确。。。但我无法将文本发送到该if声明


共 (1) 个答案

  1. # 1 楼答案

    JTextField从方法中取出,以便其他方法可以看到它

    例如:

    public class ho{
    
     (private or public) JTextArea answerArea;  
    
      public ho{.....}
    
    
      static method something(){
    
      ....}
    
      method actionListener(){ 
      ...}
    

    让我知道这是否对你有效