java - 使用函数将文本设置为 Jlabel

标签 java swing jbutton actionlistener jlabel

我正在尝试使用函数将settext设置为JLabel

由于某种原因,checkResults 函数无法正常工作,但我仍然收到错误消息。我有两个类

驱动程序数学问题

驱动程序类别

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Scanner;

import javax.swing.*;

public class Driver extends MathProblems {

    MathProblems answers = new MathProblems();
    MathProblems problems = new MathProblems();
    private static final Scanner in = new Scanner(System.in);

    String s = "Welcome Students!";
    String b = "Start!";
    private JFrame f;
    private JPanel p;

    JFrame frame = new JFrame();

    JButton b1 = new JButton(b);

    JLabel jl = new JLabel(s);

    int i;

    public Driver () {      
        gui();  
    }

    public void gui() { 
        f = new JFrame("Flash Card Program");       
        p = new JPanel();   
        f.setLayout( new GridLayout( 2, 1 ) );
        f.add(jl);
        f.add(p);
        p.setLayout( new GridLayout( 2, 1 ) );
        p.add(b1);

        jl.setHorizontalAlignment(JLabel.CENTER);

        // pack the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(560,400); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if(b1.getText().equals("Click For Answer"))
                {
                    String s = in.nextLine();
                    int expected = Integer.parseInt(s);
                    answers.run();
                    jl.setText.(answers.checkResult());
                    String b = "Next Question";
                    b1.setText(b);
                }
                else
                {
                    problems.run();
                    jl.setText(problems.getQuestion());
                    String b = "Click For Answer";
                    b1.setText(b);

                }
          }
});
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                new Driver();
           }
        });
    } // End main Method

} // End class Driver

数学问题类

import java.util.Random;

public class MathProblems {
     private static final int MAX_NUMBER = 10;
     private static final Random random = new Random();

     private int expected = 0;
     private String question = "";

     public void run() {
         final int a = random.nextInt(MAX_NUMBER);
         final int b = random.nextInt(MAX_NUMBER);

         final int type = random.nextInt(4);

         switch (type) {
             case 0: 
                 add(a, b);
                 break;
             case 1: 
                subtract(a, b);
                break;
             case 2:
                multiply(a, b);
                break;
             case 3:
                 divide(a, b);
                 break;
         }
     }

     private void add(final int a, final int b) {
         expected = a + b;

         askQuestion(a + " + " + b + " = ");
     }

     private void subtract(final int a, final int b) {
         expected = a - b;

         askQuestion(a + " - " + b + " = ");
     }

     private void multiply(final int a, final int b) {
         expected = a * b;

         askQuestion(a + " * " + b + " = ");
     }

     private void divide(final int a, final int b) {
         expected = a / b;

         askQuestion(a + " / " + b + " = ");
     }

     private  void askQuestion(final String question) {
         this.question = question;
     }  

     public String getQuestion() {
         return question;
     }

     public String checkResult(final int answer) {
         return Integer.toString(expected);
     }

}

最佳答案

正如我对您之前的问题的评论: 您需要从监听器的第一个条件中删除“problems.run()”,否则您得到的“答案”将用于下一个问题,依此类推。另外,考虑在您的divide()方法中使用 double ,通过使用整数,您将丢失小数。

关于java - 使用函数将文本设置为 Jlabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23123201/

相关文章:

java - 有没有办法在给定范围内生成唯一的变量名?

java - 将子类对象添加到父类(super class)列表中

java - 实现一个轻量级的java webbrowser

java - 合并随机数游戏中按钮的 Action 监听器

java - 按钮似乎被视频覆盖

java - 我如何强制 Idea 和 Maven 下载我项目的所有源代码?

java - java中mutator方法的最佳实践

Java 仅在后台启动进程

java - 我收到 .getDateEditor 错误

java - 如何根据按钮操作在框架内切换面板?