importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Container;importjava.awt.GridLayout;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;im...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
class JiSuanQiQi {
public static void main(String[] args) {
JFrame jf=new JFrame("计算器");
Container con=new Container();
JTextField text=new JTextField(30);
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JButton[] btnArray = new JButton[10];
for(int i = 0;i < btnArray.length;i++){
btnArray[i] = new JButton(String.valueOf(i));
p1.add(btnArray[i]);
btnArray[i].setSize(10,20);
}
JButton jia =new JButton("+");
JButton jian =new JButton("-");
JButton cheng=new JButton("*");
JButton chu =new JButton("/");
JButton eql =new JButton("=");
JButton point=new JButton(".");
JButton c =new JButton("c");
p2.add(jia);
p2.add(jian);
p2.add(cheng);
p2.add(chu);
p2.add(eql);
p2.add(point);
text.setHorizontalAlignment(JTextField.RIGHT);
p3.setLayout(new BorderLayout(0,3));
p3.add(text,BorderLayout.CENTER);
p3.add(c,BorderLayout.EAST);
p1.setLayout(new GridLayout(4,3,3,3));
p1.setSize(130,150);
p1.setLocation(20,80);
p2.setLayout(new GridLayout(3,2,3,3));
p2.setSize(100,80);
p2.setLocation(220,80);
p3.setSize(300,20);
p3.setLocation(20,20);
con.add(p1);
con.add(p2);
con.add(p3);
jf.add(con);
for(int i=0;i
btnArray.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(arg0.getSource()==btnArray[i]){
text.append(i);
}
}
});
}
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
jf.setSize(350,300);
jf.setBackground(Color.BLACK);
jf.setLocation(200,200);
jf.setVisible(true);
jf.setResizable(false);
}
}
展开