我真的不明白这里发生了什么。我有一个程序,我想使用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);
*/
}
对不起,这显然更多
答案 0 :(得分:2)
问题正在发生,因为对话框的首选大小包括窗口的标题栏以及任何其他窗口装饰。但是,窗口中的位置是从内容窗格的左上角设置的。由于存在这种差异,大多数窗口管理器会让你的按钮最右边的距离太远,而且距离太远了。
正确的解决方案是使用实际的布局管理器,它将正确定位您的组件。这是一个将按钮放在您想要的位置的示例:
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。