java - GUI 关闭问题?

标签 java swing user-interface

我有一个程序显示两个按钮并在滑过时更改其中一个按钮的图像。我的

出错了
press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

部分,它看起来像这样:方法 setDefaultCloseOperation(int) 未为类型 ButtonClass 定义。即使关闭时退出被注释掉了,还有更多错误,请帮忙。

主类(有错误):

package Buttons;
import javax.swing.JFrame;

public class Main_buttons{

public static void main(String[] args) {

    ButtonClass press = new ButtonClass();
    press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    press.setSize(300,200);
    press.setVisible(true);
    }
}

ButtonClass类:

package Buttons;

import java.awt.FlowLayout; //layout proper
import java.awt.event.ActionListener; //Waits for users action
import java.awt.event.ActionEvent; //Users action
import javax.swing.JFrame; //Window
import javax.swing.JButton; //BUTTON!!!
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane; //Standard dialogue box

public class ButtonClass extends JButton {

private JButton regular;
private JButton custom;

public ButtonClass() { // Constructor
    super("The title"); // Title
    setLayout(new FlowLayout()); // Default layout

    regular = new JButton("Regular Button");
    add(regular);

    Icon b = new ImageIcon(getClass().getResource("img.png"));
    Icon x = new ImageIcon(getClass().getResource("swag.png"));
    custom = new JButton("Custom", b);
    custom.setRolloverIcon(x); //When you roll over the button that says custom the image will change from b to x
    add(custom);

    Handlerclass handler = new Handlerclass();
    regular.addActionListener(handler);
    custom.addActionListener(handler);

}

 private class Handlerclass implements ActionListener { // This class is inside the other  class


    public void actionPerformed(ActionEvent eventvar) { // This will happen
                                                        // when button is
                                                        // clicked
        JOptionPane.showMessageDialog(null, String.format("%s", eventvar.getActionCommand()));//Opens a new window with the name of the button
    }
}



}

我到处搜索这个问题,一无所获。请告诉我如何解决有关退出窗口的问题。 谢谢!

最佳答案

当您创建一个扩展 JButton 的类,并在其上调用 setVisible(true) 时,您会感到有些困惑,就好像它是一个顶级窗口,例如 JFrame 或 JDialog ,这没有意义。由于它不是顶级窗口,因此没有默认关闭操作或理解其含义也是有意义的。

我建议您仅在顶层窗口(例如 JFrame 或 JDialog 等)上调用此方法。作为附带建议,我通常会避免使用 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);,而是更经常地使用 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);,这会使其更加灵活。

编辑:实际上,只需更改您的类以扩展 JFrame 而不是扩展 JButton。

确保资源的图片路径正确。例如:

enter image description here

关于java - GUI 关闭问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32083466/

相关文章:

java - 如何使用 Java 邮件 API 读取退回电子邮件的详细信息?

java - 为什么它不起作用? (通过java中的Runnable发送函数作为参数)

matlab - 避免中断 Matlab GUI 中的回调函数

user-interface - JavaFX 图形故障(白框)

java - LIBGDX 游戏内存泄漏

java - VLCJ:播放 1 秒后 Java 运行时环境检测到 fatal error

java - Swing:单击第一个 JPanel 时启动第二个 JPanel

java - 如何用更少的代码在java中制作JButtons?

java - 2D 图形问题 - 可能是逻辑

python - 玛雅Python : Getting a Float Slider Group to align in the same row as Checkboxes