JDialog无法正确设置首选大小?

瑞安

我真的不明白这里发生了什么。我有一个想要在其中使用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);
 */
}

抱歉,还有更多

谐振

之所以发生此问题,是因为对话框的首选大小包括窗口的标题栏以及任何其他窗口装饰。但是,窗口内的位置是从内容窗格的左上角设置的由于存在这种差异,大多数窗口管理器将使您的按钮最终偏右,偏远。

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

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布局被认为是不好的做法

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法设置 div 的正确大小

来自分类Dev

摇摆:为JDialog设置固定的窗口大小

来自分类Dev

Java-将大小设置为JDialog

来自分类Dev

Java-将大小设置为JDialog

来自分类Dev

JDesktopPane的首选大小由内容设置

来自分类Dev

htaccess-如何正确设置首选URL

来自分类Dev

ArrayIndexOutOfBounds,但数组大小正确设置

来自分类Dev

如何正确设置数组的大小?

来自分类Dev

OS X“缩放”时,如何设置首选的窗口大小?

来自分类Dev

无法设置正确的时间

来自分类Dev

无法正确设置文字

来自分类Dev

Android共享首选项设置不正确

来自分类Dev

无法将首选大小提供给JTable

来自分类Dev

UITableCell无法正确调整大小

来自分类Dev

ContextMenuStrip无法正确调整大小

来自分类Dev

UIImageView无法正确调整大小

来自分类Dev

无法正确调整图像大小

来自分类Dev

网页大小无法正确调整

来自分类Dev

无法以100%中和静态大小左右方式正确设置元素

来自分类Dev

图片的高度和宽度设置为100%,无法在Android浏览器中正确调整大小。

来自分类Dev

变量在DIV调整大小时无法正确设置-添加了其他信息

来自分类Dev

Android应用-共享首选项无法正确加载

来自分类Dev

首选项在平板电脑上无法正确显示

来自分类Dev

JDialog大小未呈现

来自分类Dev

无法设置大小JTextPane / JScrollPane

来自分类Dev

XCode 8:无法设置大小

来自分类Dev

Android Studio-无法读取首选项设置

来自分类Dev

Flutter:无法使用TabBar设置SliverPersistentHeader的首选高度

来自分类Dev

无法从首选项(设置)中检索字符串