java吧 关注:1,235,547贴子:12,703,556
  • 7回复贴,共1

java简单计算器

只看楼主收藏回复

无聊做个java加减乘除计算器,int型,没有小数,,呵呵,,真特么无聊,,
package swing;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test extends JFrame { //这里误用void,返回空值
JTextField jTextField;
String num1="";
String num2="";
String flag="";
public Test() {
super();
this.setTitle("Calculator"); //窗体标题显示
this.setSize(310, 350);
this.getContentPane().setLayout(null);
this.add(getjTextField());
for(int i = 0; i<10;i++){
this.add(getjButton(i+""));
}
this.add(getFlag("+"));
this.add(getFlag("-"));
this.add(getFlag("X"));
this.add(getFlag("/"));
this.add(getEqual());
this.add(getClear());
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public JTextField getjTextField(){
if(jTextField == null){
JTextField jTextField = new JTextField();
jTextField.setBounds(50, 49, 200, 20);
this.jTextField = jTextField;
}
return jTextField;
}
public JButton getjButton(final String num){
JButton jButton = new JButton(num);
int i = Integer.parseInt(num);
jButton.setBounds(50+i%3*50,80+i/3*50, 50, 50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(flag.equals("")){
num1 = num1+num;
jTextField.setText(num1);
}else{
num2= num2 + num;
jTextField.setText(num1+" "+flag +" "+num2);
}
}
});
return jButton;
}
public JButton getFlag(final String flaginput){
JButton jButton = new JButton(flaginput);
if(flaginput.equals("+")){
jButton.setBounds(200,80, 50, 50);
}else if(flaginput.equals("-")){
jButton.setBounds(200,130, 50, 50);
}else if(flaginput.equals("X")){
jButton.setBounds(200,180, 50, 50);
}else if(flaginput.equals("/")){
jButton.setBounds(200,230, 50, 50);
}
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
flag = flaginput;
jTextField.setText(num1 + " " + flag);
}
});
return jButton;
}
public JButton getEqual(){
JButton jButton = new JButton("=");
jButton.setBounds(150,230, 50, 50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!flag.equals("")&&!"".equals(num2)&&!"".equals(num1)){
Integer n1 = Integer.parseInt(num1);
Integer n2 = Integer.parseInt(num2);
if("+".equals(flag)){
num1=n1+n2+"";
jTextField.setText((n1+n2)+"");
}else if("-".equals(flag)){
num1=n1-n2+"";
jTextField.setText((n1-n2)+"");
}else if("X".equals(flag)){
num1=n1*n2+"";
jTextField.setText((n1*n2)+"");
}else if("/".equals(flag)){
num1=n1/n2+"";
jTextField.setText((n1/n2)+"");
}
num2="";
flag="";
}
}
});
return jButton;
}
public JButton getClear(){
JButton jButton = new JButton("C");
jButton.setBounds(100,230,50,50);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
num1="";
num2="";
flag="";
jTextField.setText("");
}
});
return jButton;
}
public static void main(String[] args) throws HeadlessException {
Test kk = new Test() ;
kk.setVisible(true); //原来的show()显示已过时
}
}


IP属地:福建1楼2015-09-30 14:51回复
    坐一楼,不要问我到底多无聊


    IP属地:福建2楼2015-09-30 14:53
    收起回复
      JButton……好熟悉的感觉


      IP属地:上海来自Android客户端3楼2015-09-30 14:59
      回复
        还不如直接给个界面直观点,这代码,谁愿意看!


        IP属地:广东来自Android客户端4楼2015-09-30 18:20
        收起回复
          楼主你好,加减乘除清零功能不会写,可以教一下我吗
          mport java.awt.BorderLayout;
          import java.awt.GridLayout;
          import java.awt.Panel;
          import java.awt.event.ActionEvent;
          import java.awt.event.ActionListener;
          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.JTextField;
          public class Calculator {
          JFrame jf = new JFrame("计算器");
          Panel p = new Panel();
          JTextField jtf = new JTextField();
          JButton yi = new JButton("1");
          JButton er = new JButton"2");
          JButton san = new JButton("3");
          JButton si = new JButton("4");
          JButton wu = new JButton("5");
          JButton liu = new JButton("6");
          JButton qi = new JButton("7");
          JButton ba = new JButton("8");
          JButton jiu = new JButton("9");
          JButton ling = new JButton("0");
          JButton cheng = new JButton("*");
          JButton chu = new JButton("/");
          JButton jia = new JButton("+");
          JButton jian = new JButton("-");
          JButton qingchu = new JButton("c");
          JButton dengyu = new JButton("=");
          class A implements ActionListener{
          @Override
          public void actionPerformed(ActionEvent arg0) {
          // TODO Auto-generated method stub
          String s1 = arg0.getActionCommand();
          jtf.setText(s1);
          }
          }
          void init(){
          A a= new A(); //创建监听器对象a
          yi.addActionListener(a);
          er.addActionListener(a);
          san.addActionListener(a);
          si.addActionListener(a);
          wu.addActionListener(a);
          liu.addActionListener(a);
          qi.addActionListener(a);
          ba.addActionListener(a);
          jiu.addActionListener(a);
          ling.addActionListener(a);
          cheng.addActionListener(a);
          chu.addActionListener(a);
          qingchu.addActionListener(a);
          jia.addActionListener(a);
          jian.addActionListener(a);
          dengyu.addActionListener(a);
          p.setLayout(new GridLayout(4,4));
          jf.add(jtf);
          p.add(yi);
          p.add(er);
          p.add(san);
          p.add(si);
          p.add(wu);
          p.add(liu);
          p.add(qi);
          p.add(ba);
          p.add(jiu);
          p.add(ling);
          p.add(cheng);
          p.add(chu);
          p.add(jia);
          p.add(jian);
          p.add(qingchu);
          p.add(dengyu);
          jf.add(p, BorderLayout.SOUTH);
          jf.setSize(300, 300);
          jf.setVisible(true);
          }
          public static void main(String[]args) {
          new Calculator().init();
          }
          }


          IP属地:广东来自Android客户端5楼2022-06-01 07:39
          回复
            话说 jbutton和button有啥区别呢


            IP属地:陕西来自Android客户端6楼2022-06-01 08:07
            回复