實驗十一:圖形界面二

1、源代碼java

package jisuanqi2;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;this


public class Jisuanqi extends Frame implements ActionListener{
public Jisuanqi() {
super("計算器");
Frame ff=new Frame("framework test");
ff.setSize(400,100);
ff.setLocation(300,240);
ff.setLayout(new FlowLayout());
//this.add(new TextField("10",8));
final TextField f1=new TextField("10",8);
ff.add(f1);orm

//this.add(new Label("+"));
Label l1=new Label("+");
ff.add(l1);
//this.add(new TextField("20",8));
TextField f2=new TextField("20",8);
ff.add(f2);
//this.add(new Button("="));
Button b1=new Button("=");
ff.add(b1);
//this.add(new TextField(10));
TextField f3=new TextField(10);
ff.add(f3);
ff.addWindowListener(new myclose());
ff.setVisible(true);
b1.addActionListener(new ActionListener()
{public void actionPerformed(ActionEvent e)
{double c;
String s1=f1.getText();
double a=Integer.parseInt(s1);
String s2=f2.getText();
double b=Integer.parseInt(s2);
c=a+b;
String m=String.valueOf(c);
f3.setText(m);

}blog

private double Integer(String s) {
// TODO Auto-generated method stub
return 0;
}
});字符串

}get

class myclose implements WindowListener{it

public void windowActivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}io

public void windowClosed(WindowEvent arg0) {
// TODO Auto-generated method stub

}event

public void windowClosing(WindowEvent arg0) {
// TODO Auto-generated method stub
System.exit(0);
}form

public void windowDeactivated(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowDeiconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowIconified(WindowEvent arg0) {
// TODO Auto-generated method stub

}

public void windowOpened(WindowEvent arg0) {
// TODO Auto-generated method stub

}

}

 

public static void main(String[] args) {
// TODO 自動生成的方法存根
new Jisuanqi();
}


public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}


}

2、結果

3、心得

一、能夠本身建立一個frame,能夠不用書上的方法建立一個窗口

二、建立按鈕之後,要用add把它添加到窗口上

三、獲得一個輸入框中的內容用gettext

四、要先把輸入框中的內容轉換成字符串類型

相關文章
相關標籤/搜索