1.
代码
1 package Person; 2 import javax.swing.*; 3 import java.awt.*; 4 import java.awt.event.ActionEvent; 5 import java.awt.event.ActionListener; 6 7 public class Calculator { 8 JFrame f; 9 JPanel p; 10 JButton [] b; 11 String[] title = {"7","8","9","/", 12 "4","5","6","*", 13 "1","2","3","-", 14 "0","c","=","+"}; 15 JTextField t; 16 17 public Calculator(){ 18 f = new JFrame("计算器"); 19 p = new JPanel(); 20 t = new JTextField(); 21 b = new JButton[16]; 22 23 f.setVisible(true); 24 f.setLocationRelativeTo(null); 25 f.setSize(450,450); 26 f.add(t,BorderLayout.NORTH); 27 t.setPreferredSize(new Dimension(15,50)); //设置文本框长、宽 28 p.setLayout(new GridLayout(4,4)); 29 f.add(p,BorderLayout.CENTER); 30 for(int i=0;i<16;i++){ 31 b[i] = new JButton(title[i]); 32 p.add(b[i]); 33 } 34 35 } 36 public static void main(String args[]){ 37 new Calculator(); 38 } 39 }
运行界面
2.
代码
运行界面
原文:https://www.cnblogs.com/smyhhh/p/10928263.html