一气之下。把监听器和启动类作到一个文件里了。
jsqframe.java
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
class jsqframe extends JFrame{int startvar=0;
int midvar=0;
int endvar=0;
int methodvar=1;JButton btn1=new JButton("1");
JButton btn2=new JButton("2");
JButton btn3=new JButton("3");
JButton btn4=new JButton("4");
JButton btn5=new JButton("5");
JButton btn6=new JButton("6");
JButton btn7=new JButton("7");
JButton btn8=new JButton("8");
JButton btn9=new JButton("9");
JButton btn10=new JButton("0");
JButton btn11=new JButton("加");
JButton btn12=new JButton("减");
JButton btn13=new JButton("乘");
JButton btn14=new JButton("除");
JButton btn15=new JButton("清空");
JButton btn16=new JButton("结果");JTextField txt1=new JTextField();
JRadioButton radYes=new JRadioButton("开始",false);
JRadioButton radNo=new JRadioButton("结束",true);ButtonGroup radioGroup1=new ButtonGroup();
public jsqframe()
{
Container c=getContentPane();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);            
c.setLayout(null);
c.add(btn1);
c.add(btn2);
c.add(btn3);
c.add(btn4);
c.add(btn5);
c.add(btn6);
c.add(btn7);
c.add(btn8);
c.add(btn9);
c.add(btn10);
c.add(btn11);
c.add(btn12);
c.add(btn13);
c.add(btn14);
c.add(btn15);
c.add(btn16);c.add(txt1);
c.add(radYes);
c.add(radNo);btn1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"1");
}});
btn2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"2");
}});
btn3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"3");
}});
btn4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"4");
}});
btn5.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"5");
}});
btn6.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"6");
}});
btn7.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"7");
}});
btn8.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"8");
}});
btn9.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"9");
}});
btn10.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText(txt1.getText()+"0");
}});
btn11.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
startvar=Integer.parseInt(txt1.getText());
txt1.setText("");
methodvar=1;
}});
btn12.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
startvar=Integer.parseInt(txt1.getText());
txt1.setText("");
methodvar=2;
}});
btn13.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
startvar=Integer.parseInt(txt1.getText());
txt1.setText("");
methodvar=3;
}});
btn14.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
startvar=Integer.parseInt(txt1.getText());
txt1.setText("");
methodvar=4;
}});
btn15.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setText("");
}});
btn16.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
midvar=Integer.parseInt(txt1.getText());txt1.setText("");
switch(methodvar){
case 1:
endvar=startvar+midvar;break;
case 2:
endvar=startvar-midvar;break;
case 3:
endvar=startvar*midvar;break;
case 4:
if(midvar==0)
JOptionPane.showMessageDialog(null,"你不能将0做被除数");
else
endvar=startvar/midvar;
break;
default:}txt1.setText(""+endvar);
}});radioGroup1.add(radYes);
radioGroup1.add(radNo);radYes.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setEnabled(true);
}
});
radNo.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
txt1.setEnabled(false);
}
});txt1.setBounds(30,20,140,20);btn1.setBounds(30,50,20,20);
btn2.setBounds(60,50,20,20);
btn3.setBounds(90,50,20,20);
btn4.setBounds(30,80,20,20);
btn5.setBounds(60,80,20,20);
btn6.setBounds(90,80,20,20);
btn7.setBounds(30,110,20,20);
btn8.setBounds(60,110,20,20);
btn9.setBounds(90,110,20,20);
btn10.setBounds(30,140,20,20);
btn11.setBounds(60,140,20,20);
btn12.setBounds(90,140,20,20);
btn13.setBounds(120,140,20,20);
btn14.setBounds(150,140,20,20);
btn15.setBounds(120,80,50,20);
btn16.setBounds(120,110,50,20);radYes.setBounds(120,50,20,20);
radNo.setBounds(150,50,20,20);
radYes.setToolTipText("打开计算器");
radNo.setToolTipText("关闭计算器");
btn1.setToolTipText("1");
btn2.setToolTipText("2");
btn3.setToolTipText("3");
btn4.setToolTipText("4");
btn5.setToolTipText("5");
btn6.setToolTipText("6");
btn7.setToolTipText("7");
btn8.setToolTipText("8");
btn9.setToolTipText("9");
btn10.setToolTipText("0");
btn11.setToolTipText("加");
btn12.setToolTipText("减");
btn13.setToolTipText("乘");
btn14.setToolTipText("除");
btn15.setToolTipText("清空");
btn16.setToolTipText("结果");txt1.setToolTipText("这里显示计算结果");
setSize(210,210);
setTitle("HONGHAIER的第一个JAVA计算器");
setVisible(true);
setResizable(false);
}public static void main(String[] args){
jsqframe jsq=new jsqframe();
jsq.setVisible(true);
}
}
编译通过。运行正常。
注:
请大家继续回答我的问题。
我还是不明白为什么那两个文件运行的监听器不管用。???????????????

解决方案 »

  1.   

    对了,忘了说:
    或许是由于按纽太小,所以上面的字没有显示出来。有没有办法解决。
    ??
      

  2.   

    难度不是太大吧!需要我加分吗?
      

  3.   

    我也刚做了个计算器,我不知能不能帮到你,你不要取来哪个按扭的数字就清空,设置一个标志位就可以连续的调用了,对于文件的监听我觉得没有必要,在你按扭的监听中就可以完成文件的取与修改的。要实现你的算法,完全可以的。
      

  4.   

    按钮字太小,怎么办?还没人回答叱
      

  5.   

    你的问题太简单!自己翻书!
      

  6.   

    你要怎样设置啊?是不是和微软的计算器一样?设置字体的大小吧
      

  7.   

    计算器不是这样设计的吧,太简单了吧!!!!!!!!!!!!!!!!!