求JAVA简易计算器代码。

做出来是这样的。(布局按钮还有功能一起说下,谢谢了)主要是功能。没分了不好意思。用Eclipse写。... 做出来是这样的。
(布局 按钮 还有功能一起说下,谢谢了)主要是功能。没分了 不好意思。
用Eclipse写。
展开
 我来答
沉西小0K
推荐于2016-10-08 · TA获得超过231个赞
知道小有建树答主
回答量:207
采纳率:0%
帮助的人:183万
展开全部
import javax.swing.SwingUtilities;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.Rectangle;
import javax.swing.JButton;
public class Calculator extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JTextField jTextField = null;
private JButton jButton = null;
private JButton jButton1 = null;
private JButton jButton2 = null;
private JButton jButton3 = null;
private JButton jButton4 = null;
private JButton jButton5 = null;
private JButton jButton6 = null;
private JButton jButton7 = null;
private JButton jButton8 = null;
private JButton jButton9 = null;
private JButton jButton10 = null;
private JButton jButton11 = null;
private JButton jButton12 = null;
private JButton jButton13 = null;
private JButton jButton14 = null;
private double first = 0;
private double second = 0;
private JButton jButton15 = null;
private JButton jButton16 = null;
private String way = "";
private double result = 0;
private char[] ch = new char[12];
private int i = 0;
/**
* This method initializes jTextField
*
* @return javax.swing.JTextField
*/
private JTextField getJTextField() {
if (jTextField == null) {
jTextField = new JTextField("0");
jTextField.setBounds(new Rectangle(60, 15, 234, 31));
jTextField.setHorizontalAlignment(JTextField.RIGHT);
}
return jTextField;
}
/**
* This method initializes jButton
*
* @return javax.swing.JButton
*/
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(62, 74, 44, 37));
jButton.setText("1");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '1';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton;
}
/**
* This method initializes jButton1
*
* @return javax.swing.JButton
*/
private JButton getJButton1() {
if (jButton1 == null) {
jButton1 = new JButton();
jButton1.setBounds(new Rectangle(119, 74, 44, 37));
jButton1.setText("2");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '2';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton1;
}
/**
* This method initializes jButton2
*
* @return javax.swing.JButton
*/
private JButton getJButton2() {
if (jButton2 == null) {
jButton2 = new JButton();
jButton2.setBounds(new Rectangle(176, 74, 44, 37));
jButton2.setText("3");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '3';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton2;
}
/**
* This method initializes jButton3
*
* @return javax.swing.JButton
*/
private JButton getJButton3() {
if (jButton3 == null) {
jButton3 = new JButton();
jButton3.setBounds(new Rectangle(62, 129, 44, 37));
jButton3.setText("4");
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '4';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton3;
}
/**
* This method initializes jButton4
*
* @return javax.swing.JButton
*/
private JButton getJButton4() {
if (jButton4 == null) {
jButton4 = new JButton();
jButton4.setBounds(new Rectangle(119, 129, 44, 37));
jButton4.setText("5");
jButton4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '5';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton4;
}
/**
* This method initializes jButton5
*
* @return javax.swing.JButton
*/
private JButton getJButton5() {
if (jButton5 == null) {
jButton5 = new JButton();
jButton5.setBounds(new Rectangle(176, 129, 44, 37));
jButton5.setText("6");
jButton5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '6';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton5;
}
/**
* This method initializes jButton6
*
* @return javax.swing.JButton
*/
private JButton getJButton6() {
if (jButton6 == null) {
jButton6 = new JButton();
jButton6.setBounds(new Rectangle(62, 184, 44, 37));
jButton6.setText("7");
jButton6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '7';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton6;
}
/**
* This method initializes jButton7
*
* @return javax.swing.JButton
*/
private JButton getJButton7() {
if (jButton7 == null) {
jButton7 = new JButton();
jButton7.setBounds(new Rectangle(119, 184, 44, 37));
jButton7.setText("8");
jButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '8';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton7;
}
/**
* This method initializes jButton8
*
* @return javax.swing.JButton
*/
private JButton getJButton8() {
if (jButton8 == null) {
jButton8 = new JButton();
jButton8.setBounds(new Rectangle(176, 184, 44, 37));
jButton8.setText("9");
jButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '9';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton8;
}
/**
* This method initializes jButton9
*
* @return javax.swing.JButton
*/
private JButton getJButton9() {
if (jButton9 == null) {
jButton9 = new JButton();
jButton9.setBounds(new Rectangle(233, 74, 44, 37));
jButton9.setText("+");
jButton9.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "+";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton9;
}
/**
* This method initializes jButton10
*
* @return javax.swing.JButton
*/
private JButton getJButton10() {
if (jButton10 == null) {
jButton10 = new JButton();
jButton10.setBounds(new Rectangle(290, 74, 44, 37));
jButton10.setText("-");
jButton10.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "-";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton10;
}
/**
* This method initializes jButton11
*
* @return javax.swing.JButton
*/
private JButton getJButton11() {
if (jButton11 == null) {
jButton11 = new JButton();
jButton11.setBounds(new Rectangle(233, 129, 44, 37));
jButton11.setText("*");
jButton11.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "*";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton11;
}
/**
* This method initializes jButton12
*
* @return javax.swing.JButton
*/
private JButton getJButton12() {
if (jButton12 == null) {
jButton12 = new JButton();
jButton12.setBounds(new Rectangle(290, 129, 44, 37));
jButton12.setText("/");
jButton12.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
first = Double.parseDouble(jTextField.getText());
way = "/";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton12;
}
/**
* This method initializes jButton13
*
* @return javax.swing.JButton
*/
private JButton getJButton13() {
if (jButton13 == null) {
jButton13 = new JButton();
jButton13.setBounds(new Rectangle(233, 184, 44, 37));
jButton13.setText("=");
jButton13.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
second = Double.parseDouble(jTextField.getText());
if (way.equals("")){
jTextField.setText(""+second);
}
if (way.equals("+")){
result = first + second;
jTextField.setText(""+result);
}
if (way.equals("-")){
result = first - second;
jTextField.setText(""+result);
}
if (way.equals("*")){
result = first * second;
jTextField.setText(""+result);
}
if(way.equals("/")){
if (second != 0){
result = first / second;
jTextField.setText(""+result);
}else{
jTextField.setText("error");
}
}
jButton16.setEnabled(true);
}
});
}
return jButton13;
}
/**
* This method initializes jButton14
*
* @return javax.swing.JButton
*/
private JButton getJButton14() {
if (jButton14 == null) {
jButton14 = new JButton();
jButton14.setBounds(new Rectangle(290, 184, 44, 37));
jButton14.setText("C");
jButton14.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jTextField.setText("0");
first = 0;
second = 0;
way = "";
ch = new char[12];
i = 0;
jButton16.setEnabled(true);
}
});
}
return jButton14;
}
/**
* This method initializes jButton15
*
* @return javax.swing.JButton
*/
private JButton getJButton15() {
if (jButton15 == null) {
jButton15 = new JButton();
jButton15.setBounds(new Rectangle(62, 231, 102, 26));
jButton15.setText("0");
jButton15.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
ch[i] = '0';
jTextField.setText(new String(ch).trim());
i++;
}
});
}
return jButton15;
}
/**
* This method initializes jButton16
*
* @return javax.swing.JButton
*/
private JButton getJButton16() {
if (jButton16 == null) {
jButton16 = new JButton();
jButton16.setBounds(new Rectangle(176, 231, 44, 26));
jButton16.setText(".");
jButton16.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
if(ch[0] == 0){
ch[i] = '0';
ch[i+1] = '.';
i++;
}else{
ch[i] = '.';
}
jTextField.setText(new String(ch).trim());
i++;
jButton16.setEnabled(false);
}
});
}
return jButton16;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Calculator thisClass = new Calculator();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
/**
* This is the default constructor
*/
public Calculator() {
super();
initialize();
}
/**
* This method initializes this
*
* @return void
*/
private void initialize() {
this.setSize(356, 298);
this.setContentPane(getJContentPane());
this.setTitle("计算器");
this.setResizable(false);
}
/**
* This method initializes jContentPane
*
* @return javax.swing.JPanel
*/
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJTextField(), null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJButton1(), null);
jContentPane.add(getJButton2(), null);
jContentPane.add(getJButton3(), null);
jContentPane.add(getJButton4(), null);
jContentPane.add(getJButton5(), null);
jContentPane.add(getJButton6(), null);
jContentPane.add(getJButton7(), null);
jContentPane.add(getJButton8(), null);
jContentPane.add(getJButton9(), null);
jContentPane.add(getJButton10(), null);
jContentPane.add(getJButton11(), null);
jContentPane.add(getJButton12(), null);
jContentPane.add(getJButton13(), null);
jContentPane.add(getJButton14(), null);
jContentPane.add(getJButton15(), null);
jContentPane.add(getJButton16(), null);
}
return jContentPane;
}
} // @jve:decl-index=0:visual-constraint="437,27"

哥们!这是用eclipse中的visual editor做的,所以代码比较多,复制粘贴不方便,我可以发你邮箱。你把代码打上去,一样能运行的!!!
fhhjnc
2018-05-24
知道答主
回答量:1
采纳率:0%
帮助的人:867
展开全部
首先定义布局用网格布局管理器,然后定义各个按钮,将按钮添加到面版中,最后对每个按钮进行监听,需要设置两种监听,一种对数字进行监听,一种对运算符及命令进行监听。同时按照按钮点击确认监听如果是运算符监听,就调用运算的功能,最后两面板添加到JFrame窗体中,最后写一个测试类就好了考虑到第一次输入可能会是为负,在设置一个标识去处理
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式