gpt4 book ai didi

java - 我的 Java 应用程序的单选按钮无法正常工作

转载 作者:行者123 更新时间:2023-12-01 23:12:57 25 4
gpt4 key购买 nike

我正在制作一个简单的 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("+");
} ...

关于java - 我的 Java 应用程序的单选按钮无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com