CalculatorEventClose
(範例) 修改CalculatorEvent使其視窗可以關閉
import java.awt.*;
import java.awt.event.*;
public class CalculatorEventClose extends Frame implements ActionListener{
private static final long serialVersionUID = 1L;
TextField tf =new TextField();
String[] txt ={"7","8","9","+","4","5","6","-","1","2","3","*","0",".","=","/"};
Button[] btn = new Button[txt.length];
public CalculatorEventClose(){
super("Calculator");
add(tf, BorderLayout.NORTH);
addWindowListener(new WindowAdapter() { // 在此加上關閉的程式
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
Panel p= new Panel();
p.setLayout(new GridLayout(4,4,4,4));
for (int i=0; i<txt.length;i++){
btn [i] = new Button(txt[i]);
btn [i].addActionListener(this);
p.add(btn [i]);
}
add(p);
setSize(200, 230);
setVisible(true);
}
public static void main(String[] args) {
new CalculatorEventClose();
}
@Override
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
tf.setText(cmd);
}
}
沒有留言:
張貼留言