Java/Swing JButton 不显示其文本且不执行其操作

我想写一个简单的过马路红绿灯系统。我想制作一个按钮来启动整个程序(打开交通灯系统的 GUI)。但是我的第一个按钮已经开始出问题了。它不显示其文本,它应该执行的操作也不会发生。我真的是一个初学者所以它可能是一些愚蠢和明显的错误但是请看看我会很高兴^^


package kreuzung;


import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.FlowLayout;


import javax.swing.JFrame;


public class HomeFrame extends JFrame{


    public HomeFrame(String title) {

        super(title);


    this.setLayout(new FlowLayout(FlowLayout.CENTER));


    Button test = new Button("noAction");



    Container cont = getContentPane();


    cont.add(test, BorderLayout.CENTER);




    }

}

这将是生成的按钮,它不做它应该做的事情


package kreuzung;


import javax.swing.Action;

import javax.swing.JButton;


public class Button extends JButton{


    private String actionName;


    public Button(String actionName) {


        this.actionName = actionName;                       //set the Action name of this button                             


        JButton button = new JButton();                     //instantiate this Button

        button.setText(actionName);                         //set the Action Name as Button Text

        button.setSize(30, 30);             

        button.setBounds(5, 5, 25, 25);     





        button.addActionListener(new Evt(this.actionName));     //add an Action Listener to the button 

                                                            //and gets the Action from the Evt Class

    }


}

最后但并非最不重要的是 Evt 类,它应该负责执行动作


package kreuzung;

import java.awt.event.*;


import javax.swing.JFrame;


public class Evt implements ActionListener {


    private String actionName;


    public Evt(String actName) {


        this.actionName = actName;


    }




    @Override

    public void actionPerformed(ActionEvent e) {

        switch(actionName) {

        case "noAction":

            JFrame frame = new HomeFrame("Home");

            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

            frame.setSize(300, 400);

            frame.setVisible(true);

            break;

        }

    }


}


qq_笑_17
浏览 183回答 2
2回答

HUH函数

您的代码中有几个错误:您不应该扩展JFrame,请参阅扩展 JFrame 与在程序中创建它不要调用布局管理器将负责定位您的setBounds(...)组件不要在行与行之间或打开/关闭大括号之后/之前留下太多额外的空间,{}这样会变得难以阅读不要作为类名调用Button,它可能与java.awt.Button类混淆。它不显示其文本,它应该执行的操作也不会发生在这个类中:public class Button extends JButton {    private String actionName;    public Button(String actionName) {        this.actionName = actionName;        JButton button = new JButton();        button.setText(actionName);        button.setSize(30, 30);                     button.setBounds(5, 5, 25, 25);             button.addActionListener(new Evt(this.actionName));    }}您从中扩展JButton,然后在其中创建一个JButton内部!所以,你有 2 JButtons,一个来自类(继承的)和你在其中创建的一个。但是您将文本设置为内部创建的文本,但您将另一个文本(没有文本)添加到您的JFrame.用一个比喻来说,就像:你在页面上写了一些东西你得到一个新的白页并将它添加到你的书中,而不是将你写的那个添加到你的书中。无需JButton在您当前的程序中进行扩展,因此只需创建一个新JButton实例即可。否则,如果您真的想使用自定义JButton类,请执行以下操作:public class MyCustomButton extends JButton { // Change class name    private String actionName;    public MyCustomButton(String actionName) {        super(actionName); //Sets the text        this.actionName = actionName;        button.addActionListener(new Evt(this.actionName));    }}

阿波罗的战车

您实际上不需要创建 JButton 的子类,因为您没有向它添加任何特定属性。相反,您应该能够使其以这种方式工作:public class HomeFrame extends JFrame{    private static final String BUTTON_ACTION_NAME = "myActionName";    public HomeFrame(String title) {        super(title);        this.setLayout(new FlowLayout(FlowLayout.CENTER));        JButton test = new JButton();        test.setText(BUTTON_ACTION_NAME);        test.setSize(30, 30);                     test.setBounds(5, 5, 25, 25);             test.addActionListener(new Evt(BUTTON_ACTION_NAME));        Container cont = getContentPane();        cont.add(test, BorderLayout.CENTER);    }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java