用JAVA作图形界面超级麻烦...
/**
* @(#)AddOrMulti.java
*
* AddOrMulti application
*
* @author
* @version 1.00 2009/12/11
*/
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
import javax.swing.event.*;
public class AddOrMulti extends JFrame implements ActionListener {
/**
* 显示第一个随机数
*/
private JTextField txt1 = new JTextField(5);
/**
* 显示第二个随机数
*/
private JTextField txt2 = new JTextField(5);
/**
* 显示两个随机数的"+/×"的结果
*/
private JTextField txt3 = new JTextField(5);
/**
* 显示运算符号
*/
private JLabel lb1 = new JLabel();
private JLabel lb2 = new JLabel("=");
/**
* 单选框组
*/
private JRadioButton rb1 = new JRadioButton();
private JRadioButton rb2 = new JRadioButton();
private ButtonGroup bg = new ButtonGroup();
JPanel jp = new JPanel();
/**
* 判断按钮
*/
private JButton btn1 = new JButton("判断");
/**
* 出题按钮
*/
private JButton btn2 = new JButton("出题");
public AddOrMulti() {
Container c = this.getContentPane();
c.setLayout(new BorderLayout(10, 30));
// northPanel
JPanel northPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 0));
northPanel.add(txt1);
northPanel.add(lb1);
northPanel.add(txt2);
northPanel.add(lb2);
northPanel.add(txt3);
c.add(northPanel, BorderLayout.NORTH);
// centerPanel
JPanel centerPanel = new JPanel(
new FlowLayout(FlowLayout.CENTER, 20, 0));
jp.add(rb1 = new JRadioButton("+", true));
jp.add(rb2 = new JRadioButton("×"));
bg.add(rb1);
bg.add(rb2);
centerPanel.add(jp);
c.add(centerPanel, BorderLayout.CENTER);
this.setSize(500, 400);
this.setVisible(true);
// southPanel
JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 0));
southPanel.add(btn1);
southPanel.add(btn2);
c.add(southPanel, BorderLayout.SOUTH);
btn1.addActionListener(this);
btn2.addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
int[] random = new int[2];
/**
* 根据单选框,产生随机数
*/
if (rb1.isSelected()) {
// 如果是“+”,生成0-100的随机数。
lb1.setText(rb1.getText());
for (int i = 0; i < 2; i++) {
random[i] = (int) (Math.random() * 100);
}
} else {
// 如果是“×”,生成0-20的随机数。
lb1.setText(rb2.getText());
for (int i = 0; i < 2; i++) {
random[i] = (int) (Math.random() * 20);
}
}
if (event.getSource().equals(btn2)) { // click button2
txt1.setText(String.valueOf(random[0]));
txt2.setText(String.valueOf(random[1]));
}
if (event.getSource().equals(btn1)) { // click button1
// 获取第一个文本域的值
String a = txt1.getText();
// 获取第二个文本域的值
String b = txt2.getText();
// 获取操作符
String op = lb1.getText();
int rs = 0;
if ("+".equals(op)) {
rs = Integer.parseInt(a) + Integer.parseInt(b);
} else {
rs = Integer.parseInt(a) * Integer.parseInt(b);
}
txt3.setText(Integer.toString(rs));
}
}
public static void main(String[] args) {
new AddOrMulti();
}
}