为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

qq_笑_17 2023-01-05 10:13:15
我想写一个简单的过马路红绿灯系统。我想制作一个按钮来启动整个程序(打开交通灯系统的 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;        }    }}
查看完整描述

2 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

您的代码中有几个错误:

  1. 您不应该扩展JFrame,请参阅扩展 JFrame 与在程序中创建它

  2. 不要调用布局管理器将负责定位您的setBounds(...)组件

  3. 不要在行与行之间或打开/关闭大括号之后/之前留下太多额外的空间,{}这样会变得难以阅读

  4. 不要作为类名调用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));

    }

}


查看完整回答
反对 回复 2023-01-05
?
阿波罗的战车

TA贡献1862条经验 获得超6个赞

您实际上不需要创建 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);

    }

}


查看完整回答
反对 回复 2023-01-05
  • 2 回答
  • 0 关注
  • 176 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信