小か。
2022-01-04 09:50
采纳率: 100%
浏览 85
已结题

java编写程序,提示说20行有问题


package com.task02;

import java.awt.*;
import java.awt.event.*;
public class WelcomeGUL {
        static Frame f=new Frame("Welcome GUL");
        static Label lb1=new Label("欢迎进入java的图形用户界面!");
        static Label lb2=new Label("在这里显示按钮事件描述");
        static Button b1=new Button("单击我");
        static Button b2=new Button("退出");
    public static void main(String[] args) {
    f.setSize(200,200);
    f.setLayout(new FlowLayout());
    f.add(lb1);
    f.add(lb2);
    f.add(b1);
    f.add(b2);
    b1.addMouseListener(new ButtonlHandler());
    b2.addActionListener(new Button2Handler());
    f.setVisible(true);
}
class Button1Handler implements MouseListener{
    public void mouseClicked(MouseEvent e) {
    WelcomeGUL.lb2.setText("已单击鼠标!");
    }
    public void mouseEntered(MouseEvent e) {
    WelcomeGUL.lb2.setText("已进入按钮上方!");
    }
    public void mouseExited(MouseEvent e) {
    WelcomeGUL.lb2.setText("已离开按钮上方!");
    }
    public void mousePressed(MouseEvent e) {
    WelcomeGUL.lb2.setText("已按下按钮!");
    }
    public void mouseReleased(MouseEvent e) {
    }
}
class Button2Handler implements ActionListener{
   public void actionPerformed(ActionEvent e) {
     System.exit(0);
    }
}
}

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

4条回答 默认 最新

  • CSDN专家-sinJack 2022-01-04 09:55
    最佳回答

    Button1Handler 是普通类的话,就定义在主类外面。静态内部类的话,就用static修饰。

    import java.awt.*;
    import java.awt.event.*;
    public class WelcomeGUL {
        static Frame f=new Frame("Welcome GUL");
        static Label lb1=new Label("欢迎进入java的图形用户界面!");
        static Label lb2=new Label("在这里显示按钮事件描述");
        static Button b1=new Button("单击我");
        static Button b2=new Button("退出");
        public static void main(String[] args) {
            f.setSize(200,200);
            f.setLayout(new FlowLayout());
            f.add(lb1);
            f.add(lb2);
            f.add(b1);
            f.add(b2);
            b1.addMouseListener(new Button1Handler());
            b2.addActionListener(new Button2Handler());
            f.setVisible(true);
        }
        
    }
    class Button1Handler implements MouseListener{
        public void mouseClicked(MouseEvent e) {
            WelcomeGUL.lb2.setText("已单击鼠标!");
        }
        public void mouseEntered(MouseEvent e) {
            WelcomeGUL.lb2.setText("已进入按钮上方!");
        }
        public void mouseExited(MouseEvent e) {
            WelcomeGUL.lb2.setText("已离开按钮上方!");
        }
        public void mousePressed(MouseEvent e) {
            WelcomeGUL.lb2.setText("已按下按钮!");
        }
        public void mouseReleased(MouseEvent e) {
        }
    }
    class Button2Handler implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }
    

    静态内部类

    import java.awt.*;
    import java.awt.event.*;
    public class WelcomeGUL {
        static Frame f=new Frame("Welcome GUL");
        static Label lb1=new Label("欢迎进入java的图形用户界面!");
        static Label lb2=new Label("在这里显示按钮事件描述");
        static Button b1=new Button("单击我");
        static Button b2=new Button("退出");
        public static void main(String[] args) {
            f.setSize(200,200);
            f.setLayout(new FlowLayout());
            f.add(lb1);
            f.add(lb2);
            f.add(b1);
            f.add(b2);
            b1.addMouseListener(new Button1Handler());
            b2.addActionListener(new Button2Handler());
            f.setVisible(true);
        }
        static class Button1Handler implements MouseListener{
            public void mouseClicked(MouseEvent e) {
                WelcomeGUL.lb2.setText("已单击鼠标!");
            }
            public void mouseEntered(MouseEvent e) {
                WelcomeGUL.lb2.setText("已进入按钮上方!");
            }
            public void mouseExited(MouseEvent e) {
                WelcomeGUL.lb2.setText("已离开按钮上方!");
            }
            public void mousePressed(MouseEvent e) {
                WelcomeGUL.lb2.setText("已按下按钮!");
            }
            public void mouseReleased(MouseEvent e) {
            }
        }
        static class Button2Handler implements ActionListener{
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
    }
    
    评论
    解决 无用
    打赏 举报 编辑记录
查看更多回答(3条)

相关推荐 更多相似问题