Make Java Swing Modal Dialog Behave Like Mac OSX Dialogs(使 Java Swing 模态对话框的行为类似于 Mac OSX 对话框)
问题描述
我正在编写一个小型应用程序,它需要 ProgressBar 显示在框架的 TitleBar 下方,这在 Mac OSX 应用程序中很常见.我有两个问题:
1.我已经管理了定位,但我不得不硬编码父 Frame 的 TitleBar 高度.是否有一种软"方法来获取 TitleBar 的高度?
在Dialog的构造函数中:
维度 dimensionParentFrame = parent.getSize();维度 dimensionDialog = getSize();int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);setLocation(x, parent.getY() + 22);//TODO 硬代码警告标题高度
2.即使对话框是模态的,我仍然可以单击父框架并移动它.如何使对话框粘"到父框架?也就是说,当父 Frame 移动时,Dialog 会随之移动,就好像附加了一样.
任何帮助/指针将不胜感激.
代码如下:
<代码>导入 javax.swing.JFrame;导入 javax.swing.JButton;导入 java.awt.BorderLayout;导入 java.awt.EventQueue;导入 java.awt.event.ActionListener;导入 java.awt.event.ActionEvent;公共类 ModalDialogDemoFrame 扩展了 JFrame{ModalDialogDemoFrame modalDialogDemo;公共 ModalDialogDemoFrame(){modalDialogDemo = 这个;setBounds(100, 100, 400, 400);设置默认关闭操作(JFrame.EXIT_ON_CLOSE);JButton buttonDialog = new JButton("打开对话框");buttonDialog.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent arg0){//创建一个以此框架为父的模态对话框.ModalDialog modalDialog = new ModalDialog(modalDialogDemo, true);modalDialog.setVisible(true);}});getContentPane().add(buttonDialog, BorderLayout.CENTER);}/*** @param 参数*/公共静态无效主要(字符串 [] 参数){EventQueue.invokeLater(new Runnable(){公共无效运行(){尝试{ModalDialogDemoFrame window = new ModalDialogDemoFrame();window.setVisible(true);}捕获(异常 e){e.printStackTrace();}}});}}导入 java.awt.Dimension;导入 javax.swing.JDialog;导入 javax.swing.JFrame;导入 javax.swing.JButton;导入 java.awt.BorderLayout;导入 java.awt.event.ActionListener;导入 java.awt.event.ActionEvent;公共类 ModalDialog 扩展了 JDialog{公共模态对话框(JFrame 父级,布尔模态){超级(父,模态);维度 dimensionParentFrame = parent.getSize();setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width/2, 75));维度 dimensionDialog = getSize();int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);setLocation(x, parent.getY() + parent.getInsets().top);设置未装饰(真);设置模态(模态);setModalityType(ModalityType.APPLICATION_MODAL);设置默认关闭操作(DISPOSE_ON_CLOSE);JButton buttonClose = new JButton("关闭");buttonClose.addActionListener(new ActionListener(){公共无效actionPerformed(ActionEvent e){处置();}});getContentPane().add(buttonClose, BorderLayout.CENTER);}}
int titleBarHeight = frame.getInsets().top;
<块引用>
即使对话框是模态的,我仍然可以单击父框架并移动它.
那么你做错了,因为这不应该发生.
发布您的 SSCCE 来证明问题.
I am writing a small app that requires a ProgressBar to appear centred under the frame's TitleBar as is often seen in Mac OSX apps. I have two problems:
1. I have managed the positioning but I had to hard code the parent Frame's TitleBar height. Is there a 'soft' way to get the TitleBar's height?
In the Dialog's constructor:
Dimension dimensionParentFrame = parent.getSize();
Dimension dimensionDialog = getSize();
int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);
setLocation(x, parent.getY() + 22); // TODO HARD CODE WARNING TITLE HEIGHT
2. Even though the Dialog is modal, I can still click on the parent Frame and move it. How can I make the Dialog 'stick' to the parent Frame? That is, when the parent Frame is moved the Dialog moves with it as if attached.
Any help/pointers would be much appreciated.
Here is the code:
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ModalDialogDemoFrame extends JFrame
{
ModalDialogDemoFrame modalDialogDemo;
public ModalDialogDemoFrame()
{
modalDialogDemo = this;
setBounds(100, 100, 400, 400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton buttonDialog = new JButton("Open Dialog");
buttonDialog.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
// Create a Modal Dialog with this Frame as Parent.
ModalDialog modalDialog = new ModalDialog(modalDialogDemo, true);
modalDialog.setVisible(true);
}
});
getContentPane().add(buttonDialog, BorderLayout.CENTER);
}
/**
* @param args
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
ModalDialogDemoFrame window = new ModalDialogDemoFrame();
window.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
}
import java.awt.Dimension;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class ModalDialog extends JDialog
{
public ModalDialog(JFrame parent, boolean modal)
{
super(parent, modal);
Dimension dimensionParentFrame = parent.getSize();
setSize(new Dimension((parent == null) ? 300 : dimensionParentFrame.width / 2, 75));
Dimension dimensionDialog = getSize();
int x = parent.getX() + ((dimensionParentFrame.width - dimensionDialog.width)/2);
setLocation(x, parent.getY() + parent.getInsets().top);
setUndecorated(true);
setModal(modal);
setModalityType(ModalityType.APPLICATION_MODAL);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
JButton buttonClose = new JButton("Close");
buttonClose.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
dispose();
}
});
getContentPane().add(buttonClose, BorderLayout.CENTER);
}
}
int titleBarHeight = frame.getInsets().top;
Even though the Dialog is modal, I can still click on the parent Frame and move it.
Then you are doing something wrong because this should NOT happen.
Post your SSCCE that demonstrates the problem.
这篇关于使 Java Swing 模态对话框的行为类似于 Mac OSX 对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使 Java Swing 模态对话框的行为类似于 Mac OSX 对话框
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01