这个是算有点格式的
  但是还是没有结果
  纳闷的很package packege_one;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class JFrame_nine {
static JFrame frame=new JFrame("算术题测试");
static JPanel pRadio=new JPanel();  //单选按钮面板
static JRadioButton button1=new JRadioButton("做加法");
static JRadioButton button2=new JRadioButton("做减法");
static JRadioButton button3=new JRadioButton("做乘法");
static JRadioButton button4=new JRadioButton("做除法");
static ButtonGroup group=new ButtonGroup();

static JPanel pCheck=new JPanel();  //复选按钮版面
static JCheckBox box1=new JCheckBox("显示已做题数");
static JCheckBox box2=new JCheckBox("显示做对题数");

static JPanel pCalcu=new JPanel();  //运算版面
static JLabel label1=new JLabel("");
static JLabel label2=new JLabel("+");
static JLabel label3=new JLabel("");
static JLabel label4=new JLabel("=");
static JTextField textfield=new JTextField("");
static JButton ok=new JButton("确定");

static JPanel pRecord=new JPanel(); //记录版面
static int totalNumber=0;
static int rightNumber=0;
static JLabel label5=new JLabel("已做"+totalNumber+"题.");
static JLabel label6=new JLabel("做对"+rightNumber+"题.");
static JPanel pCombine=new JPanel();  //组合版面
static JPanel nine=new JPanel();  //顶层版面
int calcu=1;  //运算类型.1:加,2:减,3:乘,4:除.
int total=1;
int right=1;
public JFrame_nine()
{
pRadio.setLayout(new GridLayout(0,1)); //建立单选版面
button1.setSelected(true);
group.add(button1);
group.add(button2);
group.add(button3);
group.add(button4);
pRadio.add(button1);
pRadio.add(button2);
pRadio.add(button3);
pRadio.add(button4);
pRadio.setBorder(BorderFactory.createEmptyBorder(0,30,0,20));

box1.setSelected(true);
box2.setSelected(true);
pCheck.setLayout(new GridLayout(0,1));  //建立复选版面
pCheck.add(box1);
pCheck.add(box2);

pCalcu.setLayout(new FlowLayout());  //建立运算版面
pCalcu.setBorder(BorderFactory.createEmptyBorder(0,0,20,0));
pCalcu.add(label1);
pCalcu.add(label2);
pCalcu.add(label3);
pCalcu.add(label4);
pCalcu.add(textfield);

pRecord.setLayout(new FlowLayout());  //建立运算结果记录版面
pRecord.add(label5);
pRecord.add(label6);
pRecord.setBorder(BorderFactory.createEmptyBorder(20,0,0,0));

//建立组合面板,含有运算面板和运算结果记录面板
pCombine.setLayout(new FlowLayout());
pCombine.add(pCalcu,BorderLayout.NORTH);
pCombine.add(ok,BorderLayout.CENTER);
pCombine.add(pRecord,BorderLayout.SOUTH);

//建立顶层面板
nine.setLayout(new FlowLayout());
nine.add(pCombine);
nine.add(pRadio);
nine.add(pCheck);
nine.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));

ButtonListener mybutton=new ButtonListener();
ok.addActionListener(mybutton);

CheckBoxListener mycheck=new CheckBoxListener();
box1.addItemListener(mycheck);
box2.addItemListener(mycheck);

button1.setActionCommand("+");
button2.setActionCommand("-");
button3.setActionCommand("*");
button4.setActionCommand("/");
RadioListener myradio=new RadioListener();
button1.addActionListener(myradio);
button2.addActionListener(myradio);
button3.addActionListener(myradio);
button4.addActionListener(myradio);
int x,y,temp;
String s=new String();
//产生随机数,作为参与运算的操作数,置于x,y中
//确保x,y不为0,且x>y.
x=(int)(90*Math.random());
while(x==0){x=(int)(90*Math.random());};
y=(int)(90*Math.random());
while(y==0){y=(int)(90*Math.random());};
if(y>x){temp=x;x=y;y=temp;};
label1.setText(s.valueOf(""+x+""));
label3.setText(s.valueOf(""+y+""));

frame.getContentPane().setLayout(new FlowLayout());
frame.getContentPane().add(nine);
frame.pack();
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}

});
}
//命令按钮事件服务类
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int x,y,z,temp;
String s=new String();
z=(int)Integer.valueOf(textfield.getText().trim()).intValue();
x=(int)Integer.valueOf(label1.getText().trim()).intValue();
y=(int)Integer.valueOf(label3.getText().trim()).intValue();

if(calcu==1){if(x+y==z){rightNumber++;};}else
    if(calcu==2){if(x-y==z){rightNumber++;};}else
    if(calcu==3){if(x*y==z){rightNumber++;};}else
    {if(x/y==z){rightNumber++;};}
    
x=(int)(90*Math.random());
while(x==0){x=(int)(90*Math.random());};
y=(int)(90*Math.random());
while(y==0){y=(int)(90*Math.random());};
if(y>x){temp=x;x=y;y=temp;};
label1.setText(s.valueOf(""+x+""));
label3.setText(s.valueOf(""+y+""));

totalNumber++;
label5.setText("已做"+totalNumber+"题.");
label6.setText("做对"+rightNumber+"题");
if(totalNumber>100)System.exit(0);
}
}
class CheckBoxListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Object source=e.getItemSelectable(); ////产生事件的 ItemSelectable 对象。
if(source==box1)
{
total=(total+1)%2;
}else if(source==box2)
{
right=(right+1)%2;
};
if(total==1)
{
label5.setVisible(true);
}
else
{
label5.setVisible(false);
};
if(right==1){label6.setVisible(true);}else{label6.setVisible(false);};
}
}
class RadioListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("+"))
{
calcu=1;
label2.setText("+");
}
else if(e.getActionCommand().equals("-"))
{
calcu=2;
label2.setText("-");
}
else if(e.getActionCommand().equals("*"))
{
calcu=3;
label2.setText("*");
}
else
{
calcu=4;
label2.setText("/");
}
}
}
public static void main(String[] args)
{
JFrame_nine exza=new JFrame_nine();

}}

解决方案 »

  1.   

    我要实现的功能是
      有一组JRadioButton 里面是"1,做加法
                                  2,做减法
                                  3,做乘法
                                  4,做除法
      还有一个JCheckBox   1,显示已做题目数  2,显示做对题目数
      我设置的事件源是 ButtonGroup  JRadioButton里面的  1,2,3,4  和JCheckBox的 1,2 
      还有一个确定,题目是随机产生的
      大概就这样
      你们把它放到编辑器里面就一目了然了
      帮帮忙啊
     谢谢大家
      

  2.   

    z=(int)Integer.valueOf(textfield.getText().trim()).intValue(); 
    try catch一下
    x,y肯定出来数字,就不用try..catch
      

  3.   

    楼主你把static JTextField textfield=new JTextField(""); 改成static JTextField textfield=new JTextField(5)先,这样出来就固定长度了 ;
    还有楼主你要把你的程序出现的问题描述清楚点,我初步试了一下,还不知道你程序有什么问题,我晚上再帮你看看
      

  4.   

    其实你把它放到eclipse中运行一下你就会发现问题
      当我要做加减乘除法的时候  应该显示结果的textfield不出来结果
        我不知道是哪里出了问题
     你帮我看看咯
     谢谢你们了
      

  5.   

    你按我上面说的改一下,就可以看到textfield拉