黑马程序员技术交流社区
标题:
帮忙使这个计算器界面显示并可以运行
[打印本页]
作者:
姚伟涛
时间:
2012-4-18 01:24
标题:
帮忙使这个计算器界面显示并可以运行
import java.awt.*;
import java.awt.event.*;
public class CalculatorFrame extends Frame implements ActionListener
{
private TextField text1,text2,text3;
private Button button_1,button_2,button_3,button_4,button_5,button_plus,button_subtract,button_multiply,button_divide,button_cancel;
private Label label1,label2,label3;
public CalculatorFrame()
{
super("CalculatorFrame");
this.setSize(320,120);
this.setBackground(Color.lightGray);
this.setLocation(300,240);
this.setLayout(new FlowLayout(FlowLayout.LEFT));
label1=new Label("shu1:");
add(label1);
text1 = new TextField(20);
text1.addActionListener(this);
this.add(text1);
label1=new Label("shu2:");
add(label2);
text2 = new TextField(20);
text2.addActionListener(this);
this.add(text2);
label3=new Label("=");
add(label3);
text3 = new TextField(20);
text3.addActionListener(this);
this.add(text3);
button_1 = new Button("1");
button_2 = new Button("2");
button_3 = new Button("3");
button_4 = new Button("4");
button_5 = new Button("5");
button_plus = new Button("+");
button_subtract=new Button("-");
button_multiply=new Button("*");
button_divide=new Button("/");
button_cancel = new Button("C");
this.add(button_1);
this.add(button_2);
this.add(button_3);
this.add(button_4);
this.add(button_5);
this.add(button_plus);
this.add(button_subtract);
this.add(button_multiply);
this.add(button_divide);
this.add(button_cancel);
button_1.addActionListener(this);
button_2.addActionListener(this);
button_3.addActionListener(this);
button_4.addActionListener(this);
button_5.addActionListener(this);
button_plus.addActionListener(this);
button_subtract.addActionListener(this);
button_multiply.addActionListener(this);
button_divide.addActionListener(this);
button_cancel.addActionListener(this);
this.setVisible(true);
this.addWindowListener(new WinClose());
}
public void actionPerformed(ActionEvent e)
{ if (e.getSource()==button_plus)
{text3.setText(Float.valueOf(Float.valueOf(text1.getText())+Float.valueOf(text2.getText())).toString());}
if (e.getSource()==button_subtract)
{text3.setText(Float.valueOf(Float.valueOf(text1.getText())-Float.valueOf(text2.getText())).toString()); }
if (e.getSource()==button_multiply)
{text3.setText(Float.valueOf(Float.valueOf(text1.getText())*Float.valueOf(text2.getText())).toString());}
if (e.getSource()==button_divide)
{text3.setText(Float.valueOf(Float.valueOf(text1.getText())/Float.valueOf(text2.getText())).toString());}
else
{text1.setText("");
text2.setText("");
text3.setText("");}
}
public static void main(String arg[])
{
new CalculatorFrame();
}
}
class WinClose implements WindowListener
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
public void windowOpened(WindowEvent e) { }
public void windowActivated(WindowEvent e) { }
public void windowDeactivated(WindowEvent e) { }
public void windowClosed(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2