编写代码,实现在一个Applet窗口中添加一个按钮,当单击按钮时,在窗口的状态栏上显示“Hello World!”

解决方案 »

  1.   

    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    public class AppletDemo extends JApplet implements ActionListener { JLabel show;
    JButton letshow;

    public AppletDemo()
    {
    show=new JLabel();
    letshow=new JButton("show hello world");
    letshow.addActionListener(this);

    setLayout(new GridLayout(2,1));
    add(letshow);
    add(show);

    setSize(200,200);
    setVisible(true);

    }

    public void actionPerformed(ActionEvent e) {
    show.setText("hello world");
    }}
      

  2.   

    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;public class show extends JFrame implements ActionListener { JLabel jl;
    JButton jb; public show () {

    this.setLayout(null);
    jl = new JLabel();
    jb = new JButton("点我啊");
    jb.setBounds(60, 100,80,30);
    jb.addActionListener(this);
                    jl.setBounds(70, 20,100,50);

    this.add(jb);
    this.add(jl); this.setSize(200, 200);
    this.setLocationRelativeTo(null);
    this.show(); } public static void main(String args[]) {
                  new show();
    }
    public void actionPerformed(ActionEvent e) {
    jl.setText("hello world");
    }}哈哈,这是我的啊