import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class jisuanqi extends JFrame{
private JButton JButton0;
private JButton JButton1;
private JButton JButton2;
private JButton JButton3;
private JButton JButton4;
private JButton JButton5;
private JButton JButton6;
private JButton JButton7;
private JButton JButton8;
private JButton JButton9;
private JButton JButtonjia;
private JButton JButtonjian;
private JButton JButtoncheng;
private JButton JButtonchu;
private JButton JButtondeng;
private JButton JButtonqing;
private JButton JButtontui;
private JTextField JTextField1;
static int fuhao;
static long shu;
public jisuanqi(){
createUserInterface();
}
public void createUserInterface(){
Container contentPane = getContentPane();
contentPane.setLayout( null );
JButton1 =new JButton();
JButton1.setText("1");
JButton1.setBounds(0,60,60,60);
contentPane.add(JButton1);
JButton1.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton1ActionPerformed( event );
}
}
);
JButton2 =new JButton();
JButton2.setText("2");
JButton2.setBounds(60,60,60,60);
contentPane.add(JButton2);
JButton2.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton2ActionPerformed( event );
}
}
);
JButton3 =new JButton();
JButton3.setText("3");
JButton3.setBounds(120,60,60,60);
contentPane.add(JButton3);
JButton3.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton3ActionPerformed( event );
}
}
);
JButton4 =new JButton();
JButton4.setText("4");
JButton4.setBounds(0,120,60,60);
contentPane.add(JButton4);
JButton4.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton4ActionPerformed( event );
}
}
);
JButton5 =new JButton();
JButton5.setText("5");
JButton5.setBounds(60,120,60,60);
contentPane.add(JButton5);
JButton5.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton5ActionPerformed( event );
}
}
);
JButton6 =new JButton();
JButton6.setText("6");
JButton6.setBounds(120,120,60,60);
contentPane.add(JButton6);
JButton6.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton6ActionPerformed( event );
}
}
);
JButton7 =new JButton();
JButton7.setText("7");
JButton7.setBounds(0,180,60,60);
contentPane.add(JButton7);
JButton7.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton7ActionPerformed( event );
}
}
);
JButton8 =new JButton();
JButton8.setText("8");
JButton8.setBounds(60,180,60,60);
contentPane.add(JButton8);
JButton8.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton8ActionPerformed( event );
}
}
);
JButton9 =new JButton();
JButton9.setText("9");
JButton9.setBounds(120,180,60,60);
contentPane.add(JButton9);
JButton9.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton9ActionPerformed( event );
}
}
);
JButton0 =new JButton();
JButton0.setText("0");
JButton0.setBounds(60,240,60,60);
contentPane.add(JButton0);
JButton0.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButton0ActionPerformed( event );
}
}
);
JButtonjia =new JButton();
JButtonjia.setText("+");
JButtonjia.setBounds(180,60,60,60);
contentPane.add(JButtonjia);
JButtonjia.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtonjiaActionPerformed( event );
}
}
);
JButtonjian =new JButton();
JButtonjian.setText("-");
JButtonjian.setBounds(180,120,60,60);
contentPane.add(JButtonjian);
JButtonjian.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtonjianActionPerformed( event );
}
}
);
JButtoncheng =new JButton();
JButtoncheng.setText("*");
JButtoncheng.setBounds(180,180,60,60);
contentPane.add(JButtoncheng);
JButtoncheng.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtonchengActionPerformed( event );
}
}
);
JButtonchu =new JButton();
JButtonchu.setText("/");
JButtonchu.setBounds(180,240,60,60);
contentPane.add(JButtonchu);
JButtonchu.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtonchuActionPerformed( event );
}
}
);
JButtondeng =new JButton();
JButtondeng.setText("=");
JButtondeng.setBounds(120,240,60,60);
contentPane.add(JButtondeng);
JButtondeng.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtondengActionPerformed( event );
}
}
);
JButtonqing =new JButton();
JButtonqing.setText("清0");
JButtonqing.setBounds(0,240,60,60);
contentPane.add(JButtonqing);
JButtonqing.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtonqingActionPerformed( event );
}
}
);
JButtontui =new JButton();
JButtontui.setText("退格");
JButtontui.setBounds(180,0,60,60);
contentPane.add(JButtontui);
JButtontui.addActionListener(
new ActionListener(){
public void actionPerformed( ActionEvent event ){
JButtontuiActionPerformed( event );
}
}
);
JTextField1 = new JTextField();
JTextField1.setText( "0" );
JTextField1.setBounds( 20, 20, 140, 20 );
JTextField1.setHorizontalAlignment( JTextField.RIGHT );
contentPane.add( JTextField1 );
setTitle("jisuanqi");
this.setSize(240,360);
this.setVisible(true);
}
private void JButton1ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+1;
JTextField1.setText( String.valueOf(b) );
}
private void JButton2ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+2;
JTextField1.setText( String.valueOf(b) );
}
private void JButton3ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+3;
JTextField1.setText( String.valueOf(b) );
}
private void JButton4ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+4;
JTextField1.setText( String.valueOf(b) );
}
private void JButton5ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+5;
JTextField1.setText( String.valueOf(b) );
}
private void JButton6ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+6;
JTextField1.setText( String.valueOf(b) );
}
private void JButton7ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+7;
JTextField1.setText( String.valueOf(b) );
}
private void JButton8ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+8;
JTextField1.setText( String.valueOf(b) );
}
private void JButton9ActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
b=a*10+9;
JTextField1.setText( String.valueOf(b) );
}
private void JButton0ActionPerformed( ActionEvent event ){
long a=0,b=0;
if( JTextField1.getText()==""||Integer.parseInt( JTextField1.getText() )==0)
JTextField1.setText(String.valueOf(b));
else {a=Integer.parseInt( JTextField1.getText() );b=a*10;}
JTextField1.setText( String.valueOf(b) );
}
private void JButtonqingActionPerformed( ActionEvent event ){
JTextField1.setText( String.valueOf(0) );
}
private void JButtontuiActionPerformed( ActionEvent event ){
long a,b;
a=Integer.parseInt( JTextField1.getText() );
if(a<=10) JTextField1.setText( String.valueOf(0) );
else{b=a%10;a=(a-b)/10;JTextField1.setText( String.valueOf(a) );}
}
private void JButtonjiaActionPerformed( ActionEvent event ){
fuhao=1;
shu=Integer.parseInt( JTextField1.getText() );
JTextField1.setText( String.valueOf(0) );
}
private void JButtonjianActionPerformed( ActionEvent event ){
fuhao=2;
shu=Integer.parseInt( JTextField1.getText() );
JTextField1.setText( String.valueOf(0) );
}
private void JButtonchengActionPerformed( ActionEvent event ){
fuhao=3;
shu=Integer.parseInt( JTextField1.getText() );
JTextField1.setText( String.valueOf(0) );
}
private void JButtonchuActionPerformed( ActionEvent event ){
fuhao=4;
shu=Integer.parseInt( JTextField1.getText() );
JTextField1.setText( String.valueOf(0) );
}
private void JButtondengActionPerformed( ActionEvent event ){
long a;
long b=Integer.parseInt( JTextField1.getText() );
if(fuhao==1) {a=b+shu;JTextField1.setText( String.valueOf(a) );}
else if(fuhao==2) {a=shu-b;JTextField1.setText( String.valueOf(a) );}
else if(fuhao==3) {a=b*shu;JTextField1.setText( String.valueOf(a) );}
else if(fuhao==4) {a=shu/b;JTextField1.setText( String.valueOf(a) );}
}
public static void main( String[] args ){
jisuanqi application = new jisuanqi();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
}
to 曾啊牛
首先,我必须敬佩你的毅力~~你让我想起以前的我,也是那么的热情,那么的有精力
但是,我必须还是得说:这样写这个软件!
you make a mistake!
我们先说实现的细节错误
1.对于一个计算器,我们一接手马上就应该意识到,要检查除数不能为0!!!这也是我测试的第一个计算! 很遗憾,你没通过...
2.输出的“液晶”显示屏你是用个JTextarea代替的,这很对,但是你也要设置一下不能编辑吧,如果我往里面输入的字母,你知道会发生什么情况吗?
再说说实现的设计错误
1.难道你打了那么多的JButton就没意识到,其实这是可以作为一个类的么!这样连楼9的意思都不用~
2.写swing的时候,极力不推荐采用坐标方式安插对象,比如JButton2.setBounds(60, 60, 60, 60);因为只要用户一旦改变了分辨率,你这个坐标就全乱了,当然这个对这个计算器那么小的项目来说根本就没有用Layout Managers的必要,但是为了你的以后的大设计中不出现这样的代码,我还是忍不住和你说说。
最后我还要说说整个程序的设计错误~
界面设计应该要与逻辑设计相分离,对于这个错误嘛~~你同样也可以不理会,这个要求是有点苛刻了的,起码对于这个小小的计算器来说,但是我记得我也是从计算器学起的。
我说那么多,其实不是针对这个计算器的,这个计算器除了我前面说的那些实现的错误以外,其他还算是勉强的,特别是这个程序的ActionListener的实现让我又学会了一个实现的方法~谢谢
我看了你最近的帖子,知道你要做一个药店管理的软件~~根据你现在的知识,我想你应该是打算往C/S方向发展的。祝你顺利~
我早在几个月前(呵呵,现在的电脑知识都是按月计的)就放弃了java的桌面应用程序设计~因为java在桌面应用上是不可能和已经完全绑定、整合到windows操作系统的vc++、c#相比的。Linux/unix下java就更加慢了~要知道玩Linux/Unix的人不是网管就是C语言N牛的程序设计员...