java的计算器页面代码 java实现计算器界面( 三 )


jf=new JFrame("任静java的计算器页面代码的计算器1.0java的计算器页面代码:JAVA版");
jf.addWindowListener(new WindowAdapter(){
public void windowClosing(){
System.exit(0);
}
});
allButtons=new JButton[16];
clearButton=new JButton("清除");
jtf=new JTextField(25);
jtf.setEditable(false);
String str="123+456-789*0.=/";
for(int i=0;iallButtons.length;i++){
allButtons[i]=new JButton(str.substring(i,i+1));
}
}
public void init(){
//完成布局
jf.setLayout(new BorderLayout());
JPanel northPanel=new JPanel();
JPanel centerPanel=new JPanel();
JPanel southPanel=new JPanel();
northPanel.setLayout(new FlowLayout());
centerPanel.setLayout(new GridLayout(4,4));
southPanel.setLayout(new FlowLayout());
northPanel.add(jtf);
for(int i=0;i16;i++){
centerPanel.add(allButtons[i]);
}
southPanel.add(clearButton);
jf.add(northPanel,BorderLayout.NORTH);
jf.add(centerPanel,BorderLayout.CENTER);
jf.add(southPanel,BorderLayout.SOUTH);
addEventHandler();
}
//添加事件监听
public void addEventHandler(){
jtf.addActionListener(this);
for(int i=0;iallButtons.length;i++){
allButtons[i].addActionListener(this);
}
clearButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
Calculator.this.jtf.setText("");
}
});
}
//事件处理
public void actionPerformed(ActionEvent e) {
//在这里完成事件处理使计算器可以运行
String action=e.getActionCommand();
if(action=="+"||action=="-"||action=="*"||action=="/"){
}
}
public void setFontAndColor(){
Font f=new Font("宋体",Font.BOLD,24);
jtf.setFont(f);
jtf.setBackground(new Color(0x8f,0xa0,0xfb));
for(int i=0;i16;i++){
allButtons[i].setFont(f);
allButtons[i].setForeground(Color.RED);
}
}
public void showMe(){
init();
setFontAndColor();
jf.pack();
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args){
new Calculator().showMe();
}
}
java计算器代码import java.awt.*;
import java.awt.event.*;
public class lvhaiya{
int tmp,sum,sum1,sum2=1,sum3=1;
String a,b,c,d;String s="";
Frame f=new Frame("计算器");
private String[]name={
"0","1","2","3","4","5","6","7","8","9","+","-","*","/","=","空格"
};
public Button[] button=new Button[name.length];
TextField t=new TextField("",30);
Panel p=new Panel();
Panel p1=new Panel();
Color color=new Color(100,170,90);
public lvhaiya(){
p1.setLayout(new GridLayout(5,5));
for(int i=0;iname.length;i++){
button[i]=new Button(name[i]);
p1.add(button[i]);
}
p.setLayout(new FlowLayout(FlowLayout.LEFT));
p.setBackground(color);
p.add(t);
f.add(p,BorderLayout.NORTH);
f.add(p1,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
f.addWindowListener( new WindowClose());
t.setText("0.");
for(int i=0;iname.length;i++){
button[i].addActionListener(new ButtonEventl());
}
}
public static void main(String[]args){
new lvhaiya();
}
class WindowClose extends WindowAdapter{
public void windouClosing(WindowEvent e){
System.exit(0);
}
}
class ButtonEventl implements ActionListener{
public void actionPerformed(ActionEvent e)throws ArithmeticException{
String command=e.getActionCommand();
if(command.equals("+")){
sum=Integer.parseInt(t.getText())+sum;
t.setText(String.valueOf(sum));
a=t.getText();

推荐阅读