📄 simplecalculator.java~47~
字号:
package simplecalculator;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.XYLayout;
import com.borland.jbcl.layout.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleCalculator
extends JFrame {
JPanel contentPane;
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
JButton jButton5 = new JButton();
JButton jButton6 = new JButton();
JButton jButton7 = new JButton();
JButton jButton8 = new JButton();
JButton jButton9 = new JButton();
JButton jButton10 = new JButton();
JButton jButton11 = new JButton();
JButton jButton12 = new JButton();
JButton jButton13 = new JButton();
JButton jButton14 = new JButton();
JButton jButton15 = new JButton();
JButton jButton16 = new JButton();
JButton jButton17 = new JButton();
JButton jButton18 = new JButton();
JButton jButton19 = new JButton();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JTextField jTextField1 = new JTextField();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
FlowLayout flowLayout3 = new FlowLayout();
FlowLayout flowLayout4 = new FlowLayout();
FlowLayout flowLayout5 = new FlowLayout();
public SimpleCalculator() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
}
catch (Exception exception) {
exception.printStackTrace();
}
}
//*—*—添加number(int)方法,为数字按钮事件所调用
boolean end;//增加的
String str;//增加的
public void number(int i)
{
String s;//=null;
s=String.valueOf(i);
if(end)
{//如果数字输入结束,则将文本框置零,重新输入
jTextField1.setText("0");
end=false;
}
if((jTextField1.getText()).equals("0"))
{//如果文本框的内容为零,则覆盖文本框的内容
jTextField1.setText(s);
}
else
{//如果文本框的内容不为零,则在内容后添加数字
String str=jTextField1.getText()+s;//加了String
jTextField1.setText(str);
}
}
//*—*—添加sign(int s)方法,为运算符按钮事件所调用
boolean add,sub,mul,div;
double num1;
public void sign(int s)
{
if(s==1)
{
add=true;
sub=false;
mul=false;
div=false;
}
else if(s==2)
{
add=false;
sub=true;
mul=false;
div=false;
}
else if(s==3)
{
add=false;
sub=false;
mul=true;
div=false;
}
else
{
add=false;
sub=false;
mul=false;
div=true;
}
num1=Double.parseDouble(jTextField1.getText());// 增加了double
end=true;
}
//=Double.parseDouble(jTextField1.getText());// 增加了double
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
contentPane.setBackground(Color.lightGray);//增加
setSize(new Dimension(245, 300));
setTitle("一个简单的计算器");
jButton1.setForeground(Color.blue);
jButton1.setText("9");
jButton1.addActionListener(new SimpleCalculator_jButton1_actionAdapter(this));
jButton2.setForeground(Color.blue);
jButton2.setText("8");
jButton2.addActionListener(new SimpleCalculator_jButton2_actionAdapter(this));
jButton3.setForeground(Color.blue);
jButton3.setText("7");
jButton3.addActionListener(new SimpleCalculator_jButton3_actionAdapter(this));
jButton4.setForeground(Color.magenta);
jButton4.setPreferredSize(new Dimension(39, 25));
jButton4.setText("/");
jButton4.addActionListener(new SimpleCalculator_jButton4_actionAdapter(this));
jButton5.setForeground(Color.blue);
jButton5.setText("6");
jButton5.addActionListener(new SimpleCalculator_jButton5_actionAdapter(this));
jButton6.setForeground(Color.blue);
jButton6.setText("5");
jButton6.addActionListener(new SimpleCalculator_jButton6_actionAdapter(this));
jButton7.setForeground(Color.blue);
jButton7.setText("4");
jButton7.addActionListener(new SimpleCalculator_jButton7_actionAdapter(this));
jButton8.setForeground(Color.magenta);
jButton8.setText("×");
jButton8.addActionListener(new SimpleCalculator_jButton8_actionAdapter(this));
jButton9.setForeground(Color.blue);
jButton9.setPreferredSize(new Dimension(39, 25));
jButton9.setText("3");
jButton9.addActionListener(new SimpleCalculator_jButton9_actionAdapter(this));
jButton10.setForeground(Color.blue);
jButton10.setText("2");
jButton10.addActionListener(new SimpleCalculator_jButton10_actionAdapter(this));
jButton11.setForeground(Color.blue);
jButton11.setText("1");
jButton11.addActionListener(new SimpleCalculator_jButton11_actionAdapter(this));
jButton12.setForeground(Color.magenta);
jButton12.setPreferredSize(new Dimension(43, 25));
jButton12.setText("—");
jButton12.addActionListener(new SimpleCalculator_jButton12_actionAdapter(this));
jButton13.setForeground(Color.blue);
jButton13.setText("0");
jButton13.addActionListener(new SimpleCalculator_jButton13_actionAdapter(this));
jButton14.setForeground(Color.blue);
jButton14.setPreferredSize(new Dimension(39, 25));
jButton14.setText("-");
jButton14.addActionListener(new SimpleCalculator_jButton14_actionAdapter(this));
jButton15.setForeground(Color.blue);
jButton15.setPreferredSize(new Dimension(39, 25));
jButton15.setText(".");
jButton15.addActionListener(new SimpleCalculator_jButton15_actionAdapter(this));
jButton16.setForeground(Color.magenta);
jButton16.setText("+");
jButton16.addActionListener(new SimpleCalculator_jButton16_actionAdapter(this));
jButton17.setForeground(Color.red);
jButton17.setPreferredSize(new Dimension(55, 25));
jButton17.setText("清零");
jButton17.addActionListener(new SimpleCalculator_jButton17_actionAdapter(this));
jButton18.setForeground(Color.red);
jButton18.setText("重置");
jButton18.addActionListener(new SimpleCalculator_jButton18_actionAdapter(this));
jButton19.setForeground(Color.magenta);
jButton19.setPreferredSize(new Dimension(50, 25));
jButton19.setText("=");
jButton19.addActionListener(new SimpleCalculator_jButton19_actionAdapter(this));
jPanel1.setBounds(new Rectangle(19, 104, 196, 35));
jPanel1.setBackground(new Color(192, 192, 255));//增加
jPanel1.setLayout(flowLayout4);
jPanel2.setBounds(new Rectangle(19, 147, 195, 32));
jPanel2.setBackground(new Color(192, 192, 255));//增加
jPanel2.setLayout(flowLayout5);
jPanel3.setBounds(new Rectangle(19, 186, 196, 35));
jPanel3.setBackground(new Color(192, 192, 255));//增加
jPanel3.setLayout(flowLayout3);
jPanel4.setBounds(new Rectangle(19, 226, 196, 36));
jPanel4.setBackground(new Color(192, 192, 255));//增加
jPanel4.setLayout(flowLayout1);
jPanel5.setBounds(new Rectangle(18, 62, 194, 35));
jPanel5.setBackground(new Color(192, 192, 255));//增加
jPanel5.setLayout(flowLayout2);
jTextField1.setEditable(false);
jTextField1.setText("0");
jTextField1.setBounds(new Rectangle(17, 18, 196, 28));
flowLayout3.setHgap(3);
jPanel2.add(jButton5);
jPanel2.add(jButton6);
jPanel2.add(jButton7);
jPanel2.add(jButton8);
contentPane.add(jPanel1);
contentPane.add(jPanel4);
jPanel5.add(jButton17);
jPanel5.add(jButton18);
jPanel5.add(jButton19);
contentPane.add(jTextField1);
contentPane.add(jPanel5);
jPanel3.add(jButton9);
jPanel3.add(jButton10);
jPanel3.add(jButton11);
jPanel3.add(jButton12);
contentPane.add(jPanel2);
jPanel1.add(jButton1);
jPanel1.add(jButton2);
jPanel1.add(jButton3);
jPanel1.add(jButton4);
jPanel4.add(jButton13, null);
jPanel4.add(jButton14, null);
jPanel4.add(jButton15, null);
jPanel4.add(jButton16, null);
contentPane.add(jPanel3);
}
public void jButton1_actionPerformed(ActionEvent e) {
number(9);
}
public void jButton2_actionPerformed(ActionEvent e) {
number(8);
}
public void jButton3_actionPerformed(ActionEvent e) {
number(7);
}
public void jButton5_actionPerformed(ActionEvent e) {
number(6);
}
public void jButton6_actionPerformed(ActionEvent e) {
number(5);
}
public void jButton7_actionPerformed(ActionEvent e) {
number(4);
}
public void jButton9_actionPerformed(ActionEvent e) {
number(3);
}
public void jButton10_actionPerformed(ActionEvent e) {
number(2);
}
public void jButton11_actionPerformed(ActionEvent e) {
number(1);
}
public void jButton13_actionPerformed(ActionEvent e) {
number(0);
}
public void jButton4_actionPerformed(ActionEvent e) {
sign(4);
}
public void jButton16_actionPerformed(ActionEvent e) {
sign(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -