每次我从jtextfield窗口检查用户输入时,仅当我移动整个图形窗口时,结果才打印在我的图形窗口上

时间:2019-05-16 05:25:44

标签: java swing graphics jframe jpanel

所以我正在制作危险游戏,并且有一个jtextfield用户可以输入答案。当他们单击提交按钮时,答案将存储在变量中。该变量将被放入制作危险公告板的类中,并用答案检查用户的输入。但是为了使正确或不正确的图形出现在我的图形窗口中,我必须最小化图形窗口,并通过一些方法来创建另一个jtextfield窗口,然后仅输出正确或不正确的图形。

 import java.awt.event.ActionEvent;
 import java.awt.event.ActionListener;
 import java.util.Scanner;

 import javax.swing.JButton;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.JTextField;

public class Answer extends JFrame implements ActionListener {

    static Scanner sc = new Scanner(System.in);
    static String s;
    static JTextField textfield;
    static JButton jButton;


     public  void findingAnswer() {

          JFrame jf = new JFrame();
          textfield = new JTextField("", 30);
          jButton = new JButton();
          jButton = new JButton("Click");
          jButton.addActionListener(new Answer());

          JPanel panel = new JPanel();
          JLabel jl = new JLabel("Put your answer here");
          jf.setSize(500, 500);
          jf.setVisible(true);
          jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          jf.add(panel);
          panel.add(jl);
          panel.add(textfield);
          panel.add(jButton);

     }



       @Override
       public void actionPerformed(ActionEvent arg0) {
         s = textfield.getText();

      }

    public String get_s()
     {
         return s;
     }
}

//在制作危险板的班级中,有以下代码:

 // answer box
 // calling the previous class to make the window with jtextfield
              an.findingAnswer();
// checks to see if the answer correct
// x is an object from a class where you can get the question and answer
 if (an.get_s().equals(x.getAnswer()))
                 {
                 g.drawString("Correct! +" + y + " points!", 300, 300);
                 if(turns % 2 != 0)
                  {
                      player1 += y;
                      turns ++;
                   }
                  else
                {
                     player2 += y;
                      turns ++;
                   }
            }   
              else
               {
                   g.drawString("Incorrect.\nThe correct answer is: "+   x.getAnswer(), 300, 300);
                 turns ++;
             }
             }

预期结果是获得错误且正确的语句,而不必创建更多窗口或移动屏幕。

0 个答案:

没有答案