Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Java应用程序的单选按钮工作不正常_Java_Swing_Radio Button - Fatal编程技术网

我的Java应用程序的单选按钮工作不正常

我的Java应用程序的单选按钮工作不正常,java,swing,radio-button,Java,Swing,Radio Button,我正在制作一个简单的GUI应用程序,为用户提供随机的数学问题。当我点击单选按钮更改操作员时,它应该更改标签。它不笑 我已经把评论放在我所指的地方 感谢您的帮助 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MathPractice extends JFrame { Container c; JPanel output; JLabel title, num1

我正在制作一个简单的GUI应用程序,为用户提供随机的数学问题。当我点击单选按钮更改操作员时,它应该更改标签。它不笑

我已经把评论放在我所指的地方

感谢您的帮助

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;



public class MathPractice extends JFrame {
    Container c;
    JPanel output;
    JLabel title, num1, num2, operator, equals, mark, display, correct, wrong ,correctN, wrongN;
    JTextField answer;
    JButton newB, ansB;
    JTextArea txtdisplay;
    JRadioButton optAdd, optSub, optMul, optDiv;

    int number1 = 2;int number2 = 2;int rightNum = 4;int wrongNum = 2;
    String displayText = "Keep up the good work!";
    String operatorField;

    Font f = new Font("Arial", Font.BOLD, 20);
    Font numbers = new Font("Arial", Font.PLAIN, 30);
    Font operators = new Font("Arial", Font.PLAIN, 20);
    Font outputF = new Font("Arial", Font.ITALIC, 15);
    Font fAnswers = new Font("Arial", Font.BOLD, 30);

    //These are the if statements to change the operators.
    class RadioHandler implements ActionListener {  
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == optAdd){
                operatorField = "+";
            }
            else if (e.getSource() == optSub){
                operatorField = "-";
            }
            else if (e.getSource() == optMul){
                operatorField = "X";
            }
            else if (e.getSource() == optDiv){
                operatorField = "/";
            }
        }
        }

    MathPractice (){
        super("Math Practice");
        c = getContentPane();
        c.setLayout(null);
        //c.setBackground(Color.blue);

        title = new JLabel("How Much is: ");
        title.setSize(150,20);
        title.setLocation(130, 5);
        title.setFont(f);
        title.setForeground(Color.red);

        num1 = new JLabel(""+ number1);
        num1.setSize(40, 40);
        num1.setLocation(30, 35);
        num1.setFont(numbers);

        operator = new JLabel(operatorField);
        operator.setSize(40, 40);
        operator.setLocation(85, 37);
        operator.setFont(operators);

        num2 = new JLabel(""+ number2);
        num2.setSize(40, 40);
        num2.setLocation(140, 35);
        num2.setFont(numbers);      

        equals = new JLabel(" = ");
        equals.setSize(40, 40);
        equals.setLocation(200, 35);
        equals.setFont(numbers);

        answer = new JTextField(8);
        answer.setSize(80, 40);
        answer.setLocation(250, 35);
        answer.setFont(numbers);        

        mark = new JLabel("?");
        mark.setSize(40, 40);
        mark.setLocation(350, 35);
        mark.setFont(numbers);

        display = new JLabel("" + displayText);
        display.setSize(350, 40);
        display.setLocation(30, 90);
        display.setFont(numbers);
        //display.setVisible(false);

        optAdd = new JRadioButton("Add");
        optAdd.setSize(50, 20);
        optAdd.setLocation(40, 140);
        optAdd.setSelected(true);


        optSub = new JRadioButton("Subtract");
        optSub.setSize(75, 20);
        optSub.setLocation(100, 140);

        optMul = new JRadioButton("Multiply");
        optMul.setSize(75, 20);
        optMul.setLocation(190, 140);

        optDiv = new JRadioButton("Divide");
        optDiv.setSize(75, 20);
        optDiv.setLocation(270, 140);

        ButtonGroup bg = new ButtonGroup();
        bg.add(optAdd);
        bg.add(optSub);
        bg.add(optMul);
        bg.add(optDiv);

            //these are the actionHandlers
        RadioHandler rh = new RadioHandler();
        optAdd.addActionListener(rh);
        optSub.addActionListener(rh);
        optMul.addActionListener(rh);
        optDiv.addActionListener(rh);

        newB = new JButton("    New Excercise    ");
        newB.setSize(170, 40);
        newB.setLocation(15, 170);

        ansB = new JButton("    Answer    ");
        ansB.setSize(170, 40);
        ansB.setLocation(195,170);  

        output = new JPanel();
        output.setLayout(null);
        output.setSize(375, 225);
        output.setBorder(BorderFactory.createTitledBorder("Statistic: "));
        output.setLocation(5, 225);

        correct = new JLabel("Correct Answers: " );
        correct.setSize(150, 25);
        correct.setLocation(15, 25);
        correct.setFont(outputF);

        wrong = new JLabel("Wrong answers: ");
        wrong.setSize(150, 25);
        wrong.setLocation(230, 25);
        wrong.setFont(outputF);

        correctN = new JLabel("" + rightNum);
        correctN.setSize(100, 100);
        correctN.setLocation(60, 50);
        correctN.setFont(fAnswers);
        correctN.setForeground(Color.green);

        wrongN = new JLabel("" + wrongNum);
        wrongN.setSize(100, 100);
        wrongN.setLocation(275, 50);
        wrongN.setFont(fAnswers);
        wrongN.setForeground(Color.red);





        c.add(title);c.add(num1);c.add(operator);c.add(num2);c.add(equals);c.add(answer);c.add(mark);c.add(display);c.add(newB);
        c.add(ansB);c.add(optAdd);c.add(optSub);c.add(optMul);c.add(optDiv);c.add(output);

        output.add(correct);output.add(wrong);output.add(correctN);output.add(wrongN);  
    }

    public static void main(String[] args) {
        MathPractice app = new MathPractice();
        app.setSize(400, 500);
        app.setVisible(true);
        app.setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }

}

确定
operatorField
后,需要调用
JLabel\setText

operator.setText(operatorField);
更好的是,为什么不直接设置文本呢

if (e.getSource() == optAdd) {
   operator.setText("+");
} ...

确定
operatorField
后,需要调用
JLabel\setText

operator.setText(operatorField);
更好的是,为什么不直接设置文本呢

if (e.getSource() == optAdd) {
   operator.setText("+");
} ...

确定
operatorField
后,需要调用
JLabel\setText

operator.setText(operatorField);
更好的是,为什么不直接设置文本呢

if (e.getSource() == optAdd) {
   operator.setText("+");
} ...

确定
operatorField
后,需要调用
JLabel\setText

operator.setText(operatorField);
更好的是,为什么不直接设置文本呢

if (e.getSource() == optAdd) {
   operator.setText("+");
} ...