📄 mysimplecal.java
字号:
/**
This program is a simple calculator that does addition and substraction.
Do try to enhance the program by :
(1) Add in division and multiplication features.
(2) Using Button arrays for better efficiency.
(3) Improve error handling.
**/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class mySimpleCal extends JFrame implements ActionListener{
private JTextField jtext = new JTextField("");
private JButton b1 = new JButton("1");
private JButton b2 = new JButton("2");
private JButton b3 = new JButton("3");
private JButton b4 = new JButton("4");
private JButton b5 = new JButton("5");
private JButton b6 = new JButton("6");
private JButton b7 = new JButton("7");
private JButton b8 = new JButton("8");
private JButton b9 = new JButton("9");
private JButton b0 = new JButton("0");
private JButton badd = new JButton("+");
private JButton bsub = new JButton("-");
private JButton beq = new JButton("=");
private JButton bclr = new JButton("C");
private int lastResult = 0;
private String process;
private boolean next = false;
public mySimpleCal(){
Container container = getContentPane();
/** creating the text panel for calculator **/
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(1,1));
p1.add(jtext);
/** creating buttons pad for calculator **/
JPanel p2 = new JPanel();
p2.setLayout(new GridLayout(4,3,1,1));
p2.add(b1);
p2.add(b2);
p2.add(b3);
p2.add(b4);
p2.add(b5);
p2.add(b6);
p2.add(b7);
p2.add(b8);
p2.add(b9);
p2.add(badd);
p2.add(b0);
p2.add(bsub);
//Create a listener for each button
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
b6.addActionListener(this);
b7.addActionListener(this);
b8.addActionListener(this);
b9.addActionListener(this);
b0.addActionListener(this);
badd.addActionListener(this);
bsub.addActionListener(this);
beq.addActionListener(this);
bclr.addActionListener(this);
/** creating result button **/
JPanel p3 = new JPanel();
p3.setLayout(new GridLayout(1,2));
p3.add(beq);
p3.add(bclr);
container.add(p1, BorderLayout.NORTH);
container.add(p2, BorderLayout.CENTER);
container.add(p3, BorderLayout.SOUTH);
}
public static void main(String[] args) {
mySimpleCal frame = new mySimpleCal();
frame.setTitle("My Simple Calculator");
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,200);
frame.setVisible(true);
}
/** This method will be invoked when a button is clicked **/
public void actionPerformed(ActionEvent e)
{
if (next == true)
{
jtext.setText("");
next = false;
}
if (e.getSource()==b1)
jtext.setText(jtext.getText() + "1");
else if (e.getSource()==b2)
jtext.setText(jtext.getText() + "2");
else if (e.getSource()==b3)
jtext.setText(jtext.getText() + "3");
else if (e.getSource()==b4)
jtext.setText(jtext.getText() + "4");
else if (e.getSource()==b5)
jtext.setText(jtext.getText() + "5");
else if (e.getSource()==b6)
jtext.setText(jtext.getText() + "6");
else if (e.getSource()==b7)
jtext.setText(jtext.getText() + "7");
else if (e.getSource()==b8)
jtext.setText(jtext.getText() + "8");
else if (e.getSource()==b9)
jtext.setText(jtext.getText() + "9");
else if (e.getSource()==b0)
{
/** Disallow preceding zero **/
if (jtext.getText().equals("") == false)
jtext.setText(jtext.getText() + "0");
}
else if (e.getSource()==badd)
{
process = "+";
String s = jtext.getText();
lastResult = Integer.parseInt(s);
next = true;
}
else if (e.getSource()==bsub)
{
process = "-";
String s = jtext.getText();
lastResult = Integer.parseInt(s);
next = true;
}
else if (e.getSource()==beq)
{
String s = jtext.getText();
int i = Integer.parseInt(s);
if (process.equals("+") == true)
lastResult = lastResult + i;
if (process.equals("-") == true)
lastResult = lastResult - i;
jtext.setText("" + lastResult);
process = "";
lastResult = 0;
}
else if (e.getSource()==bclr)
{
process = "";
next = false;
jtext.setText("");
lastResult = 0;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -