gpt4 book ai didi

java - JDialog 未正确设置首选大小?

转载 作者:行者123 更新时间:2023-11-30 02:52:16 26 4
gpt4 key购买 nike

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

抱歉,内容多了

最佳答案

出现此问题的原因是对话框的首选大小包括窗口的标题栏以及任何其他窗口装饰。但是,窗口内的位置是从内容 Pane 的左上角开始设置的。由于这种差异,大多数窗口管理器都会将按钮放置在太靠右的位置,以及太靠下的位置。

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

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 .

关于java - JDialog 未正确设置首选大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38255828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com