GUI关闭问题? [英] GUI close issue?
问题描述
我有一个程序,它显示两个按钮并在翻转时更改其中一个按钮的图像.我的
I have a program which displays two buttons and changes the image of one of the buttons on roll over. I am getting an error on my
press.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
部分,它看起来像这样: ButtonClass 类型的方法 setDefaultCloseOperation(int) 是未定义的.即使在关闭时退出注释还有更多错误,请帮助.
part, And it looks like this: The method setDefaultCloseOperation(int) is undefined for the type ButtonClass. Even with the exit on close commented out there are more errors, please help.
主类(有错误):
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 类:
ButtonClass class:
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
}
}
}
我到处搜索这个问题,但一无所获.请告诉我如何解决有关退出窗口的问题.谢谢!
I have searched everywhere for this problem and found nothing. Please tell me how to resolve this issue about exiting my window. Thanks!
推荐答案
当您创建一个扩展 JButton 的类并在其上调用 setVisible(true)
作为如果它是一个顶级窗口,如 JFrame 或 JDialog,那没有意义.由于它不是顶级窗口,因此没有默认关闭操作或了解其含义也很有意义.
You're a little confused as you're creating a class that extends JButton, and calling setVisible(true)
on it as if it were a top-level window such as a JFrame or JDialog, and that doesn't make sense. Since it isn't a top-level window it also makes sense to not have a default close operation or understand what that means.
我建议您仅在顶级窗口(例如 JFrame 或 JDialog 等)上调用此方法.作为附带建议,我通常避免使用 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
,而是更频繁地使用 setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
,这给了它更多的灵活性.
I suggest that you call this method only on top-level windows such as on a JFrame or JDialog and the like. As a side recommendation, I usually avoid setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
and instead more often do setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
which gives it a little more flexibility.
实际上,只需将您的类更改为扩展 JFrame 而不是扩展 JButton.
actually, just change your class to extends JFrame not extends JButton.
确保资源的图片路径正确.例如:
Make sure your image path to your resources is correct. For example:
这篇关于GUI关闭问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!