import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class compute extends JFrame {
public static void main(String[] args) {
new compute();
}
public compute() {
JFrame mainFrame = new JFrame("算数我最棒");
conputePanel jp1 = new conputePanel();
JPanel buttonPanel = new JPanel();
// 退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) {
mainFrame.dispose();
}
});
buttonPanel.add(exitButton);
mainFrame.add(jp1, BorderLayout.NORTH);
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
JRadioButton optionA, optionB, optionC, optionD;
int result;
int flag = -1;
// 新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
ButtonGroup option = new ButtonGroup();
optionA = new JRadioButton();
optionB = new JRadioButton();
optionC = new JRadioButton();
optionD = new JRadioButton();
optionA.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if(flag==0)
{
label.setText("答案正确" );
}
else
{
label.setText("答案错误" );
}
}
});
optionB.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if(flag==1)
{
label.setText("答案正确" );
}
else
{
label.setText("答案错误" );
}
}
});
optionC.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if(flag==2)
{
label.setText("答案正确" );
}
else
{
label.setText("答案错误" );
}
}
});
optionD.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if(flag==2)
{
label.setText("答案正确" );
}
else
{
label.setText("答案错误" );
}
}
});
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
add(optionA);
add(optionB);
add(optionC);
add(optionD);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder("单选"));
init();
}
// 取1-100之间的随机值
public void init() {
int a, b, c0 = -1, c1 = -1, c2 = -1, c3 = -1;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
optionA.setText(" A. " + result);
flag = 0;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
optionB.setText(" B. " + result);
flag = 1;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
optionC.setText(" C. " + result);
flag = 2;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
optionD.setText(" D. " + result);
flag = 3;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 设置错误单选项名称
if (flag != 0) {
do {
c0 = (int) (Math.random() * 10) + 1;
} while (c0 == result);
optionA.setText(" A. " + c0);
}
if (flag != 1) {
do {
c1 = (int) (Math.random() * 10) + 1;
} while (c1 == result || c1 == c0);
optionB.setText(" B. " + c1);
}
if (flag != 2) {
do {
c2 = (int) (Math.random() * 10) + 1;
} while (c2 == result || c2 == c0 || c2 == c1);
optionC.setText(" C. " + c2);
}
if (flag != 3) {
do {
c3 = (int) (Math.random() * 10) + 1;
} while (c3 == result || c3 == c0 || c3 == c1 || c3 == c2);
optionD.setText(" D. " + c3);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class compute extends JFrame {
public static void main(String[] args) {
new compute();
}
public compute() {
JFrame mainFrame = new JFrame("算数我最棒"); // 主窗体
conputePanel jp1 = new conputePanel(); // 计算面板
JPanel buttonPanel = new JPanel(); // 按钮面板
// 退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) {
mainFrame.dispose();
}
});
buttonPanel.add(exitButton);
mainFrame.add(jp1, BorderLayout.NORTH);
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
JRadioButton optionA, optionB, optionC, optionD;
int result;
int flag = -1;
static int right = 0;
// 新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
ButtonGroup option = new ButtonGroup();
optionA = new JRadioButton();
optionB = new JRadioButton();
optionC = new JRadioButton();
optionD = new JRadioButton();
JLabel rightLabel = new JLabel(" ");
JLabel numLabel = new JLabel("正确题数:"+right);
optionA.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (flag == 0) {
rightLabel.setText("答案正确");
right++;
new compute();
} else {
rightLabel.setText("答案错误");
}
numLabel.setText("正确题数: " + right);
}
});
optionB.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (flag == 1) {
rightLabel.setText("答案正确");
right++;
new compute();
} else {
rightLabel.setText("答案错误");
}
numLabel.setText("正确题数: " + right);
}
});
optionC.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (flag == 2) {
rightLabel.setText("答案正确");
right++;
new compute();
} else {
rightLabel.setText("答案错误");
}
numLabel.setText("正确题数: " + right);
}
});
optionD.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (flag == 3) {
rightLabel.setText("答案正确");
right++;
new compute();
} else {
rightLabel.setText("答案错误");
}
numLabel.setText("正确题数: " + right);
}
});
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
add(optionA);
add(optionB);
add(optionC);
add(optionD);
add(rightLabel);
add(numLabel);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder("单选"));
init();
}
// 取1-100之间的随机值
public void init() {
int a, b, c0 = -1, c1 = -1, c2 = -1, c3 = -1;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
optionA.setText(" A. " + result);
flag = 0;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
optionB.setText(" B. " + result);
flag = 1;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
optionC.setText(" C. " + result);
flag = 2;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
optionD.setText(" D. " + result);
flag = 3;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 设置错误单选项名称
if (flag != 0) {
do {
c0 = (int) (Math.random() * 10) + 1;
} while (c0 == result);
optionA.setText(" A. " + c0);
}
if (flag != 1) {
do {
c1 = (int) (Math.random() * 10) + 1;
} while (c1 == result || c1 == c0);
optionB.setText(" B. " + c1);
}
if (flag != 2) {
do {
c2 = (int) (Math.random() * 10) + 1;
} while (c2 == result || c2 == c0 || c2 == c1);
optionC.setText(" C. " + c2);
}
if (flag != 3) {
do {
c3 = (int) (Math.random() * 10) + 1;
} while (c3 == result || c3 == c0 || c3 == c1 || c3 == c2);
optionD.setText(" D. " + c3);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class computer extends JFrame {
public static void main(String[] args) {
new computer();
}
public computer() {
JFrame mainFrame = new JFrame("算数我最棒");
conputePanel jp = new conputePanel(); // 计算面板
mainFrame.add(jp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
// 退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) {
mainFrame.dispose();
}
});
buttonPanel.add(exitButton);
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
JRadioButton optionA, optionB, optionC, optionD, optionW;
int result;
int flag = -1;
static float right = 0, all = 0, pen = 0;
public void reprint() {
optionW.setSelected(true);
init();
}
// 新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
ButtonGroup option = new ButtonGroup();
optionA = new JRadioButton();
optionB = new JRadioButton();
optionC = new JRadioButton();
optionD = new JRadioButton();
optionW = new JRadioButton();
JLabel numLabel = new JLabel(" 正确题数:" + (int) right + " 正确率: " + (float) pen);
optionA.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 0) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
optionB.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 1) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
optionC.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 2) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
optionD.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 3) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
option.add(optionW);
add(optionA);
add(optionB);
add(optionC);
add(optionD);
add(optionW);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder("单选"));
init();
}
// 取1-100之间的随机值
public void init() {
int a, b, c0 = -1, c1 = -1, c2 = -1, c3 = -1;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
optionA.setText(" A. " + result);
flag = 0;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
optionB.setText(" B. " + result);
flag = 1;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
optionC.setText(" C. " + result);
flag = 2;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
optionD.setText(" D. " + result);
flag = 3;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 设置错误单选项名称
if (flag != 0) {
do {
c0 = (int) (Math.random() * 10) + 1;
} while (c0 == result);
optionA.setText(" A. " + c0);
}
if (flag != 1) {
do {
c1 = (int) (Math.random() * 10) + 1;
} while (c1 == result || c1 == c0);
optionB.setText(" B. " + c1);
}
if (flag != 2) {
do {
c2 = (int) (Math.random() * 10) + 1;
} while (c2 == result || c2 == c0 || c2 == c1);
optionC.setText(" C. " + c2);
}
if (flag != 3) {
do {
c3 = (int) (Math.random() * 10) + 1;
} while (c3 == result || c3 == c0 || c3 == c1 || c3 == c2);
optionD.setText(" D. " + c3);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class computer extends JFrame {
public static void main(String[] args) {
new computer();
}
public computer() {
JFrame mainFrame = new JFrame("算数我最棒");
conputePanel jp = new conputePanel(); // 计算面板
mainFrame.add(jp, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
// 退出按钮
JButton exitButton = new JButton("退出");
exitButton.addActionListener(new ActionListener() {
// 事件源(注册事件)
@Override
public void actionPerformed(ActionEvent e) {
mainFrame.dispose();
}
});
buttonPanel.add(exitButton);
mainFrame.add(buttonPanel, BorderLayout.SOUTH);
mainFrame.setDefaultCloseOperation(mainFrame.EXIT_ON_CLOSE);
mainFrame.setLocation(500, 250);
mainFrame.setSize(500, 300);
mainFrame.setVisible(true);
}
}
//对计算panel随机计算式的设置
class conputePanel extends JPanel {
JLabel label;
JRadioButton optionA, optionB, optionC, optionD, optionW;
int result;
int flag = -1;
static float right = 0, all = 0, pen = 0;
public void reprint() {
optionW.setSelected(true);
init();
}
// 新建label
public conputePanel() {
super();
label = new JLabel();
add(label);
ButtonGroup option = new ButtonGroup();
optionA = new JRadioButton();
optionB = new JRadioButton();
optionC = new JRadioButton();
optionD = new JRadioButton();
optionW = new JRadioButton();
JLabel numLabel = new JLabel(" 正确题数:" + (int) right + " 正确率: " + (float) pen);
optionA.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 0) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
optionB.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 1) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
optionC.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 2) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
optionD.addItemListener(new ItemListener() { // 给单选按钮添加一个点击监听器
public void itemStateChanged(ItemEvent e) { // 单选按钮被选中
if (e.getStateChange() == ItemEvent.SELECTED) {
if (flag == 3) {
right++;
reprint();
} else {
reprint();
}
numLabel.setText("正确题数: " + right);
}
}
});
option.add(optionA);
option.add(optionB);
option.add(optionC);
option.add(optionD);
option.add(optionW);
add(optionA);
add(optionB);
add(optionC);
add(optionD);
add(numLabel);
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
setBorder(BorderFactory.createTitledBorder("单选"));
init();
}
// 取1-100之间的随机值
public void init() {
int a, b, c0 = -1, c1 = -1, c2 = -1, c3 = -1;
String tm;
do {
a = (int) (Math.random() * 100) + 1;
b = (int) (Math.random() * 100) + 1;
} while (a == b || b == 1);
// 通过随机值随机取符号
switch ((int) (Math.random() * 4)) {
case 0:
tm = " + ";
label.setText("" + a + tm + b + " = ");
result = a + b;
optionA.setText(" A. " + result);
flag = 0;
break;
case 1:
tm = " - ";
label.setText("" + a + tm + b + " = ");
result = a - b;
optionB.setText(" B. " + result);
flag = 1;
break;
case 2:
tm = " × ";
label.setText("" + a + tm + b + " = ");
result = a * b;
optionC.setText(" C. " + result);
flag = 2;
break;
case 3:
tm = " ÷ ";
label.setText("" + a + tm + b + " = ");
result = a / b;
optionD.setText(" D. " + result);
flag = 3;
break;
// 当随机数不为0,1,2,3时的修正语句
default:
JOptionPane.showMessageDialog(null, "出错!");
System.exit(0);
}
// 设置错误单选项名称
if (flag != 0) {
do {
c0 = (int) (Math.random() * 10) + 1;
} while (c0 == result);
optionA.setText(" A. " + c0);
}
if (flag != 1) {
do {
c1 = (int) (Math.random() * 10) + 1;
} while (c1 == result || c1 == c0);
optionB.setText(" B. " + c1);
}
if (flag != 2) {
do {
c2 = (int) (Math.random() * 10) + 1;
} while (c2 == result || c2 == c0 || c2 == c1);
optionC.setText(" C. " + c2);
}
if (flag != 3) {
do {
c3 = (int) (Math.random() * 10) + 1;
} while (c3 == result || c3 == c0 || c3 == c1 || c3 == c2);
optionD.setText(" D. " + c3);
}
// 当结果不为简单运算结果时,重新进行init()取值
if ((result < 0) || (result > 100) || ((a % b) != 0)) {
init();
}
return;
}
}