- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我真的不明白这里发生了什么。我有一个程序,我想在其中使用 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/
如何在另一个 JDialog 中添加 JDialog? 最佳答案 JDialog secondDialog = new JDialog(this); // ("this" is the first J
我有一个 JDialog,其中有一个可打开新窗口的按钮。我想要做的是每当其他窗口打开时阻止此 JDialog。当我说阻止时,我的意思是用户无法操纵它,无法移动它或最大化或任何东西。 顺便问一下,对于带
我有一个从 JFrame 上的按钮打开的父 JDialog。父 JDialog 本身有一个从父对话框上的按钮打开的子 JDialog。当我关闭父对话框并使用框架上的按钮再次打开它时,我不希望子对话框也
抱歉这个奇怪的标题,但这是解释。所以我有一个带有学生列表的 StudentRepository 类,这些学生是在 GUI 上选择的(通过 TableModel)。学生对象的属性是: int stude
我有一个未修饰的模态 JDialog,当用户在模态对话框外单击时,我想将其设置为 setVisible(false)。 这在 Swing 中可能吗? 我正在做的是为日期选择器之类的文本字段弹出一个自定
我有一个模式设置对话框,它是一个 JDialog。在这个设置窗口中,我放置了一些组件,包括一个按钮到另一个模态设置对话框,它也是一个 JDialog。我制作了它们 JDialogs,因为这是我所知道的
我只想在单击 JDialog 之外时关闭 JDialog import javax.swing.JDialog; import javax.swing.JLabel; public class Dia
我正在尝试重现我在多个应用程序中看到的功能:我有一个带有多个 JDialog 的 GUI 应用程序。我想轻松地将它们紧密地组织在屏幕上:当我移动一个 JDialog,并且它的一个边界变得“接近”(例如
对 Java 相当陌生,并且遇到了 z 顺序问题。我有一个旧版 Java 应用程序,它有一个主窗口 A,它会弹出一个模式 JDialog B。单击 B 上的按钮后,会弹出一个模式对话框 C。 对于从
我的应用程序中有多个 JDialogs 存储在 map 中。这些JDialogs都有 setModel(false); 当这些对话框失去焦点并且我想将特定的 JDialog 带到前面时,所有 JDia
我有一个扩展 JDialog 的类。当 JDialog 显示时,我单击其启动 Jframe 的显示按钮,但在关闭 JDialog 之前我无法访问 JFrame。当屏幕上存在 JDialog 时,如何访
场景是这样的我的 JFrame 有一个按钮,单击它会打开一个 JDialog,它是一个模型对话框。JDialog 有另一个按钮,我想打开另一个 JFrmae 点击它打开。 结果:另一个 Jframe
我有一个带有主 JFrame 的小型应用程序,它以模态方式打开 JDialog。在这个 JDialog 中,我启动了一个 javax.swing.Timer,它应该在 JDialog 关闭时停止。 p
如何将用户凭据传回到包含的 JFrame,以便 JFrame 知道特定用户? JFrame 有一个 main 方法。 包含的 JFrame 能否以某种方式从 Dialog 中获取用户? 当jbtOk
我基本上创建的是一个 JDialog,它在表单上有一个关键事件。因此,当例如按下空间时,它会做一些事情。在我在同一个对话框上创建一个可编辑的 JTextArea 之前,这种方法工作得很好。当我这样做时
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我正在构建我的第一个 gui,到目前为止一切正常,除了 JDialog 的故障。 .它在第一次使用时相应地接受名称和进程列表。但是当我把它拉回来输入新的输入时,它仍然没有响应。我认为这不是线程问题,因
如何创建一个 Modal JDialog,在任务正在处理时显示“正在加载”,并在超过 3 秒后显示? 最佳答案 为了扩展 Paul 的回答,SwingWorker 可以很好地运行您的后台任务。然后您可
该程序大部分运行正常,但没有打开任何窗口。它应该在桌面右下角显示一个小对话框。但是对于另一个人来说,编译相同的代码没有问题。我们有相同的 Java 运行时 (1.8_u40)。我该如何解决这个问题?
我正在使用 Eclipse 的 Window Builder 插件。 当我执行以下代码时,它正确显示JDialog。我原本希望 JDialog 也能显示在设计选项卡中(在设计时),但它不会。 pack
我是一名优秀的程序员,十分优秀!