【问题标题】:JDialog not setting preferred size correctly?JDialog没有正确设置首选大小?
【发布时间】:2016-11-10 09:12:11
【问题描述】:

我真的不明白这里发生了什么。我有一个程序,我想在其中使用 JDialog,但我想要一个单独的类,所以我扩展了 JDialog 的类,去设置它的首选大小,但它似乎没有设置它正确。我制作了一个按钮来“取消”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);
 */
}

抱歉,这明显更多

【问题讨论】:

  • 如果你希望它有那么大,为什么不直接使用setSize呢?或者,最好使用布局管理器并让它处理选择大小。
  • set size 也不起作用,我将布局设置为 null,以便更精确地放置组件
  • setLayout(null); 我建议不要使用空布局 - 选择适合您需求的 LayoutManager。
  • @copeg 好的,但这仍然不能解决我的问题。我之前已经多次将几个组件的布局设置为空,我不应该遇到这个问题
  • @Ryan that still doesn't fix my problem 我建议更新发布的代码以显示布局的使用,并描述您希望您的布局如何与您拥有的布局。相信我,避免像瘟疫这样的空布局 - 它可能会在一台计算机与另一台计算机之间显示不同,并且可能无法修改。

标签: java jdialog jcomponent preferredsize


【解决方案1】:

出现问题是因为对话框的首选大小包括窗口的标题栏以及任何其他窗口装饰。但是,窗口内的位置是从内容窗格的左上角设置的。由于这种差异,大多数窗口管理器都会让您的按钮最终靠得太右,而靠得太远。

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

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)),这将使您的框架为窗口装饰添加必要的空间。但是,我强烈建议您不要这样做。使用null 布局是considered a bad practice

【讨论】:

  • 谢谢。而且我已经开始考虑使用不同的布局管理器作为替代品,但是如果我将来做类似的事情,很高兴知道如何解决这个问题
  • 另外,如果我可以问你一个简单的问题,你对 BoxLayouts 和 CardLayouts 的熟悉程度如何。这就是我开始做的事情,我正在接近它,看起来或多或少是我想要的,但我有一两个问题。不管怎样,我都会发布我当前的代码
  • @Ryan CardLayouts 非常简单。我对BoxLayouts 不太熟悉,但也许可以回答。你有什么问题?
  • 好的,谢谢。我的一个问题是我在 BoxLayout 的中间有 JPanel(但是 JPanel 显示的 CardLayout 控件),并且我无法正确调整 JPanel 的大小。我已经设法让它改变宽度,但它似乎做错了高度,我不确定为什么。根据我所做的研究,我相信我做得对。
  • 我显然没有做足够的研究,因为我很快就找到了部分原因。这是布局的类型BoxLayout.Y_AXIS,当我设置宽度而不是高度时,BoxLayout.X_AXIS 反之亦然。我不知道该怎么做,是如何绕过它。我也许可以忽略它,仍然能够实现我想要的,但我宁愿看看有没有办法解决它。
猜你喜欢
  • 2020-09-19
  • 2011-08-11
  • 1970-01-01
  • 2016-03-24
  • 1970-01-01
  • 2011-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多