JDialog 没有正确设置首选大小?

JDialog not setting preferred size correctly?

我真的不明白这里发生了什么。我有一个程序,我想在其中使用 JDialog,但我想要一个单独的 class,所以我扩展了 JDialog 的 class,去设置它的首选大小,但是它似乎没有正确设置它。我为 "cancel" JDialog 将执行的操作创建了一个按钮,该按钮应该位于 JDialog 的右下角,但它并没有出现在那里。相反,它在它之外,只有在我调整 JDialog 的大小时才会显示。

我完全不知道发生了什么,或者我是否遗漏了一些愚蠢的东西,但我们将不胜感激。

代码如下:

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class NewPlatformDialog extends JDialog implements ActionListener {
private LevelEditor le;

private JButton cancel = new JButton("cancel");

public NewPlatformDialog(Frame owner, String title, LevelEditor l) {
    super(owner, title, Dialog.ModalityType.APPLICATION_MODAL);
    le = l;
    setLayout(null);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setPreferredSize(new Dimension(300, 200));

    add(cancel);
    setLocation((owner.getLocationOnScreen().x + (owner.getLocationOnScreen().x + owner.getWidth()))/2 - getPreferredSize().width/2
            , (owner.getLocation().y + (owner.getLocationOnScreen().y + owner.getHeight()))/2 - getPreferredSize().height/2);

    cancel.setBounds(getPreferredSize().width - cancel.getPreferredSize().width - 10, 
            getPreferredSize().height - cancel.getPreferredSize().height - 10,
            cancel.getPreferredSize().width, cancel.getPreferredSize().height);
    cancel.addActionListener(this);
    cancel.setFocusable(false);

    pack();
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if(source == cancel) dispose();
}
}

抱歉有点乱,复制粘贴的时候有点乱。

带有布局的更新代码

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class NewPlatformDialog extends JDialog implements ActionListener {
private LevelEditor le;
private String[] options = {
        "Standard Platform",
        "Boost Platform",
        "Moving Platform",
        "Death Platform"
};
private JComboBox<String> platformSelector = new JComboBox<String>(options);

private JButton cancel = new JButton("cancel");

CardLayout cl = new CardLayout();

private JPanel typeSelect = new JPanel(new FlowLayout(FlowLayout.LEFT));
private JPanel panelContainer = new JPanel();
private JPanel standardPlatform = new JPanel();
private JPanel boostPlatform = new JPanel();
private JPanel movingPlatform = new JPanel();
private JPanel deathPlatform = new JPanel();
private JPanel buttonPanel = new JPanel();

public NewPlatformDialog(Frame owner, String title, LevelEditor l) {
    super(owner, title, Dialog.ModalityType.APPLICATION_MODAL);
    setDefaultCloseOperation(DISPOSE_ON_CLOSE);
    setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
    setResizable(false);

    cl.setVgap(10);
    panelContainer.setLayout(cl);
    le = l;

    platformSelector.addActionListener(this);
    platformSelector.setFocusable(false);
    platformSelector.setSize(100, 70);
    platformSelector.setFont(new Font("", Font.BOLD, 12));
    platformSelector.setMaximumSize(new Dimension(200, platformSelector.getPreferredSize().height));
    platformSelector.setMinimumSize(new Dimension(200, platformSelector.getPreferredSize().height));

    add(typeSelect);
    add(panelContainer);
    add(buttonPanel);

    panelContainer.add(standardPlatform, "1");
    panelContainer.add(boostPlatform, "2");
    panelContainer.add(movingPlatform, "3");
    panelContainer.add(deathPlatform, "4");
    panelContainer.setMaximumSize(new Dimension(500, 500));
    panelContainer.setMinimumSize(new Dimension(20, 20));

    createTypeSelect();
    createStandardPlatformPanel();
    createButtonPanel();

    cl.show(panelContainer, "1");

    pack();
    setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    Object source = e.getSource();
    if(source == cancel) dispose();
    else if(source == platformSelector) {
        if(platformSelector.getSelectedIndex() == 0) {
            cl.show(panelContainer, "1");
            createStandardPlatformPanel();
        }
        else if(platformSelector.getSelectedIndex() == 1) {
            cl.show(panelContainer, "2");
            createBoostPlatformPanel();
        }
        else if(platformSelector.getSelectedIndex() == 2) {
            cl.show(panelContainer, "3");
            createMovingPlatformPanel();
        }
        else if(platformSelector.getSelectedIndex() == 3) {
            cl.show(panelContainer, "4");
            createDeathPlatformPanel();
        }
    }
}

private void createTypeSelect() {
    typeSelect.add(new JLabel("Platform Type: ")).setFont(new Font("", Font.PLAIN, 14));
    typeSelect.add(platformSelector);
}

private void createStandardPlatformPanel() {
    panelContainer.setPreferredSize(new Dimension(200, 200));
    standardPlatform.setBackground(Color.BLUE);
    pack();
}

private void createBoostPlatformPanel() {
    panelContainer.setPreferredSize(new Dimension(500, 500));
    boostPlatform.setBackground(Color.RED);
    pack();
}

private void createMovingPlatformPanel() {
    panelContainer.setPreferredSize(new Dimension(50, 50));
    movingPlatform.setBackground(Color.YELLOW);
    pack();
}

private void createDeathPlatformPanel() {
    panelContainer.setPreferredSize(new Dimension(100, 100));
    deathPlatform.setBackground(Color.GRAY);
    pack();
}

private void createButtonPanel() {
    cancel.addActionListener(this);
    cancel.setFocusable(false);
    cancel.setAlignmentX(Box.RIGHT_ALIGNMENT);
    buttonPanel.add(cancel);
}

/*
 * 
    add(cancel);
    setLocation((owner.getLocationOnScreen().x + (owner.getLocationOnScreen().x + owner.getWidth()))/2 - getPreferredSize().width/2
            , (owner.getLocation().y + (owner.getLocationOnScreen().y + owner.getHeight()))/2 - getPreferredSize().height/2);

    cancel.setBounds(getPreferredSize().width - cancel.getPreferredSize().width - 10, 
            getPreferredSize().height - cancel.getPreferredSize().height - 10,
            cancel.getPreferredSize().width, cancel.getPreferredSize().height);
    cancel.addActionListener(this);
    cancel.setFocusable(false);
 */
}

抱歉,多了很多

出现此问题是因为对话框的首选大小包括 window 的标题栏以及任何其他 window 装饰。但是,window 中的位置是从 内容窗格 的左上角开始设置的。由于这种差异,大多数 window 经理会让您的按钮在最后有点太右,而且太低。

正确的解决方案是使用实际的布局管理器,这将正确定位您的组件。这是一个将按钮放在您想要的位置的示例:

setLayout(new BorderLayout());
JPanel bottom = new JPanel();
bottom.setLayout(new BorderLayout());
bottom.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
bottom.add(new JPanel(), BorderLayout.CENTER);
bottom.add(cancel, BorderLayout.EAST);
add(new JPanel(), BorderLayout.CENTER);
add(bottom, BorderLayout.SOUTH);

如果您坚持使用 null 布局,您可以更改代码以改用 getContentPane().setPreferredSize(new Dimension(300, 200)),这将使您的框架为 window装饰品。但是,我强烈建议您不要这样做。使用 null 布局是 considered a bad practice.