gpt4 book ai didi

java - 为什么 JDialog 显示不正确

转载 作者:行者123 更新时间:2023-11-30 08:10:59 27 4
gpt4 key购买 nike

我创建了一个在对话框中显示消息的方法,但每次运行我的 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 非常不灵活,虽然它们在单一平台和屏幕分辨率上看起来不错,它们在大多数其他平台或屏幕分辨率上看起来很糟糕,并且很难更新和维护。

建议:

  • 去掉 setSize(...)、setBounds(...) 等。
  • 使用布局管理器来帮助调整和定位组件。
  • 在显示对话框之前打包。
  • 不要在程序中过度使用 static 修饰符。
  • 您可以像下面的代码那样做一些事情,但是在展示了这个之后,JOptionPane 将是显示此类消息的最简单方法。

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();
}
});
}
}

关于java - 为什么 JDialog 显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31357430/

27 4 0
文章推荐: java - 用静态 block 覆盖静态字段是不好的做法吗?
文章推荐: java - 如何在 javafxports-Application 中使用外部 Jar
文章推荐: java - RESTEasy - 某些方法 URL 地址不起作用
文章推荐: java - 将 String、ArrayList 等对象添加到 List