这两天好累啊,为实现每天学习一点点的目标,我一定要坚持再坚持,一直到某一天写这个系列的博文时博文标题改成:JAVA学习第一百天。以下是一个习题的代码:
import java.awt.*;import java.applet.*;import java.awt.event.*;
class mainwindow extends Frame implements ActionListener { TextField t1,t2; Button b1,b2; mainwindow(String s,int a,int b) { super(s); MenuBar mb1 =new MenuBar(); Menu m1=new Menu("file"); MenuItem mi1=new MenuItem("exit"); t1 =new TextField(10); t2=new TextField(10); b1 =new Button("ok"); b2 =new Button("close"); b1.addActionListener(this); t2.setEditable(false); setSize(a,b); setVisible(true); setBackground(Color.blue); setLayout(new GridLayout(1,3)); setMenuBar(mb1); mb1.add(m1);m1.add(mi1); mi1.addActionListener(this); add(t1);add(t2);add(b1);add(b2); b1.addActionListener(this); b2.addActionListener(this); pack(); } public void actionPerformed(ActionEvent e) { if ((e.getSource()==b1) && (t1.getText().equals("pig"))) {t2.setText("猪");} else if((e.getSource()==b1)&&(t1.getText().equals("dog"))) {t2.setText("狗");} else if((e.getSource()==b1)&&(t1.getText().equals("cat"))) {t2.setText("猫");} if(e.getSource()==b2) {System.exit(0);} }} public class three extends Applet{ public void init() { mainwindow mywindow =new mainwindow("mywindow",60,300); }
}
还是close按钮无效,看来我还得好好再继续努力呀!