【发布时间】:2015-07-11 12:58:03
【问题描述】:
我已经创建了一种在对话框中显示消息的方法,但是每次运行我的 java swing 应用程序时对话框的大小都会发生变化,下面是 dailog 屏幕截图的链接:
Improrer Message Box Display
Proper Message Box Display
下面是我为 JDialog Display 创建的方法(SetMSGDialog)
public class DemoClass
{
private static JDialog MSGDialog;
private static JPanel MSGPanel;
private static JLabel MSGLabel;
private static JButton MSGButton;
DemoClass()
{
MSGDialog = new JDialog(MSGDialog,"Message",JDialog.ModalityType.APPLICATION_MODAL);
MSGPanel = new JPanel(null);
MSGLabel = new JLabel();
MSGButton = new JButton("Close");
}
public static void SetMSGDialog(String MSG)
{
MSGDialog.setModal(true);
MSGDialog.setSize(400,125);
MSGDialog.setResizable(false);
MSGDialog.setLocationRelativeTo(null);
MSGLabel.setText(MSG);
MSGButton.setText("Close");
MSGLabel.setBounds(25, 25, 375, 30);
MSGButton.setBounds(160,70,80,30);
MSGPanel.add(MSGLabel);
MSGPanel.add(MSGButton);
MSGDialog.add(MSGPanel);
MSGDialog.setVisible(true);
}
public static void main(String[] args)
{
DemoClass MsgBox = new DemoClass();
MsgBox.SetMSGDialog("Please Login First");
}
}
请告诉我我做错了什么。我必须做什么才能正确显示 Jdialog。
【问题讨论】:
-
1) 摆脱
setSize(...)、setBounds(...)等。 2) 使用布局管理器来帮助调整组件的大小和位置。 3)在显示对话框之前打包。 4) 不要过度静态化你的程序。 -
顺便说一句-Ubuntu、Eclipse、Linux??无需告诉我们您的操作系统和 IDE,除非代码仅在这些特定条件下失败!相信我,这段代码在任何系统上都会失败,使用任何 IDE。
标签: java swing layout-manager null-layout-manager