使用函数将setText添加到Jlabel [英] setText to Jlabel with function

查看:90
本文介绍了使用函数将setText添加到Jlabel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用函数 settext JLabel

I am trying to settext to JLabel with a function.

checkResults函数因某些原因无法正常工作我仍然会收到错误。我有2个班级

The checkResults function is not working for some reason I still get errors. I have 2 classes

司机 MathProblems

驱动程序类

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

MathProblems Class

MathProblems Class

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()方法中使用双精度数,通过使用整数,你将丢失小数。

As I commented on your previous question: You need to remove "problems.run()" from your first condition in the listener, otherwise the "answer" you get will be for the next question, and so on. Also, consider using doubles in your divide() method, by using ints you will lose the decimals.

这篇关于使用函数将setText添加到Jlabel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆