一个JAVA小程序,计算器的
程序做了一半不会做了,出了错误,不知道怎么办,而且键盘事件不会,具体实现计算的方法也不会,画面也有错误,数字键与清除键距离太远了下面是程序:importjavax.swi...
程序做了一半不会做了,出了错误,不知道怎么办,而且键盘事件不会,具体实现计算的方法也不会,画面也有错误,数字键与清除键距离太远了
下面是程序:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class jpanel extends JPanel implements ActionListener{
static JTextField text=new JTextField(25);
text.setEditable(false);
public jpanel(){
add(text);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
text.setText(s);
}
public static void main(String args[]){
jpanel panel=new jpanel();
panel panel1=new panel();
panel1 panel2=new panel1();
JFrame frame=new JFrame("简易计算器");
Container c=frame.getContentPane();
BorderLayout b=new BorderLayout(10,10);
c.setLayout(b);
c.add(BorderLayout.NORTH,panel);
c.add(BorderLayout.CENTER,panel1);
c.add(BorderLayout.SOUTH,panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
class panel extends JPanel implements ActionListener{
public static JButton jbutton1=new JButton("删除");
JButton jbutton2=new JButton("退格");
public panel(){
add(jbutton1);
add(jbutton2);
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
text.setText(s);
}
}
class panel1 extends JPanel implements ActionListener{
JPanel p=new JPanel();
public panel1(){
p.setLayout(new GridLayout(4,4));
String buttons="0123456789.+-*/=";
for(int i=0;i<buttons.length();i++)
addButton(p,buttons.substring(i,i+1));
add(p);
}
void addButton(Container c,String s){
JButton b=new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
text.setText(s);
}
} 展开
下面是程序:
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class jpanel extends JPanel implements ActionListener{
static JTextField text=new JTextField(25);
text.setEditable(false);
public jpanel(){
add(text);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
text.setText(s);
}
public static void main(String args[]){
jpanel panel=new jpanel();
panel panel1=new panel();
panel1 panel2=new panel1();
JFrame frame=new JFrame("简易计算器");
Container c=frame.getContentPane();
BorderLayout b=new BorderLayout(10,10);
c.setLayout(b);
c.add(BorderLayout.NORTH,panel);
c.add(BorderLayout.CENTER,panel1);
c.add(BorderLayout.SOUTH,panel2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
}
}
class panel extends JPanel implements ActionListener{
public static JButton jbutton1=new JButton("删除");
JButton jbutton2=new JButton("退格");
public panel(){
add(jbutton1);
add(jbutton2);
jbutton1.addActionListener(this);
jbutton2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
text.setText(s);
}
}
class panel1 extends JPanel implements ActionListener{
JPanel p=new JPanel();
public panel1(){
p.setLayout(new GridLayout(4,4));
String buttons="0123456789.+-*/=";
for(int i=0;i<buttons.length();i++)
addButton(p,buttons.substring(i,i+1));
add(p);
}
void addButton(Container c,String s){
JButton b=new JButton(s);
c.add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
String s=e.getActionCommand();
text.setText(s);
}
} 展开
2个回答
展开全部
text.setEditable(false);// 得写到方法体中去,单独是不能存在的,你看来是写得头晕了哈
第二个类那样是用不了text.setText();的
没有这个对象,你把它们写到同一个类中去就没问题了。
非要这么写,那你得再写个方法将text对象传递出来在第二个类中获取来使用……
你写得有点儿乱,我也不知道怎么改了!写一个太花时间,我这边给你一个吧!
你说按钮距离那就是你布局没有处理好!建议直接像下面给出的程序那样!也简单
出了问题了不怕,问题越多进步越快哈
有问题你可以加这个群进行提问:5358308,java world!(two)
给你一个计算器程序
//第一个类是GUI,两个类放两文件里去
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
public class Calculate_GUI extends JFrame{
private JPanel p1;
private JButton keyButton[]=new JButton[16],signButton,clearButton,deleteButton;
private JTextField outputField;
private Container contentPane;
private boolean firstInput=true;
private Calculate_Model model=new Calculate_Model();
public Calculate_GUI(){
p1=new JPanel();
keyButton[0]=new JButton("1"); //创建各个按钮
keyButton[1]=new JButton("2");
keyButton[2]=new JButton("3");
keyButton[3]=new JButton("/");
keyButton[4]=new JButton("4");
keyButton[5]=new JButton("5");
keyButton[6]=new JButton("6");
keyButton[7]=new JButton("*");
keyButton[8]=new JButton("7");
keyButton[9]=new JButton("8");
keyButton[10]=new JButton("9");
keyButton[11]=new JButton("-");
keyButton[12]=new JButton("0");
keyButton[13]=new JButton(".");
keyButton[14]=new JButton("=");
keyButton[15]=new JButton("+");
clearButton=new JButton("c");
signButton=new JButton("+/-");
deleteButton=new JButton("←");
outputField=new JTextField(); //它允许编辑单行文本
outputField.setText("0");
outputField.setEditable(false); //指示此 TextComponent 是否应该为可编辑的
contentPane=this.getContentPane(); //返回此窗体的 contentPane 对象;
this.setSize(250,250);
p1.setLayout(new java.awt.GridLayout(4,5)); //创建具有4行5列的布局
for(int i=0;i<keyButton.length;i++) //在容器中画上按钮
{
if(i==4) p1.add(deleteButton);
if(i==8) p1.add(clearButton);
if(i==12) p1.add(signButton);
keyButton[i].setFont(new java.awt.Font("Dialog",3,15));//设置字体Dialog字体名,样式,大小
p1.add(keyButton[i]);
}
contentPane.add(outputField,java.awt.BorderLayout.NORTH);
contentPane.add(p1,java.awt.BorderLayout.CENTER);
this.show();
} //用于显示界面
public void registEvent() //注册各个事件
{
this.addWindowListener(new WindowAdapter() //窗口事件,窗口关闭里程序关闭
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
for(int i=0;i<keyButton.length;i++)
{
keyButton[i].addActionListener(new keyButtonHandler()); //为每一个按钮添加监听器
}
signButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
outputField.setText(String.valueOf(model.toSign(Double.parseDouble(outputField.getText()))));
}
});
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
outputField.setText(model.backSpace(outputField.getText()));
if(outputField.getText().equals("0"))
firstInput=true;
}
});
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
firstInput=true;
outputField.setText("0");
model.reset();
}
});
}
class keyButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
char actionCommand=ae.getActionCommand().charAt(0);
switch(actionCommand)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
case '.':
if(firstInput) //是否为首次输入
{
outputField.setText(String.valueOf(actionCommand));
firstInput=false;
}
else
outputField.setText(outputField.getText()+actionCommand);
break;
case '+':
case '-':
case '*':
case '/':
if(firstInput)
model.setOperate(actionCommand);
else
{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
model.setOperate(actionCommand);
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
break;
case '=':
if(firstInput)
{
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
}
else
{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
}
}
}
public static void main(String args[])
{
new Calculate_GUI().registEvent();
}
}
/******************************************
*****计算的模块****************************/
public class Calculate_Model
{
private double result;
private double operateNumber;
private char operate;
public Calculate_Model() //构造方法进行初始化
{
result=0; //结果
operateNumber=0; //操作数
operate=' '; //操作符
}
public void setResult(double n) //设置结果,成员变量result是private的
{
result=n;
}
public void setOperate(char o) //设置操作符
{
operate=o;
}
public void setOperateNumber(double n) //设置操作数
{
operateNumber=n;
}
public double getResult() //获取结果
{
return result;
}
public void calculating() //计算过程
{
switch(operate)
{
case ' ':result=operateNumber;break; //分别对操作判断后进行相应操作
case '+':result+=operateNumber;break;
case '-':result-=operateNumber;break;
case '*':result*=operateNumber;break;
case '/':result/=operateNumber;break;
}
}
public void reset() //重设方法
{
result=0;
operateNumber=0;
operate=' ';
}
public double toSign(double n) //正负转化
{
return 0-(n-0);
}
public String backSpace(String n) //对backSpace设置
{
String reResult="0";
try
{
reResult=n.substring(0,n.length()-1); //从第一个开始取前n-1个
Double.parseDouble(reResult); //将其转化为Double型
}
catch(NumberFormatException e)
{
reResult="0";
}
return reResult;
}
}
第二个类那样是用不了text.setText();的
没有这个对象,你把它们写到同一个类中去就没问题了。
非要这么写,那你得再写个方法将text对象传递出来在第二个类中获取来使用……
你写得有点儿乱,我也不知道怎么改了!写一个太花时间,我这边给你一个吧!
你说按钮距离那就是你布局没有处理好!建议直接像下面给出的程序那样!也简单
出了问题了不怕,问题越多进步越快哈
有问题你可以加这个群进行提问:5358308,java world!(two)
给你一个计算器程序
//第一个类是GUI,两个类放两文件里去
import javax.swing.*;
import java.awt.Container;
import java.awt.event.*;
public class Calculate_GUI extends JFrame{
private JPanel p1;
private JButton keyButton[]=new JButton[16],signButton,clearButton,deleteButton;
private JTextField outputField;
private Container contentPane;
private boolean firstInput=true;
private Calculate_Model model=new Calculate_Model();
public Calculate_GUI(){
p1=new JPanel();
keyButton[0]=new JButton("1"); //创建各个按钮
keyButton[1]=new JButton("2");
keyButton[2]=new JButton("3");
keyButton[3]=new JButton("/");
keyButton[4]=new JButton("4");
keyButton[5]=new JButton("5");
keyButton[6]=new JButton("6");
keyButton[7]=new JButton("*");
keyButton[8]=new JButton("7");
keyButton[9]=new JButton("8");
keyButton[10]=new JButton("9");
keyButton[11]=new JButton("-");
keyButton[12]=new JButton("0");
keyButton[13]=new JButton(".");
keyButton[14]=new JButton("=");
keyButton[15]=new JButton("+");
clearButton=new JButton("c");
signButton=new JButton("+/-");
deleteButton=new JButton("←");
outputField=new JTextField(); //它允许编辑单行文本
outputField.setText("0");
outputField.setEditable(false); //指示此 TextComponent 是否应该为可编辑的
contentPane=this.getContentPane(); //返回此窗体的 contentPane 对象;
this.setSize(250,250);
p1.setLayout(new java.awt.GridLayout(4,5)); //创建具有4行5列的布局
for(int i=0;i<keyButton.length;i++) //在容器中画上按钮
{
if(i==4) p1.add(deleteButton);
if(i==8) p1.add(clearButton);
if(i==12) p1.add(signButton);
keyButton[i].setFont(new java.awt.Font("Dialog",3,15));//设置字体Dialog字体名,样式,大小
p1.add(keyButton[i]);
}
contentPane.add(outputField,java.awt.BorderLayout.NORTH);
contentPane.add(p1,java.awt.BorderLayout.CENTER);
this.show();
} //用于显示界面
public void registEvent() //注册各个事件
{
this.addWindowListener(new WindowAdapter() //窗口事件,窗口关闭里程序关闭
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
});
for(int i=0;i<keyButton.length;i++)
{
keyButton[i].addActionListener(new keyButtonHandler()); //为每一个按钮添加监听器
}
signButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
outputField.setText(String.valueOf(model.toSign(Double.parseDouble(outputField.getText()))));
}
});
deleteButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
outputField.setText(model.backSpace(outputField.getText()));
if(outputField.getText().equals("0"))
firstInput=true;
}
});
clearButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
firstInput=true;
outputField.setText("0");
model.reset();
}
});
}
class keyButtonHandler implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
char actionCommand=ae.getActionCommand().charAt(0);
switch(actionCommand)
{
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '0':
case '.':
if(firstInput) //是否为首次输入
{
outputField.setText(String.valueOf(actionCommand));
firstInput=false;
}
else
outputField.setText(outputField.getText()+actionCommand);
break;
case '+':
case '-':
case '*':
case '/':
if(firstInput)
model.setOperate(actionCommand);
else
{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
model.setOperate(actionCommand);
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
break;
case '=':
if(firstInput)
{
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
}
else
{
model.setOperateNumber(Double.parseDouble(outputField.getText()));
model.calculating();
outputField.setText(String.valueOf(model.getResult()));
firstInput=true;
}
}
}
}
public static void main(String args[])
{
new Calculate_GUI().registEvent();
}
}
/******************************************
*****计算的模块****************************/
public class Calculate_Model
{
private double result;
private double operateNumber;
private char operate;
public Calculate_Model() //构造方法进行初始化
{
result=0; //结果
operateNumber=0; //操作数
operate=' '; //操作符
}
public void setResult(double n) //设置结果,成员变量result是private的
{
result=n;
}
public void setOperate(char o) //设置操作符
{
operate=o;
}
public void setOperateNumber(double n) //设置操作数
{
operateNumber=n;
}
public double getResult() //获取结果
{
return result;
}
public void calculating() //计算过程
{
switch(operate)
{
case ' ':result=operateNumber;break; //分别对操作判断后进行相应操作
case '+':result+=operateNumber;break;
case '-':result-=operateNumber;break;
case '*':result*=operateNumber;break;
case '/':result/=operateNumber;break;
}
}
public void reset() //重设方法
{
result=0;
operateNumber=0;
operate=' ';
}
public double toSign(double n) //正负转化
{
return 0-(n-0);
}
public String backSpace(String n) //对backSpace设置
{
String reResult="0";
try
{
reResult=n.substring(0,n.length()-1); //从第一个开始取前n-1个
Double.parseDouble(reResult); //将其转化为Double型
}
catch(NumberFormatException e)
{
reResult="0";
}
return reResult;
}
}
展开全部
package javatest;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class calculatorface extends JFrame{
/**
每次用时都要按下clear按钮,然后就是小数点按钮我没有处理,做的不是太好,里面除运算无法计算到小数
*/
private static final long serialVersionUID = 1581029666853134020L;
ArrayList<String> al = new ArrayList<String>();
JButton jb1 = new JButton("1");
JButton jb2 = new JButton("2");
JButton jb3 = new JButton("3");
JButton jb4 = new JButton("4");
JButton jb5 = new JButton("5");
JButton jb6 = new JButton("6");
JButton jb7 = new JButton("7");
JButton jb8 = new JButton("8");
JButton jb9 = new JButton("9");
JButton jb0 = new JButton("0");
JButton jbxing = new JButton("*");
JButton jbdian = new JButton(".");
JButton jbjian = new JButton("-");
JButton jbjia = new JButton("+");
JButton jbchu = new JButton("/");
JButton jbdeng = new JButton("=");
JButton jbC = new JButton("Clear");
public JLabel jl = new JLabel("0.",JLabel.RIGHT);
public calculatorface(){
super("计算器");
init();
}
public void init(){
Container c = getContentPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JPanel jp5 = new JPanel();
JPanel jp6 = new JPanel();
/*设定容器的布局方式*/
jp1.setLayout(new GridLayout(1,1));
jp2.setLayout(new GridLayout(1,4));
jp3.setLayout(new GridLayout(1,4));
jp4.setLayout(new GridLayout(1,4));
jp5.setLayout(new GridLayout(1,4));
jp6.setLayout(new GridLayout(1,4));
c.setLayout(new GridLayout(6,1));
/*将文本域放入容器jp1*/
jp1.add(jl);
/*将按钮放入容器jp2*/
jp2.add(jb7);
jp2.add(jb8);
jp2.add(jb9);
jp2.add(jbchu);
/*将按钮放入容器jp3*/
jp3.add(jb4);
jp3.add(jb5);
jp3.add(jb6);
jp3.add(jbxing);
/*将按钮放入容器jp4*/
jp4.add(jb1);
jp4.add(jb2);
jp4.add(jb3);
jp4.add(jbjian);
/*将按钮放入容器jp5*/
jp5.add(jb0);
jp5.add(jbdian);
jp5.add(jbjia);
jp5.add(jbdeng);
/*将按钮放入容器jp5*/
jp6.add(jbC);
/*将容器放入框架*/
c.add(jp1);
c.add(jp6);
c.add(jp2);
c.add(jp3);
c.add(jp4);
c.add(jp5);
/*设置大小并显示*/
this.setSize(300,250);
this.setVisible(true);
/*实例化一个监听器对象*/
actionDemo ad = new actionDemo();
/*为组件添加监听器*/
jbC.addActionListener(ad);
jb1.addActionListener(ad);
jb2.addActionListener(ad);
jb3.addActionListener(ad);
jb4.addActionListener(ad);
jb5.addActionListener(ad);
jb6.addActionListener(ad);
jb7.addActionListener(ad);
jb8.addActionListener(ad);
jb9.addActionListener(ad);
jb0.addActionListener(ad);
jbxing.addActionListener(ad);
jbdian.addActionListener(ad);
jbjian.addActionListener(ad);
jbjia.addActionListener(ad);
jbdeng.addActionListener(ad);
jbchu.addActionListener(ad);
}
/*事件监听器*/
class actionDemo implements ActionListener{
/*具体的功能在这里实现*/
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jbC)){
jl.setText("");
al.add("");
al.add("");
al.add("");
al.add("");
al.add("");
}
if(e.getSource().equals(jb1)){
jl.setText("");
String str=jl.getText();
str=str+"1";
jl.setText(str);
}
else if(e.getSource().equals(jb2)){
jl.setText("");
String str=jl.getText();
str=str+"2";
jl.setText(str);
}
else if(e.getSource().equals(jb3)){
String str=jl.getText();
str=str+"3";
jl.setText(str);
}
else if(e.getSource().equals(jb4)){
String str=jl.getText();
str=str+"4";
jl.setText(str);
}
else if(e.getSource().equals(jb5)){
String str=jl.getText();
str=str+"5";
jl.setText(str);
}
else if(e.getSource().equals(jb6)){
String str=jl.getText();
str=str+"6";
jl.setText(str);
}
else if(e.getSource().equals(jb7)){
String str=jl.getText();
str=str+"7";
jl.setText(str);
}
else if(e.getSource().equals(jb8)){
String str=jl.getText();
str=str+"8";
jl.setText(str);
}
else if(e.getSource().equals(jb9)){
String str=jl.getText();
str=str+"9";
jl.setText(str);
}
else if(e.getSource().equals(jb0)){
String str=jl.getText();
str=str+"0";
jl.setText(str);
}
else if(e.getSource().equals(jbdian)){
String str=jl.getText();
str=str+".";
jl.setText(str);
}
else if(e.getSource().equals(jbxing)){
al.set(0,jl.getText());
al.set(1,"xing");
jl.setText("");
}
else if(e.getSource().equals(jbchu)){
al.set(0,jl.getText());
al.set(1,"chu");
jl.setText("");
}
else if(e.getSource().equals(jbjian)){
al.set(0,jl.getText());
al.set(1,"jian");
jl.setText("");
}
else if(e.getSource().equals(jbjia)){
al.set(0,jl.getText());
al.set(1,"jia");
jl.setText("");
}
else if(e.getSource().equals(jbdeng)){
/*当点击等于号时,我首先从数组索引1处取得该元素判断他是什么运算符*/
String operator=(String) al.get(1);
System.out.println("该运算符是: "+operator);
/*当点击等于号时,从标签处获得数字是多少*/
int last = Integer.parseInt(jl.getText());
if(operator.equals("jian")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first-last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"JIAN");
jl.setText(new Long(sum).toString());
}
if(operator.equals("jia")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first+last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"JIA");
jl.setText(new Long(sum).toString());
}
if(operator.equals("chu")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first/last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"CHU");
jl.setText(new Long(sum).toString());
}
if(operator.equals("xing")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first*last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"XING");
jl.setText(new Long(sum).toString());
}
if(operator.equals("")){
/*当你在一直按等于号之前,获得标签上的数字*/
int in=Integer.parseInt(jl.getText());
System.out.println("当你在一直按等于号之前,获得标签上的数字 "+in);
/*表示当按完运算符后再按的数字号码是多少,这其实已经保存在数组里了,这个号码可以说是last变量存储的*/
String xlast=(String) al.get(2);
System.out.println("表示当按完运算符后再按的数字号码是多少 "+xlast);
int newxlast=Integer.parseInt(xlast);
long sum = 0;
if(al.get(3).equals("JIAN")){
sum=in-newxlast;
}
if(al.get(3).equals("JIA")){
sum=in+newxlast;
}
if(al.get(3).equals("CHU")){
sum=in/newxlast;
}
if(al.get(3).equals("XING")){
sum=in*newxlast;
}
System.out.println("sum表示最终的结果是 "+sum);
jl.setText(new Long(sum).toString());
}
}
}
}
public static void main(String [] args)
{
calculatorface c = new calculatorface();
}
}
这是我做的计算器,有一个缺点,就是除法不是很准确,剩下运算都是对的。
第一次计算前,要按下一下clear按钮,以后如果要重新计算的话还需要按一下clear按钮。结果才会正确。
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class calculatorface extends JFrame{
/**
每次用时都要按下clear按钮,然后就是小数点按钮我没有处理,做的不是太好,里面除运算无法计算到小数
*/
private static final long serialVersionUID = 1581029666853134020L;
ArrayList<String> al = new ArrayList<String>();
JButton jb1 = new JButton("1");
JButton jb2 = new JButton("2");
JButton jb3 = new JButton("3");
JButton jb4 = new JButton("4");
JButton jb5 = new JButton("5");
JButton jb6 = new JButton("6");
JButton jb7 = new JButton("7");
JButton jb8 = new JButton("8");
JButton jb9 = new JButton("9");
JButton jb0 = new JButton("0");
JButton jbxing = new JButton("*");
JButton jbdian = new JButton(".");
JButton jbjian = new JButton("-");
JButton jbjia = new JButton("+");
JButton jbchu = new JButton("/");
JButton jbdeng = new JButton("=");
JButton jbC = new JButton("Clear");
public JLabel jl = new JLabel("0.",JLabel.RIGHT);
public calculatorface(){
super("计算器");
init();
}
public void init(){
Container c = getContentPane();
JPanel jp1 = new JPanel();
JPanel jp2 = new JPanel();
JPanel jp3 = new JPanel();
JPanel jp4 = new JPanel();
JPanel jp5 = new JPanel();
JPanel jp6 = new JPanel();
/*设定容器的布局方式*/
jp1.setLayout(new GridLayout(1,1));
jp2.setLayout(new GridLayout(1,4));
jp3.setLayout(new GridLayout(1,4));
jp4.setLayout(new GridLayout(1,4));
jp5.setLayout(new GridLayout(1,4));
jp6.setLayout(new GridLayout(1,4));
c.setLayout(new GridLayout(6,1));
/*将文本域放入容器jp1*/
jp1.add(jl);
/*将按钮放入容器jp2*/
jp2.add(jb7);
jp2.add(jb8);
jp2.add(jb9);
jp2.add(jbchu);
/*将按钮放入容器jp3*/
jp3.add(jb4);
jp3.add(jb5);
jp3.add(jb6);
jp3.add(jbxing);
/*将按钮放入容器jp4*/
jp4.add(jb1);
jp4.add(jb2);
jp4.add(jb3);
jp4.add(jbjian);
/*将按钮放入容器jp5*/
jp5.add(jb0);
jp5.add(jbdian);
jp5.add(jbjia);
jp5.add(jbdeng);
/*将按钮放入容器jp5*/
jp6.add(jbC);
/*将容器放入框架*/
c.add(jp1);
c.add(jp6);
c.add(jp2);
c.add(jp3);
c.add(jp4);
c.add(jp5);
/*设置大小并显示*/
this.setSize(300,250);
this.setVisible(true);
/*实例化一个监听器对象*/
actionDemo ad = new actionDemo();
/*为组件添加监听器*/
jbC.addActionListener(ad);
jb1.addActionListener(ad);
jb2.addActionListener(ad);
jb3.addActionListener(ad);
jb4.addActionListener(ad);
jb5.addActionListener(ad);
jb6.addActionListener(ad);
jb7.addActionListener(ad);
jb8.addActionListener(ad);
jb9.addActionListener(ad);
jb0.addActionListener(ad);
jbxing.addActionListener(ad);
jbdian.addActionListener(ad);
jbjian.addActionListener(ad);
jbjia.addActionListener(ad);
jbdeng.addActionListener(ad);
jbchu.addActionListener(ad);
}
/*事件监听器*/
class actionDemo implements ActionListener{
/*具体的功能在这里实现*/
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(jbC)){
jl.setText("");
al.add("");
al.add("");
al.add("");
al.add("");
al.add("");
}
if(e.getSource().equals(jb1)){
jl.setText("");
String str=jl.getText();
str=str+"1";
jl.setText(str);
}
else if(e.getSource().equals(jb2)){
jl.setText("");
String str=jl.getText();
str=str+"2";
jl.setText(str);
}
else if(e.getSource().equals(jb3)){
String str=jl.getText();
str=str+"3";
jl.setText(str);
}
else if(e.getSource().equals(jb4)){
String str=jl.getText();
str=str+"4";
jl.setText(str);
}
else if(e.getSource().equals(jb5)){
String str=jl.getText();
str=str+"5";
jl.setText(str);
}
else if(e.getSource().equals(jb6)){
String str=jl.getText();
str=str+"6";
jl.setText(str);
}
else if(e.getSource().equals(jb7)){
String str=jl.getText();
str=str+"7";
jl.setText(str);
}
else if(e.getSource().equals(jb8)){
String str=jl.getText();
str=str+"8";
jl.setText(str);
}
else if(e.getSource().equals(jb9)){
String str=jl.getText();
str=str+"9";
jl.setText(str);
}
else if(e.getSource().equals(jb0)){
String str=jl.getText();
str=str+"0";
jl.setText(str);
}
else if(e.getSource().equals(jbdian)){
String str=jl.getText();
str=str+".";
jl.setText(str);
}
else if(e.getSource().equals(jbxing)){
al.set(0,jl.getText());
al.set(1,"xing");
jl.setText("");
}
else if(e.getSource().equals(jbchu)){
al.set(0,jl.getText());
al.set(1,"chu");
jl.setText("");
}
else if(e.getSource().equals(jbjian)){
al.set(0,jl.getText());
al.set(1,"jian");
jl.setText("");
}
else if(e.getSource().equals(jbjia)){
al.set(0,jl.getText());
al.set(1,"jia");
jl.setText("");
}
else if(e.getSource().equals(jbdeng)){
/*当点击等于号时,我首先从数组索引1处取得该元素判断他是什么运算符*/
String operator=(String) al.get(1);
System.out.println("该运算符是: "+operator);
/*当点击等于号时,从标签处获得数字是多少*/
int last = Integer.parseInt(jl.getText());
if(operator.equals("jian")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first-last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"JIAN");
jl.setText(new Long(sum).toString());
}
if(operator.equals("jia")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first+last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"JIA");
jl.setText(new Long(sum).toString());
}
if(operator.equals("chu")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first/last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"CHU");
jl.setText(new Long(sum).toString());
}
if(operator.equals("xing")){
/*将索引为1的元素设置为空的,以便当你连续按等于号有正确的输出*/
al.set(1,"");
/*获取第一次输入的数字*/
int first= Integer.parseInt((String)al.get(0));
long sum = first*last;
Integer inLast = new Integer(last);
String newInLast=inLast.toString();
al.set(2,newInLast);
al.set(3,"XING");
jl.setText(new Long(sum).toString());
}
if(operator.equals("")){
/*当你在一直按等于号之前,获得标签上的数字*/
int in=Integer.parseInt(jl.getText());
System.out.println("当你在一直按等于号之前,获得标签上的数字 "+in);
/*表示当按完运算符后再按的数字号码是多少,这其实已经保存在数组里了,这个号码可以说是last变量存储的*/
String xlast=(String) al.get(2);
System.out.println("表示当按完运算符后再按的数字号码是多少 "+xlast);
int newxlast=Integer.parseInt(xlast);
long sum = 0;
if(al.get(3).equals("JIAN")){
sum=in-newxlast;
}
if(al.get(3).equals("JIA")){
sum=in+newxlast;
}
if(al.get(3).equals("CHU")){
sum=in/newxlast;
}
if(al.get(3).equals("XING")){
sum=in*newxlast;
}
System.out.println("sum表示最终的结果是 "+sum);
jl.setText(new Long(sum).toString());
}
}
}
}
public static void main(String [] args)
{
calculatorface c = new calculatorface();
}
}
这是我做的计算器,有一个缺点,就是除法不是很准确,剩下运算都是对的。
第一次计算前,要按下一下clear按钮,以后如果要重新计算的话还需要按一下clear按钮。结果才会正确。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询