永发信息网

用java语言,编写一个计算器

答案:6  悬赏:50  手机版
解决时间 2021-03-15 14:43
  • 提问者网友:轮囘Li巡影
  • 2021-03-15 04:39
用java语言,编写一个计算器
最佳答案
  • 五星知识达人网友:十鸦
  • 2021-03-15 04:53
有一个现成的.你看看吧

import java.awt.event.ActionEvent;

public class Application extends JFrame {

protected String str = "";
protected boolean isChar = true;
protected boolean isEqual = false;
protected JTextField textField;

public Application() {
Listener listerner = new Listener(this);
getContentPane().setLayout(null);

JButton button = new JButton("7");
button.addActionListener(listerner);
button.setBounds(12, 69, 43, 27);
getContentPane().add(button);

textField = new JTextField();
textField.setText("0");
textField.setEditable(false);
textField.setHorizontalAlignment(JTextField.RIGHT);
textField.setBounds(12, 22, 377, 27);
getContentPane().add(textField);
textField.setColumns(10);

JButton button_1 = new JButton("8");
button_1.addActionListener(listerner);
button_1.setBounds(103, 69, 43, 27);
getContentPane().add(button_1);

JButton button_2 = new JButton("9");
button_2.addActionListener(listerner);
button_2.setBounds(182, 69, 43, 27);
getContentPane().add(button_2);

JButton button_3 = new JButton("4");
button_3.addActionListener(listerner);
button_3.setBounds(12, 106, 43, 27);
getContentPane().add(button_3);

JButton button_4 = new JButton("5");
button_4.addActionListener(listerner);
button_4.setBounds(103, 106, 43, 27);
getContentPane().add(button_4);

JButton button_5 = new JButton("6");
button_5.addActionListener(listerner);
button_5.setBounds(182, 106, 43, 27);
getContentPane().add(button_5);

JButton button_6 = new JButton("1");
button_6.addActionListener(listerner);
button_6.setBounds(12, 143, 43, 27);
getContentPane().add(button_6);

JButton button_7 = new JButton("2");
button_7.addActionListener(listerner);
button_7.setBounds(103, 143, 43, 27);
getContentPane().add(button_7);

JButton button_8 = new JButton("3");
button_8.addActionListener(listerner);
button_8.setBounds(182, 143, 43, 27);
getContentPane().add(button_8);

JButton button_9 = new JButton("+");
button_9.addActionListener(listerner);
button_9.setBounds(269, 72, 43, 27);
getContentPane().add(button_9);

JButton button_10 = new JButton("-");
button_10.addActionListener(listerner);
button_10.setBounds(346, 72, 43, 27);
getContentPane().add(button_10);

JButton button_11 = new JButton("*");
button_11.addActionListener(listerner);
button_11.setBounds(269, 109, 43, 27);
getContentPane().add(button_11);

JButton button_12 = new JButton("/");
button_12.addActionListener(listerner);
button_12.setBounds(346, 109, 43, 27);
getContentPane().add(button_12);

JButton button_13 = new JButton("=");
button_13.addActionListener(listerner);
button_13.setBounds(346, 143, 43, 27);
getContentPane().add(button_13);

JButton button_14 = new JButton("0");
button_14.addActionListener(listerner);
button_14.setBounds(103, 180, 43, 27);
getContentPane().add(button_14);

JButton btnReset = new JButton("reset");
btnReset.addActionListener(listerner);
btnReset.setBounds(269, 180, 118, 27);
getContentPane().add(btnReset);

JButton button_15 = new JButton(".");
button_15.addActionListener(listerner);
button_15.setBounds(269, 146, 43, 27);
getContentPane().add(button_15);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(442, 260);
this.setLocationRelativeTo(null);
this.setVisible(true);
}

public static void main(String[] args) {
new Application();
}
}

class Listener implements ActionListener {
private Application app = null;

public Listener(Application app) {
this.app = app;
}

public void actionPerformed(ActionEvent e) {
String value = e.getActionCommand();
if (value.matches("[0-9.]")) {
if (app.isChar) {
app.textField.setText("");
app.isChar = false;
}
if (app.isEqual && app.str.matches("[0-9.]*")) {
app.str = "";
app.isEqual = false;
}
app.str += value;
app.textField.setText(app.textField.getText() + value);
} else if (value.matches("[\\+\\-\\*/]")) {
if (!app.str.substring(app.str.length() - 1)
.matches("[\\+\\-\\*/]")) {
app.str += value;
app.isChar = true;
}

} else if ("=".equals(value)) {
app.isEqual = true;
if (app.str.substring(app.str.length() - 1).matches("[\\+\\-]")) {
app.str += "0";
} else if (app.str.substring(app.str.length() - 1)
.matches("[\\*/]")) {
app.str += "1";
}
Interpreter bsh = new Interpreter();
String obj = null;
try {
obj = bsh.eval(app.str).toString();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}

System.out.println(app.str);
app.textField.setText(obj);
app.str = obj;
app.isChar = true;
} else {
app.str = "";
app.textField.setText("0");
}
}
}
全部回答
  • 1楼网友:神的生死簿
  • 2021-03-15 10:38
sgdfg
  • 2楼网友:动情书生
  • 2021-03-15 10:24
网上很多现成代码,稍微搜索一下就有了,无需提问。
  • 3楼网友:琴狂剑也妄
  • 2021-03-15 08:47
给邮箱才能发啊 已经发送了~请查收
  • 4楼网友:三千妖杀
  • 2021-03-15 07:09
和你大概说一下思路吧!这样的代码百度一下很多的! 先创建一个Container这个的对象!这个就相当于是一个容器,然后使用布局管理器推荐使用网格布局!计算器么上面肯定要有文本框显示的!new一个JtestField的对象放入容器中,然后就用事件监听,监听窗体上面的每一个按键!
  • 5楼网友:枭雄戏美人
  • 2021-03-15 05:48
package Calculater; import javax.swing.JButton; import javax.swing.BorderFactory; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JFrame; public class MyCalculater extends JFrame { JTextField text; public MyCalculater(){ text=new JTextField(10); text.setEditable(false); text.setBorder(BorderFactory.createLineBorder(Color.red,2)); setDefaultCloseOperation(EXIT_ON_CLOSE); JPanel numPa=new JPanel(); JPanel opePa=new JPanel(); Container c=getContentPane(); numPa.setLayout(new GridLayout(4,3,2,2)); opePa.setLayout(new GridLayout(4,1,2,2)); JButton numButt[]=new JButton[12]; JButton opeButt[]=new JButton[8]; for(int i=0;i<10;i++){ numButt[i]=new JButton(new Integer(i).toString()); } numButt[10]=new JButton("."); numButt[11]=new JButton("CE"); opeButt[0]=new JButton("+"); opeButt[1]=new JButton("-"); opeButt[2]=new JButton("x"); opeButt[3]=new JButton("/"); opeButt[4]=new JButton("sqr"); opeButt[5]=new JButton("pow"); opeButt[6]=new JButton("+/-"); opeButt[7]=new JButton("="); for(int i=0;i<12;i++){ numPa.add(numButt[i]); } ButtonListener bu=new ButtonListener(); for(int i=0;i<12;i++){ numButt[i].addActionListener(bu); } for(int i=0;i<8;i++){ opePa.add(opeButt[i]); } for(int i=0;i<8;i++){ opeButt[i].addActionListener(bu); } c.add(text,BorderLayout.NORTH); c.add(numPa,BorderLayout.WEST); c.add(opePa,BorderLayout.EAST); pack(); setResizable(false); setVisible(true); } public static void main(String[] args) { new MyCalculater(); } class ButtonListener implements ActionListener{ private String s2=""; private boolean deter=false; private double total=0.0; private String totalStirng=""; int flag=0; public void actionPerformed(ActionEvent e) { String s1=text.getText(); String s=e.getActionCommand(); if(s.equals(".")){ text.setText(s1+s); } else if(s.equals("+/-")){ text.setText("-"+s1); } else if(Character.isDigit(s.charAt(0))){ if(flag==1){ text.setText(s); flag--; }else{ text.setText(s1+s); } } else if(s.equals("CE")){ text.setText("0"); total=0.0; totalStirng=""; s1="0"; s2=""; s=""; flag=0; deter=false; } else { if(!deter){ deter=true; totalStirng=text.getText(); flag++; s2=s; } else { double s0=Double.parseDouble(totalStirng); double s10=Double.parseDouble(s1); if(s2.equals("+")){ total=s0+s10; } else if(s2.equals("-")){ total=s0-s10; } else if(s2.equals("x")){ total=s0*s10; } else if(s2.equals("/")){ total=s0/s10; } else if(s2.equals("pow")){ total=Math.pow(s0, s10); } else if(s2.equals("sqr")){ total=Math.sqrt(s10); } totalStirng=Double.toString(total); text.setText(totalStirng); s2=s; if(flag==0){ flag++; } } } } } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯