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

Java/Swing JButton does not display its text and does not perform its action

我想写一个简单的过马路红绿灯系统。我想制作一个按钮来启动整个程序(打开交通灯系统的 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 class,它应该负责执行的操作

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;
        }
    }

}

您实际上不需要创建 JButton 的子项 class,因为您没有向它添加任何特定属性。 相反,您应该能够使其以这种方式工作:

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);
    }
}

您的代码中有几个错误:

  1. 您不应扩展 JFrame,请参阅 Extends JFrame vs. creating it inside the program
  2. 不要调用 setBounds(...)Layout Managers 会负责定位您的组件
  3. 不要在行与行之间或开/关大括号之后/之前留下太多额外的内容space {} 它变得难以阅读
  4. 不要将 Button 称为 class 名称,它可能会与 java.awt.Button class.
  5. 混淆

It doesnt display its text and the action it should perform won't happen

在这个class中:

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,一个来自 class(继承的),一个是您在其中创建的。但是您将文本设置为内部创建的文本,但将另一个文本(无文本)添加到 JFrame.

打个比方,就是:

  • 你在页面上写点东西
  • 你得到一个新的白页并将其添加到你的书中,而不是将你写的那一页添加到你的书中。

无需在您当前的程序中扩展 JButton,因此只需创建一个新的 JButton 实例即可。

否则,如果您真的想使用自定义 JButton class,请执行以下操作:

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));
    }
}