简体   繁体   English

Java无法调用使用eclipse WindowBuilder构建的简单JDialog

[英]Java can't call simple JDialog built using eclipse WindowBuilder

I'm trying to make a custom JDialog using windowBuilder, but at the very beginning I faced some issues. 我正在尝试使用windowBuilder创建自定义JDialog,但在一开始我遇到了一些问题。 So here they are: 所以他们在这里:

I created simple Jdialog using windowBuilder. 我使用windowBuilder创建了简单的Jdialog。 Here it's code: 这是代码:

public class GroupFrame extends JDialog {

    private final JPanel contentPanel = new JPanel();

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        try {
            GroupFrame dialog = new GroupFrame();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Create the dialog.
     */
    public GroupFrame() {
        setBounds(100, 100, 450, 300);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton okButton = new JButton("OK");
                okButton.setActionCommand("OK");
                buttonPane.add(okButton);
                getRootPane().setDefaultButton(okButton);
            }
            {
                JButton cancelButton = new JButton("Cancel");
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

}

But then I want to change public static void main(String[] args) to something like public void show() . 但后来我想将public static void main(String [] args)更改为public void show()

The new show() method code is: 新的show()方法代码是:

public void show() {
        try {
            GroupFrame dialog = new GroupFrame();
            dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
            dialog.setVisible(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

So when I'm triyng to call this changed method using the following code: 因此,当我使用以下代码调用此更改的方法时:

GroupFrame groupFrame = new GroupFrame();
groupFrame.show(); 

I've got the StackOverflowError (what a coincidence! ;D): 我有StackOverflowError(真是巧合!; D):

Exception in thread "AWT-EventQueue-0" java.lang.StackOverflowError
    at sun.awt.Win32GraphicsConfig.getBounds(Native Method)
    at sun.awt.Win32GraphicsConfig.getBounds(Unknown Source)
    at java.awt.Window.init(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Window.<init>(Unknown Source)
    at java.awt.Dialog.<init>(Unknown Source)
    at java.awt.Dialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at javax.swing.JDialog.<init>(Unknown Source)
    at UILayer.GroupFrame.<init>(GroupFrame.java:32)
    at UILayer.GroupFrame.show(GroupFrame.java:21)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at java.awt.Dialog.setVisible(Unknown Source)
    at UILayer.GroupFrame.show(GroupFrame.java:23)
    at java.awt.Component.show(Unknown Source)
    at java.awt.Component.setVisible(Unknown Source)
    at java.awt.Window.setVisible(Unknown Source)
    at java.awt.Dialog.setVisible(Unknown Source)
    at UILayer.GroupFrame.show(GroupFrame.java:23)
    (etc...)

Could anybody point me on what I'm doing wrong? 谁能指出我做错了什么?

Thanks in advance! 提前致谢!

You construct a GroupFrame, and the call the show() method, which constructs another GroupFrame. 您构造一个GroupFrame,并调用show()方法,该方法构造另一个GroupFrame。 This is already wrong. 这已经错了。 But it's even more wrong because then you call setVisible(true) which internally calls the show() method that you inadvertently overrode. 但是它更加错误,因为你调用了setVisible(true) ,它在内部调用你无意中覆盖的show()方法。

The show method should be named something else, and should be static: show方法应该被命名为其他东西,并且应该是静态的:

public static GroupFrame createAndShow() {
    GroupFrame dialog = new GroupFrame();
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);
}

This method would thus be called as this: 因此,此方法将被称为:

GroupFrame.createAndShow();

And please, don't catch Exception. 请不要抓住异常。 And when you do it, don't swallow the exception. 当你这样做时,不要吞下异常。

But then I want to change public static void main(String[] args) to something like public void show(). 但后来我想将public static void main(String [] args)更改为public void show()。

You cannot do that, main method with the exact signature is the starting point of a Java program (for most of the cases but not all). 你不能这样做,具有确切签名的main方法是Java程序的起点(对于大多数情况但不是全部)。

What you need is a separate show method which will make your dialog visible 你需要的是一个单独的show方法,它将使你的对话框可见

EDIT: 编辑:

Noticed your class extends JDialog , which means if you define show again, you are technically overriding the method present in Dialog , moreover show is deprecated. 注意到你的类扩展了JDialog ,这意味着如果你再次定义show,你在技术上会覆盖Dialog的方法,而且不推荐使用show。

From the docs: 来自文档:

Deprecated. 已过时。 As of JDK version 1.5, replaced by setVisible(boolean). 从JDK 1.5版开始,由setVisible(boolean)取代。

Makes the Dialog visible. 使对话框可见。 If the dialog and/or its owner are not yet displayable, both are made displayable. 如果对话框和/或其所有者尚未显示,则两者都可显示。 The dialog will be validated prior to being made visible. 该对话框将在可见之前进行验证。 If the dialog is already visible, this will bring the dialog to the front. 如果对话框已经可见,则会将对话框置于最前面。 If the dialog is modal and is not already visible, this call will not return until the dialog is hidden by calling hide or dispose. 如果对话框是模态的并且尚未显示,则在通过调用hide或dispose隐藏对话框之前,此调用将不会返回。 It is permissible to show modal dialogs from the event dispatching thread because the toolkit will ensure that another event pump runs while the one which invoked this method is blocked. 允许从事件调度线程显示模态对话框,因为工具箱将确保在调用此方法的事件被阻止时运行另一个事件泵。

Instead use this: 而是使用这个:

public static void showMyDialog(){
    try {
         GroupFrame dialog = new GroupFrame();
         dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
         dialog.setVisible(true);
    } catch (Exception e) {
         e.printStackTrace();
    }
}

and in your main method you call showMyDialog . 并在您的main方法中调用showMyDialog

But main method has to be present in your class if you want it to run as a Java Application , if some other class is going to call your show method then you dont need main method existence in your class. 但是如果你希望它作为Java应用程序运行,那么main方法必须存在于你的类中,如果其他类要调用你的show方法,那么你不需要在你的类中存在main方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM