发布于2021-11-28 10:30 阅读(1139) 评论(0) 点赞(6) 收藏(4)
我创建了一种在对话框中显示消息的方法,但是每次运行我的 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。
你问为什么你的 JDialog 没有正确显示,主要原因是你的代码忽略了你的组件已经使用的布局管理器。一种快速而糟糕的解决方案是使用绝对或null
布局,但不建议将其用于组件放置,因为这会导致 GUI 非常不灵活,虽然它们在单个平台和屏幕分辨率上看起来不错,但在大多数其他平台上看起来很糟糕或屏幕分辨率,并且很难更新和维护。
建议:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.*;
@SuppressWarnings("serial")
public class DemoClass2 extends JPanel {
// constants
private static final String TITLE = "Message";
private static final float LABEL_POINTS = 24f;
private static final int L_GAP = 15;
private static final int B_GAP = 25;
// static poor man's singlton instance
private static DemoClass2 demoClass2;
private JDialog dialog = new JDialog();
private JLabel label = new JLabel("", SwingConstants.CENTER);
public DemoClass2() {
dialog.setModal(true);
dialog.setTitle(TITLE);
label.setFont(label.getFont().deriveFont(Font.BOLD, LABEL_POINTS));
label.setBorder(BorderFactory.createEmptyBorder(L_GAP, L_GAP, L_GAP, L_GAP));
JPanel btnPanel = new JPanel(new GridBagLayout());
btnPanel.setBorder(BorderFactory.createEmptyBorder(B_GAP, B_GAP, B_GAP, B_GAP));
btnPanel.add(new JButton(new DisposeAction("Close", KeyEvent.VK_C)));
setLayout(new BorderLayout());
//setBorder(border);
add(label, BorderLayout.PAGE_START);
add(btnPanel, BorderLayout.CENTER);
dialog.getContentPane().add(this);
}
private void setMessage(String message) {
label.setText(message);
}
private void display() {
dialog.setResizable(false);
dialog.pack();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
}
public void setMessageAndDisplay(String message) {
setMessage(message);
display();
}
public static void displayMessage(String message) {
if (demoClass2 == null) {
demoClass2 = new DemoClass2();
}
demoClass2.setMessageAndDisplay(message);
}
private class DisposeAction extends AbstractAction {
public DisposeAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
Component c = (Component) e.getSource();
if (c == null) {
return;
}
Window win = SwingUtilities.getWindowAncestor(c);
if (win == null) {
return;
}
win.dispose();
}
}
private static void createAndShowGui() {
DemoClass2.displayMessage("Fubars Rule!!!");
DemoClass2.displayMessage("This is a bit of a longer message. The dialog should get bigger.");
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/337849/3c96da6abcdd4663bf6f/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!