calculator.java

来自「这是一个java程序」· Java 代码 · 共 75 行

JAVA
75
字号
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 + =
减小字号Ctrl + -
显示快捷键?