我正在尝试使用 windowBuilder 制作自定义 JDialog,但一开始我遇到了一些问题。所以他们在这里:
我使用 windowBuilder 创建了简单的 Jdialog。这是代码:
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);
}
}
}
}
但是后来我想将public static void main(String[] args)更改为public void show()之类的东西。
新的 show() 方法代码是:
public void show() {
try {
GroupFrame dialog = new GroupFrame();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
因此,当我尝试使用以下代码调用此更改的方法时:
GroupFrame groupFrame = new GroupFrame();
groupFrame.show();
我有 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...)
有人可以指出我做错了什么吗?
提前致谢!