求大神给我的java计算器加监听以实现计算
importjava.swing.*;importjava.awt.*;publicclassasd{publicstaticvoidmain(String[]args)...
import java.swing.*;
import java.awt.*;
public class asd {
public static void main(String[] args) {
as a=new as();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class as extends JFrame
{ /**
*
*/
JPanel p1,p0;
StringBuffer str;
GridLayout g3,g0;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
TextField t1;
Container cp;
double x, y;// x和y都是运算数
int z;// Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
as()
{JButton b1=new JButton("7");
b1.addActionListener(new Bt());
JButton b2=new JButton("8");
b2.addActionListener(new Bt());
JButton b3=new JButton("9");
b3.addActionListener(new Bt());
JButton b4=new JButton("/");
b4.addActionListener(new Bt());
JButton b5=new JButton("4");
b5.addActionListener(new Bt());
JButton b6=new JButton("5");
b6.addActionListener(new Bt());
JButton b7=new JButton("6");
b7.addActionListener(new Bt());
JButton b8=new JButton("*");
b8.addActionListener(new Bt());
JButton b9=new JButton("1");
b9.addActionListener(new Bt());
JButton b10=new JButton("2");
b10.addActionListener(new Bt());
JButton b11=new JButton("3");
b11.addActionListener(new Bt());
JButton b12=new JButton("-");
b12.addActionListener(new Bt());
JButton b13=new JButton("清除");
b13.addActionListener(new Bt());
JButton b14=new JButton("0");
b14.addActionListener(new Bt());
JButton b15=new JButton("=");
b15.addActionListener(new Bt());
JButton b16=new JButton("+");
b16.addActionListener(new Bt());
setVisible(true);
cp=new Container();
this.add(cp);
cp= getContentPane();
JTextField t1= new JTextField(20);
JFrame f= new JFrame("安卓计算器");
g0 = new GridLayout(1, 1, 0, 0);
g3 = new GridLayout(4, 4, 0, 0);
t1 = new JTextField(27);// 显示屏
t1.setHorizontalAlignment(JTextField.RIGHT);
t1.setEnabled(true);
t1.setText("0");
//创建一个空字符串缓冲区
str = new StringBuffer();
p1=new JPanel();
p0=new JPanel();
p0.setLayout(g0);
p1.setLayout(g3);
f.add(p0);
p0.setBounds(0, 0, 300, 20);
p0.add(t1);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
f.add(t1,"North");
f.add(p1,"Center");
f.pack();
f.setVisible(true);
} 展开
import java.awt.*;
public class asd {
public static void main(String[] args) {
as a=new as();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class as extends JFrame
{ /**
*
*/
JPanel p1,p0;
StringBuffer str;
GridLayout g3,g0;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16;
TextField t1;
Container cp;
double x, y;// x和y都是运算数
int z;// Z表示单击了那一个运算符.0表示"+",1表示"-",2表示"*",3表示"/"
as()
{JButton b1=new JButton("7");
b1.addActionListener(new Bt());
JButton b2=new JButton("8");
b2.addActionListener(new Bt());
JButton b3=new JButton("9");
b3.addActionListener(new Bt());
JButton b4=new JButton("/");
b4.addActionListener(new Bt());
JButton b5=new JButton("4");
b5.addActionListener(new Bt());
JButton b6=new JButton("5");
b6.addActionListener(new Bt());
JButton b7=new JButton("6");
b7.addActionListener(new Bt());
JButton b8=new JButton("*");
b8.addActionListener(new Bt());
JButton b9=new JButton("1");
b9.addActionListener(new Bt());
JButton b10=new JButton("2");
b10.addActionListener(new Bt());
JButton b11=new JButton("3");
b11.addActionListener(new Bt());
JButton b12=new JButton("-");
b12.addActionListener(new Bt());
JButton b13=new JButton("清除");
b13.addActionListener(new Bt());
JButton b14=new JButton("0");
b14.addActionListener(new Bt());
JButton b15=new JButton("=");
b15.addActionListener(new Bt());
JButton b16=new JButton("+");
b16.addActionListener(new Bt());
setVisible(true);
cp=new Container();
this.add(cp);
cp= getContentPane();
JTextField t1= new JTextField(20);
JFrame f= new JFrame("安卓计算器");
g0 = new GridLayout(1, 1, 0, 0);
g3 = new GridLayout(4, 4, 0, 0);
t1 = new JTextField(27);// 显示屏
t1.setHorizontalAlignment(JTextField.RIGHT);
t1.setEnabled(true);
t1.setText("0");
//创建一个空字符串缓冲区
str = new StringBuffer();
p1=new JPanel();
p0=new JPanel();
p0.setLayout(g0);
p1.setLayout(g3);
f.add(p0);
p0.setBounds(0, 0, 300, 20);
p0.add(t1);
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.add(b6);
p1.add(b7);
p1.add(b8);
p1.add(b9);
p1.add(b10);
p1.add(b11);
p1.add(b12);
p1.add(b13);
p1.add(b14);
p1.add(b15);
p1.add(b16);
f.add(t1,"North");
f.add(p1,"Center");
f.pack();
f.setVisible(true);
} 展开
2个回答
展开全部
懒得改了,给你个现成的吧。
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MyCalculator implements ActionListener{
private JTextField jTextField;
private StringBuilder sb = new StringBuilder("0");
String operand1;
String operator = "=";
public void actionPerformed(ActionEvent e){
String comm = e.getActionCommand();
boolean append = false;
if("0123456789".contains(comm)){
if(append){
jTextField.setText(jTextField.getText() + comm);
}else{
jTextField.setText(comm);
append = true;
}
}else if("+-*/".contains(comm)){
operand1 = jTextField.getText();
operator = comm;
append = false;
}else if("=".equals(comm)){
String s2 = jTextField.getText();
double d1 = Double.parseDouble(operand1);
double d2 = Double.parseDouble(s2);
if("+".equals(operator)){
d1 = d1+d2;
}else if("-".equals(operator)){
d1 = d1-d2;
}else if("*".equals(operator)){
d1 = d1*d2;
}else if("/".equals(operator)){
d1 = d1/d2;
}
jTextField.setText(d1+"");
append = false;
}else if(".".equals(comm)){
String temp = jTextField.getText();
if(temp.indexOf('.') == -1){
jTextField.setText(temp + ".");
append = true; //?接着输入operator呢
}
}else if("+/-".equals(comm)){
String temp = jTextField.getText();
if(temp.startsWith("-")){
jTextField.setText(temp.substring(1));
}else{
jTextField.setText("-" + temp);
}
}else if("BackSpace".equals(comm)){
String temp = jTextField.getText();
if(temp.length() > 0){
jTextField.setText(temp.substring(0,temp.length()-1));
}
} else if ("C".equals(comm)) {
jTextField.setText("0");
} else if ("CE".equals(comm)) {
jTextField.setText("0");
}
}
public MyCalculator(){
JFrame jFrame = new JFrame("Calculator");
jTextField = new JTextField("0",20);
jTextField.setHorizontalAlignment(JTextField.RIGHT);
jTextField.setEditable(false);//不让编辑文本框
jFrame.add(jTextField,BorderLayout.NORTH);
JPanel jPanelBottom = new JPanel();
jFrame.add(jPanelBottom,BorderLayout.SOUTH);
JPanel jPanel = new JPanel();
GridLayout gridLayout = new GridLayout(5,4);
jPanel.setLayout(gridLayout);
jFrame.add(jPanel,BorderLayout.CENTER);
String[] label = {"BackSpace","CE","C","+", "7","8","9","-", "4", "5", "6", "*", "1", "2", "3", "/", "0", "+/-", ".", "=",};
for(int i=0;i<label.length;i++){
JButton jButton = new JButton(label[i]);
jButton.addActionListener(this);
jPanel.add(jButton);
}
jFrame.pack();//自动包裹,使用这个就不用setSize()
jFrame.setSize(300,250);
//jFrame.setResizable(false);//禁止调整Frame尺寸
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyCalculator();
}
}
import javax.swing.JFrame;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
public class MyCalculator implements ActionListener{
private JTextField jTextField;
private StringBuilder sb = new StringBuilder("0");
String operand1;
String operator = "=";
public void actionPerformed(ActionEvent e){
String comm = e.getActionCommand();
boolean append = false;
if("0123456789".contains(comm)){
if(append){
jTextField.setText(jTextField.getText() + comm);
}else{
jTextField.setText(comm);
append = true;
}
}else if("+-*/".contains(comm)){
operand1 = jTextField.getText();
operator = comm;
append = false;
}else if("=".equals(comm)){
String s2 = jTextField.getText();
double d1 = Double.parseDouble(operand1);
double d2 = Double.parseDouble(s2);
if("+".equals(operator)){
d1 = d1+d2;
}else if("-".equals(operator)){
d1 = d1-d2;
}else if("*".equals(operator)){
d1 = d1*d2;
}else if("/".equals(operator)){
d1 = d1/d2;
}
jTextField.setText(d1+"");
append = false;
}else if(".".equals(comm)){
String temp = jTextField.getText();
if(temp.indexOf('.') == -1){
jTextField.setText(temp + ".");
append = true; //?接着输入operator呢
}
}else if("+/-".equals(comm)){
String temp = jTextField.getText();
if(temp.startsWith("-")){
jTextField.setText(temp.substring(1));
}else{
jTextField.setText("-" + temp);
}
}else if("BackSpace".equals(comm)){
String temp = jTextField.getText();
if(temp.length() > 0){
jTextField.setText(temp.substring(0,temp.length()-1));
}
} else if ("C".equals(comm)) {
jTextField.setText("0");
} else if ("CE".equals(comm)) {
jTextField.setText("0");
}
}
public MyCalculator(){
JFrame jFrame = new JFrame("Calculator");
jTextField = new JTextField("0",20);
jTextField.setHorizontalAlignment(JTextField.RIGHT);
jTextField.setEditable(false);//不让编辑文本框
jFrame.add(jTextField,BorderLayout.NORTH);
JPanel jPanelBottom = new JPanel();
jFrame.add(jPanelBottom,BorderLayout.SOUTH);
JPanel jPanel = new JPanel();
GridLayout gridLayout = new GridLayout(5,4);
jPanel.setLayout(gridLayout);
jFrame.add(jPanel,BorderLayout.CENTER);
String[] label = {"BackSpace","CE","C","+", "7","8","9","-", "4", "5", "6", "*", "1", "2", "3", "/", "0", "+/-", ".", "=",};
for(int i=0;i<label.length;i++){
JButton jButton = new JButton(label[i]);
jButton.addActionListener(this);
jPanel.add(jButton);
}
jFrame.pack();//自动包裹,使用这个就不用setSize()
jFrame.setSize(300,250);
//jFrame.setResizable(false);//禁止调整Frame尺寸
jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new MyCalculator();
}
}
追问
你这只能个位数加减乘除…………
追答
你把 boolean append = false; 挪到外面去做成员变量即可。
public class MyCalculator implements ActionListener{
private JTextField jTextField;
private StringBuilder sb = new StringBuilder("0");
private String operand1;
private String operator = "=";
private boolean append = false;
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
String content = jTextField.getText();
if (content.startsWith("0") && !content.contains(".")) {
jTextField.setText("");
}
...
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你给出的不全,
Bt()是你的侦听类吧.给出这部分的代码呀. 楼上的没有实现你的要求吗?
Bt()是你的侦听类吧.给出这部分的代码呀. 楼上的没有实现你的要求吗?
追问
代码太长了 百度不让给这么长的!!!这些是布局 你给看这么给加个监听!随便给个加法的什么的,我模仿的弄就行了
追答
给你一个简单的例子,侦听器就是实现了 ActionListener 接口的类.
看你的例子中Bt这个类应该是侦听器类.
我这里直接实现了 ActionListener 接口,
所以是 button.addActionListener(this);
当按钮按下时,会执行.
public void actionPerformed(ActionEvent e) {
}
这个方法.也就是你要处理按下了那个按钮的逻辑.如果是数字你怎么办,如果是运算符怎么办
如果是=号,你怎么办.就是这样.
----------------------------------------------------------------------------------------
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Table extends JFrame implements ActionListener {
private JTextField textField;
public static void main(String[] args) {
new Table();
}
public Table() {
setSize(267, 140);
setDefaultCloseOperation(EXIT_ON_CLOSE);
getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(10, 10, 186, 21);
getContentPane().add(textField);
textField.setColumns(10);
JButton button = new JButton("1");
button.setBounds(10, 41, 39, 23);
button.addActionListener(this);
getContentPane().add(button);
JButton button_1 = new JButton("2");
button_1.setBounds(59, 41, 39, 23);
button_1.addActionListener(this);
getContentPane().add(button_1);
JButton button_2 = new JButton("+");
button_2.setBounds(108, 41, 39, 23);
button_2.addActionListener(this);
getContentPane().add(button_2);
JButton button_3 = new JButton("=");
button_3.setBounds(157, 41, 39, 23);
button_3.addActionListener(this);
getContentPane().add(button_3);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
textField.setText(e.getActionCommand());
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询