📄 caculator.java
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* @author 李政劭
* 一个简单地计算器
*/
public class Caculator extends JPanel
{
private static final long serialVersionUID = 1L;
private JButton buttonDvision,buttonPlus,buttonMinus;
private JButton buttonD,buttonEqual,buttonNC,buttonMupl;
private JButton[] buttons = new JButton[10];
private JTextField field;
private String x = "0";
private String y = "0";
private String numbers = "";
private String symbol;
private double answer = 0;
private boolean exist = false;
public static void main(String args[])
{
GUI();
}
public static void GUI()
{
JFrame frame = new JFrame("caculator");
frame.setContentPane(new Caculator());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
public Caculator()
{
JPanel panelLeft = new JPanel();
JPanel panelRight = new JPanel();
JPanel panelUp = new JPanel();
for(int i = 9 ; i >= 0 ; i--)
{
int j = i;
if(i % 3 == 0 && i >= 3)
{
j = j - 2;
}
if((i + 2) % 3 == 0 )
{
j =j + 2;
}
buttons[i] = new JButton("" + j);
panelLeft.add(buttons[i]);
}
panelLeft.setLayout(new GridLayout(4, 3));
panelRight.setLayout(new GridLayout(4, 1));
buttonD = new JButton(".");
buttonEqual = new JButton("=");
buttonPlus = new JButton("+");
buttonMinus = new JButton("-");
buttonMupl = new JButton("*");
buttonDvision = new JButton("%");
buttonNC = new JButton("NC");
field = new JTextField("0.0",14);
field.setVisible(true);
field.setEditable(false);
for(int i = 0 ; i < 10 ; i++)
{
buttons[i].addActionListener(new ListenerNumber());
}
buttonD.addActionListener(new ListenerD());
buttonEqual.addActionListener(new ListenerEqual());
buttonPlus.addActionListener(new ListenerSymbol());
buttonMinus.addActionListener(new ListenerSymbol());
buttonMupl.addActionListener(new ListenerSymbol());
buttonDvision.addActionListener(new ListenerSymbol());
buttonNC.addActionListener(new ListenerNC());
panelLeft.add(buttonD);
panelLeft.add(buttonEqual);
panelRight.add(buttonPlus);
panelRight.add(buttonMinus);
panelRight.add(buttonMupl);
panelRight.add(buttonDvision);
panelUp.add(field);
panelUp.add(buttonNC);
setLayout(new BorderLayout());
add(panelLeft,BorderLayout.CENTER);
add(panelRight,BorderLayout.EAST);
add(panelUp,BorderLayout.NORTH);
}
class ListenerNumber implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() instanceof JButton)
{
JButton actButton = (JButton)(event.getSource());
numbers = numbers + actButton.getText();
field.setText(numbers);
}
}
}
class ListenerD implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(exist == false && numbers != "")
{
numbers+=".";
field.setText(numbers);
exist = true;
}
}
}
class ListenerSymbol implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
if(event.getSource() instanceof JButton)
{
JButton actButton = (JButton)(event.getSource());
symbol = actButton.getText();
x = field.getText();
field.setText("");
numbers = "";
}
}
}
class ListenerEqual implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
double a;
double b;
y = field.getText();
a = Double.parseDouble(x);
b = Double.parseDouble(y);
if(symbol == "+")
{
answer = a + b;
}
else if(symbol == "-")
{
answer = a - b;
}
else if(symbol == "*")
{
answer = a * b;
}
else if(symbol == "%")
{
answer = a / b;
}
else
{
answer = b;
}
field.setText("" + answer);
exist = false;
numbers = "";
answer = 0;
symbol = "";
x = "0";
y = "0";
}
catch(NumberFormatException e )
{
exist = false;
numbers = "";
field.setText("0.0");
answer = 0;
symbol = "";
x = "0";
y = "0";
}
}
}
class ListenerNC implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
symbol = "";
x = "0";
y = "0";
exist = false;
numbers = "";
field.setText("0.0");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -