3个回答
2013-11-10
展开全部
Swing写的,不是很完善!!package com.swing;import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;public class 计算器 {
JFrame p ;
JPanel s;
JPanel w;
JTextField o ;
String c = "" ;
double t1;
String f;
void count(){
JButton a1 = new JButton("1");
JButton a2 = new JButton("2");
JButton a3 = new JButton("3");
JButton a4 = new JButton("+");
JButton a5 = new JButton("4");
JButton a6 = new JButton("5");
JButton a7 = new JButton("6");
JButton a8 = new JButton("-");
JButton a9 = new JButton("7");
JButton a10 = new JButton("8");
JButton a11 = new JButton("9");
JButton a12 = new JButton("*");
JButton a13 = new JButton("C");
JButton a14 = new JButton(".");
JButton a15 = new JButton("=");
JButton a16 = new JButton("/");
p = new JFrame("计算器");
p.setLocation(new Point(300,300));
s = new JPanel();
w = new JPanel();
GridLayout t = new GridLayout(4,4);
t.setHgap(5);
t.setVgap(5);
w.setLayout(t);
w.add(a1);
w.add(a2);
w.add(a3);
w.add(a4);
w.add(a5);
w.add(a6);
w.add(a7);
w.add(a8);
w.add(a9);
w.add(a10);
w.add(a11);
w.add(a12);
w.add(a13);
w.add(a14);
w.add(a15);
w.add(a16);
//注册监听
a1.addActionListener(new MyActionListener());
a2.addActionListener(new MyActionListener());
a3.addActionListener(new MyActionListener());
a4.addActionListener(new MyActionListener());
a5.addActionListener(new MyActionListener());
a6.addActionListener(new MyActionListener());
a7.addActionListener(new MyActionListener());
a8.addActionListener(new MyActionListener());
a9.addActionListener(new MyActionListener());
a10.addActionListener(new MyActionListener());
a11.addActionListener(new MyActionListener());
a12.addActionListener(new MyActionListener());
a13.addActionListener(new MyActionListener());
a14.addActionListener(new MyActionListener());
a15.addActionListener(new MyActionListener());
a16.addActionListener(new MyActionListener());
o = new JTextField();
o.setHorizontalAlignment(JTextField.RIGHT);
// s.add(o);
p.add(o,BorderLayout.NORTH);
p.add(w,BorderLayout.CENTER);
p.pack();
p.setResizable(false);
p.setVisible(true);
}
public static void main(String[] args){
计算器 ji = new 计算器();
ji.count();
}
//监听事件
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String b = e.getActionCommand();
if(b.equals("+")){
t1 = Double.parseDouble(o.getText());
c = "+";
o.setText("");
}else if(b.equals("-")){
t1 = Double.parseDouble(o.getText());
c = "-";
o.setText("");
}else if(b.equals("*")){
c = "*";
t1 = Double.parseDouble(o.getText());
o.setText("");
}else if(b.equals("/")){
c = "/";
t1 = Double.parseDouble(o.getText());
o.setText("");
}else if(b.equals("=")){
double t2 = Double.parseDouble(o.getText());
if(c.equals("-")){
double t = t1 -t2;
f = Double.toString(t);
o.setText(f);
}else if(c.equals("+")){
double t = t1 + t2;
f = Double.toString(t);
o.setText(f);
}else if(c.equals("*")){
double t = t1 * t2;
f = Double.toString(t);
o.setText(f);
}else if(c.equals("/")){
double t = t1 / t2;
f = Double.toString(t);
o.setText(f);
}
}else{
String ti = o.getText()+b;
o.setText(ti);
}
}
}
}
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;public class 计算器 {
JFrame p ;
JPanel s;
JPanel w;
JTextField o ;
String c = "" ;
double t1;
String f;
void count(){
JButton a1 = new JButton("1");
JButton a2 = new JButton("2");
JButton a3 = new JButton("3");
JButton a4 = new JButton("+");
JButton a5 = new JButton("4");
JButton a6 = new JButton("5");
JButton a7 = new JButton("6");
JButton a8 = new JButton("-");
JButton a9 = new JButton("7");
JButton a10 = new JButton("8");
JButton a11 = new JButton("9");
JButton a12 = new JButton("*");
JButton a13 = new JButton("C");
JButton a14 = new JButton(".");
JButton a15 = new JButton("=");
JButton a16 = new JButton("/");
p = new JFrame("计算器");
p.setLocation(new Point(300,300));
s = new JPanel();
w = new JPanel();
GridLayout t = new GridLayout(4,4);
t.setHgap(5);
t.setVgap(5);
w.setLayout(t);
w.add(a1);
w.add(a2);
w.add(a3);
w.add(a4);
w.add(a5);
w.add(a6);
w.add(a7);
w.add(a8);
w.add(a9);
w.add(a10);
w.add(a11);
w.add(a12);
w.add(a13);
w.add(a14);
w.add(a15);
w.add(a16);
//注册监听
a1.addActionListener(new MyActionListener());
a2.addActionListener(new MyActionListener());
a3.addActionListener(new MyActionListener());
a4.addActionListener(new MyActionListener());
a5.addActionListener(new MyActionListener());
a6.addActionListener(new MyActionListener());
a7.addActionListener(new MyActionListener());
a8.addActionListener(new MyActionListener());
a9.addActionListener(new MyActionListener());
a10.addActionListener(new MyActionListener());
a11.addActionListener(new MyActionListener());
a12.addActionListener(new MyActionListener());
a13.addActionListener(new MyActionListener());
a14.addActionListener(new MyActionListener());
a15.addActionListener(new MyActionListener());
a16.addActionListener(new MyActionListener());
o = new JTextField();
o.setHorizontalAlignment(JTextField.RIGHT);
// s.add(o);
p.add(o,BorderLayout.NORTH);
p.add(w,BorderLayout.CENTER);
p.pack();
p.setResizable(false);
p.setVisible(true);
}
public static void main(String[] args){
计算器 ji = new 计算器();
ji.count();
}
//监听事件
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
String b = e.getActionCommand();
if(b.equals("+")){
t1 = Double.parseDouble(o.getText());
c = "+";
o.setText("");
}else if(b.equals("-")){
t1 = Double.parseDouble(o.getText());
c = "-";
o.setText("");
}else if(b.equals("*")){
c = "*";
t1 = Double.parseDouble(o.getText());
o.setText("");
}else if(b.equals("/")){
c = "/";
t1 = Double.parseDouble(o.getText());
o.setText("");
}else if(b.equals("=")){
double t2 = Double.parseDouble(o.getText());
if(c.equals("-")){
double t = t1 -t2;
f = Double.toString(t);
o.setText(f);
}else if(c.equals("+")){
double t = t1 + t2;
f = Double.toString(t);
o.setText(f);
}else if(c.equals("*")){
double t = t1 * t2;
f = Double.toString(t);
o.setText(f);
}else if(c.equals("/")){
double t = t1 / t2;
f = Double.toString(t);
o.setText(f);
}
}else{
String ti = o.getText()+b;
o.setText(ti);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-10
展开全部
来我空间 我自己写的代码全被我方QQ空间里了 自己COPY去
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-11-10
展开全部
哥 太简单了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询