📄 calculator.java
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Calculator implements ActionListener{
JFrame jf = new JFrame("Calculator");
JTextField tf = new JTextField();
public void init(){
Container c = jf.getContentPane();
tf.setHorizontalAlignment(JTextField.RIGHT);
c.add(tf,"North");
JPanel pnl=new JPanel();
c.add(pnl,"Center");
pnl.setLayout(new GridLayout(4,4));
JButton b=new JButton("1");
b.addActionListener(this);
pnl.add(b);
b=new JButton("2");
b.addActionListener(this);
pnl.add(b);
b=new JButton("3");
b.addActionListener(this);
pnl.add(b);
b=new JButton("+");
b.addActionListener(this);
pnl.add(b);
b=new JButton("4");
b.addActionListener(this);
pnl.add(b);
b=new JButton("5");
b.addActionListener(this);
pnl.add(b);
b=new JButton("6");
b.addActionListener(this);
pnl.add(b);
b=new JButton("-");
b.addActionListener(this);
pnl.add(b);
b=new JButton("7");
b.addActionListener(this);
pnl.add(b);
b=new JButton("8");
b.addActionListener(this);
pnl.add(b);
b=new JButton("9");
b.addActionListener(this);
pnl.add(b);
b=new JButton("*");
b.addActionListener(this);
pnl.add(b);
b=new JButton("0");
b.addActionListener(this);
pnl.add(b);
b=new JButton(".");
b.addActionListener(this);
pnl.add(b);
b=new JButton("=");
b.addActionListener(this);
pnl.add(b);
b=new JButton("\\");
b.addActionListener(this);
pnl.add(b);
jf.setSize(200,300);
jf.setVisible(true);
}
public void actionPerformed(ActionEvent e){
tf.setText(tf.getText()+e.getActionCommand());
}
public static void main(String [] args){
new Calculator().init();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -