📄 calculatorframe.java~7~
字号:
package calculator;
import java.awt.*;
import javax.swing.*;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2007</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class CalculatorFrame
extends JFrame {
JPanel contentPane;
JTextField txtresult = new JTextField();
JButton btn7 = new JButton();
JButton btn8 = new JButton();
JButton btn9 = new JButton();
JButton btndivide = new JButton();
JButton btn4 = new JButton();
JButton btn5 = new JButton();
JButton btn6 = new JButton();
JButton btnmultiply = new JButton();
JButton btn1 = new JButton();
JButton btn2 = new JButton();
JButton btn3 = new JButton();
JButton btnminus = new JButton();
JButton btn0 = new JButton();
JButton btnclear = new JButton();
JButton btnequal = new JButton();
JButton btnplus = new JButton();
//声明
boolean flag = false;
String operand1;
String operand2;
double result;
String action;
public CalculatorFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(400, 300));
setTitle("Frame Title");
txtresult.setBounds(new Rectangle(38, 25, 301, 24));
btn7.setBounds(new Rectangle(38, 71, 63, 36));
btn7.setText("7");
btn7.addActionListener(new CalculatorFrame_btn7_actionAdapter(this));
btn8.setBounds(new Rectangle(118, 71, 63, 36));
btn8.setText("8");
btn8.addActionListener(new CalculatorFrame_btn8_actionAdapter(this));
btn9.setBounds(new Rectangle(197, 71, 63, 36));
btn9.setText("9");
btn9.addActionListener(new CalculatorFrame_btn9_actionAdapter(this));
btndivide.setBounds(new Rectangle(276, 71, 63, 36));
btndivide.setText("/");
btndivide.addActionListener(new CalculatorFrame_btndivide_actionAdapter(this));
btn4.setBounds(new Rectangle(38, 119, 63, 36));
btn4.setText("4");
btn4.addActionListener(new CalculatorFrame_btn4_actionAdapter(this));
btn5.setBounds(new Rectangle(118, 119, 63, 36));
btn5.setText("5");
btn5.addActionListener(new CalculatorFrame_btn5_actionAdapter(this));
btn6.setBounds(new Rectangle(197, 119, 63, 36));
btn6.setText("6");
btn6.addActionListener(new CalculatorFrame_btn6_actionAdapter(this));
btnmultiply.setBounds(new Rectangle(276, 119, 63, 36));
btnmultiply.setText("*");
btnmultiply.addActionListener(new CalculatorFrame_btnmultiply_actionAdapter(this));
btn1.setBounds(new Rectangle(38, 166, 63, 36));
btn1.setText("1");
btn1.addActionListener(new CalculatorFrame_btn1_actionAdapter(this));
btn2.setBounds(new Rectangle(118, 166, 63, 36));
btn2.setText("2");
btn2.addActionListener(new CalculatorFrame_btn2_actionAdapter(this));
btn3.setBounds(new Rectangle(197, 166, 63, 36));
btn3.setText("3");
btn3.addActionListener(new CalculatorFrame_btn3_actionAdapter(this));
btnminus.setBounds(new Rectangle(276, 166, 63, 36));
btnminus.setText("-");
btnminus.addActionListener(new CalculatorFrame_btnminus_actionAdapter(this));
btn0.setBounds(new Rectangle(38, 216, 63, 36));
btn0.setText("0");
btn0.addActionListener(new CalculatorFrame_btn0_actionAdapter(this));
btnclear.setBounds(new Rectangle(118, 216, 63, 36));
btnclear.setText("C");
btnclear.addActionListener(new CalculatorFrame_btnclear_actionAdapter(this));
btnequal.setBounds(new Rectangle(197, 216, 63, 36));
btnequal.setText("=");
btnequal.addActionListener(new CalculatorFrame_btnequal_actionAdapter(this));
btnplus.setBounds(new Rectangle(276, 216, 63, 36));
btnplus.setText("+");
btnplus.addActionListener(new CalculatorFrame_btnplus_actionAdapter(this));
contentPane.add(txtresult);
contentPane.add(btn7);
contentPane.add(btn8);
contentPane.add(btn9);
contentPane.add(btndivide);
contentPane.add(btn4);
contentPane.add(btn5);
contentPane.add(btn6);
contentPane.add(btnmultiply);
contentPane.add(btn1);
contentPane.add(btn2);
contentPane.add(btn3);
contentPane.add(btnminus);
contentPane.add(btnclear);
contentPane.add(btnequal);
contentPane.add(btnplus);
contentPane.add(btn0);
}
public void btn1_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn1.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn1.getActionCommand());
}
}
public void btn2_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn2.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn2.getActionCommand());
}
}
public void btn3_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn3.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn3.getActionCommand());
}
}
public void btn4_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn4.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn4.getActionCommand());
}
}
public void btn5_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn5.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn5.getActionCommand());
}
}
public void btn6_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn6.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn6.getActionCommand());
}
}
public void btn7_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn7.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn7.getActionCommand());
}
}
public void btn8_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn8.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn8.getActionCommand());
}
}
public void btn9_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn9.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn9.getActionCommand());
}
}
public void btn0_actionPerformed(ActionEvent e) {
if (flag) {
txtresult.setText(btn0.getActionCommand());
flag = false;
}
else {
txtresult.setText(txtresult.getText() + btn0.getActionCommand());
}
}
public void btnplus_actionPerformed(ActionEvent e) {
operand1 = txtresult.getText();
action = "plus";
flag = true;
}
public void btndivide_actionPerformed(ActionEvent e) {
action = "divide";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}
public void btnclear_actionPerformed(ActionEvent e) {
txtresult.setText("");
}
public void btnminus_actionPerformed(ActionEvent e) {
action = "minus";
operand1 = txtresult.getText();
txtresult.setText("");
flag = true;
}
public void btnmultiply_actionPerformed(ActionEvent e) {
action = "multiply";
operand1 = txtresult.getText();
flag = true;
}
public void btnequal_actionPerformed(ActionEvent e) {
double digit1;
double digit2;
operand2 = txtresult.getText();
if (flag == false) {
if (action.equals("divide")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 / digit2;
txtresult.setText(new Double(result).toString());
// txtresult.setText(""+result);
flag = true;
}
else if (action.equals("plus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 + digit2;
txtresult.setText("" + (int) result);
flag = true;
}
else if (action.equals("multiply")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 * digit2;
txtresult.setText("" + (int) result);
flag = true;
}
else if (action.equals("minus")) {
digit1 = Double.parseDouble(operand1);
digit2 = Double.parseDouble(operand2);
result = digit1 - digit2;
txtresult.setText("" + (int) result);
flag = true;
}
}
}
}
class CalculatorFrame_btnequal_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnequal_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnequal_actionPerformed(e);
}
}
class CalculatorFrame_btnmultiply_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnmultiply_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnmultiply_actionPerformed(e);
}
}
class CalculatorFrame_btnminus_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnminus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnminus_actionPerformed(e);
}
}
class CalculatorFrame_btnclear_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnclear_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnclear_actionPerformed(e);
}
}
class CalculatorFrame_btndivide_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btndivide_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btndivide_actionPerformed(e);
}
}
class CalculatorFrame_btnplus_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btnplus_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btnplus_actionPerformed(e);
}
}
class CalculatorFrame_btn0_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn0_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn0_actionPerformed(e);
}
}
class CalculatorFrame_btn9_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn9_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn9_actionPerformed(e);
}
}
class CalculatorFrame_btn8_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn8_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn8_actionPerformed(e);
}
}
class CalculatorFrame_btn7_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn7_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn7_actionPerformed(e);
}
}
class CalculatorFrame_btn6_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn6_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn6_actionPerformed(e);
}
}
class CalculatorFrame_btn5_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn5_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn5_actionPerformed(e);
}
}
class CalculatorFrame_btn4_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn4_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn4_actionPerformed(e);
}
}
class CalculatorFrame_btn3_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn3_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn3_actionPerformed(e);
}
}
class CalculatorFrame_btn2_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn2_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn2_actionPerformed(e);
}
}
class CalculatorFrame_btn1_actionAdapter
implements ActionListener {
private CalculatorFrame adaptee;
CalculatorFrame_btn1_actionAdapter(CalculatorFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btn1_actionPerformed(e);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -