Java语言计算器界面实现


import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class MyFrame extends JFrame {
private JPanel pan;
private JButton[] btn;
private JTextField textField;


public MyFrame(String s) {
super(s);
setSize(700, 400);
setLocationRelativeTo(null);


JPanel contentPane = new JPanel();
setContentPane(contentPane);
contentPane.setLayout(new BorderLayout());


textField = new JTextField(40);
contentPane.add(textField, BorderLayout.NORTH);


pan = new JPanel(new GridLayout(4, 4));
contentPane.add(pan);
btn = new JButton[16];
btn[0] = new JButton("7");
btn[1] = new JButton("8");
btn[2] = new JButton("9");
btn[3] = new JButton("/");
btn[4] = new JButton("4");
btn[5] = new JButton("5");
btn[6] = new JButton("6");
btn[7] = new JButton("*");
btn[8] = new JButton("1");
btn[9] = new JButton("2");
btn[10] = new JButton("3");
btn[11] = new JButton("-");
btn[12] = new JButton("0");
btn[13] = new JButton(".");
btn[14] = new JButton("=");
btn[15] = new JButton("+");
for (int x = 0; x < 16; x++) {
pan.add(btn[x]);
}


setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
}

}

public class MyFraneDemo {
public static void main(String[] args) {
MyFrame mf = new MyFrame("计算器");
}
}

你可能感兴趣的:(Java基础)