import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Counter extends JFrame
{
//实例化2个显示屏
JTextField Jtextfield1=new JTextField(20);
JTextField Jtextfield2=new JTextField(5);
//实例化4个面板
JPanel Jpanel1=new JPanel();
JPanel Jpanel2=new JPanel();
JPanel Jpanel3=new JPanel();
JPanel Jpanel4=new JPanel();
//实例化布局管理器
GridLayout GLayout3=new GridLayout(1,3);
GridLayout GLayout4=new GridLayout(4,5);
//实例化按钮
JButton[][] b=new JButton[][]{{new JButton("MC"),
new JButton("MR"),
new JButton("MS"),
new JButton("M+")},
{new JButton("Backspace"),
new JButton("CE"),
new JButton("C")},
{new JButton("0"),
new JButton("1"),
new JButton("2"),
new JButton("3"),
new JButton("4"),
new JButton("5"),
new JButton("6"),
new JButton("7"),
new JButton("8"),
new JButton("9"),
new JButton("/"),
new JButton("*"),
new JButton("-"),
new JButton("+"),
new JButton("sqrt"),
new JButton("%"),
new JButton("1/x"),
new JButton("="),
new JButton("+/-")}
};
ActionListener action=new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
Jtextfield1.setText(" ");
}
};
public Counter(String str)
{
super(str);
setLayout(null);
setJPanel1();
setJPanel2();
add(Jpanel1);
add(Jpanel2);
for(int i=0;i<b.length;i++)
{
for(int j=0;j<b[i].length;j++)
{
b[i][j].addActionListener(action);
}
}
}
public void setJPanel1()
{
GridLayout GLayout1=new GridLayout(1,1);
Jpanel1.setLayout(GLayout1);
Jpanel1.add(Jtextfield1);
Jtextfield1.setHorizontalAlignment(JTextField.RIGHT );
Jpanel1.setBounds(10,3,252,22);
}
public void setJPanel2()
{
GridLayout GLayout2=new GridLayout(5,1,10,10);
Jpanel2.setLayout(GLayout2);
Jpanel2.add(b[0][0]);
Jpanel2.add(b[0][1]);
Jpanel2.add(b[0][2]);
Jpanel2.add(b[0][3]);
Jpanel2.setBounds(10,40,30,200);
}
public static void main(String[] args)
{
Counter counter=new Counter("计算器");
counter.setSize(280,250);
counter.setLocation(300,250);
counter.setVisible(true);
counter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}