//CalculFrame.java
import java.awt.*;
import javax.swing.*;
public class CalculFrame extends JPanel {
JButton btn[];
private GridLayout gridLayout;
CalculFrame(){
gridLayout=new GridLayout(4,4);
setLayout(gridLayout);
btn=new JButton[16];
String s[]={"7","8","9","/","4","5","6","*","1","2","3","-","0",".","=","+"};
for(int i=0;i<16;i++){
btn[i]=new JButton(s[i]+" ");
add(btn[i]);
}
}
}
//CalFrame.java
import java.awt.*;
import javax.swing.*;
public class CalFrame extends JFrame {
private JTextField result;
private BorderLayout bLayout;
CalFrame(String s){
super(s);
setSize(250,200);
setLocationRelativeTo(null);
JPanel contentPane=new JPanel();
setContentPane(contentPane);
CalculFrame cal=new CalculFrame();
bLayout=new BorderLayout();
contentPane.setLayout(bLayout);
result=new JTextField(20);
contentPane.add(result, BorderLayout.NORTH);
contentPane.add(cal);
this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setVisible(true);
}
}
//Frame.java
public class Frame {
public static void main(String[] args) {
// TODO Auto-generated method stub
CalFrame frm=new CalFrame("计算器");
}
}