使用JavaSwing在两个对话框的顶部显示
我有一个情况,我显示一个对话框,用户必须填写一些菜单,然后按OK。它工作得很好,但现在我在这个对话框上有另一个按钮,如果用户想添加某些值,我希望弹出另一个对话框,用户在其中填充附加值,当按下ok时,这个对话框消失,用户返回主对话框 我已经试过了,但是每次我调用新对话框时,焦点都不会离开主对话框,我如何才能完成这样的任务 有没有相关的例子,或者做这些事情的正确方法是什么 编辑:使用JavaSwing在两个对话框的顶部显示,java,swing,jdialog,Java,Swing,Jdialog,我有一个情况,我显示一个对话框,用户必须填写一些菜单,然后按OK。它工作得很好,但现在我在这个对话框上有另一个按钮,如果用户想添加某些值,我希望弹出另一个对话框,用户在其中填充附加值,当按下ok时,这个对话框消失,用户返回主对话框 我已经试过了,但是每次我调用新对话框时,焦点都不会离开主对话框,我如何才能完成这样的任务 有没有相关的例子,或者做这些事情的正确方法是什么 编辑: public static class EdgeMenu extends JPopupMenu { //
public static class EdgeMenu extends JPopupMenu {
// private JFrame frame;
public MyMenu(final JFrame frame) {
super("My Menu");
// this.frame = frame;
this.addSeparator();
this.add(new EdgePropItem(frame));
}
}
//this shows the first dialog, another class because i have some other
//functions to be performed here
public static class EdgePropItem extends JMenuItem{
//...
public EdgePropItem(final JFrame frame) {
super("Edit Properties");
this.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
EdgePropertyDialog dialog = new EdgePropertyDialog(frame, edge);
dialog.setVisible(true);
}
});
}
}
现在在另一个对话框中,在按钮偶数侦听器中,我尝试调用另一个对话框:
private void newDialogHandler(java.awt.event.ActionEvent evt) {
MyNewDialog rdialog = new MyNewDialog(edge);
rdialog.setVisible(true);
}
它看起来很好,但上一个对话框没有离开焦点,只有当我在该对话框上按finish/done时,它才会消失,我想要的是新对话框进入焦点,在这里按ok时,焦点应该回到旧的主对话框,但它不工作?您可以通过以下方式实现:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class MultipleDialogs
{
public MultipleDialogs()
{
JButton btnOpen = new JButton("Open another dialog!");
btnOpen.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "This is the second dialog!");
}
});
Object[] options = {"OK", "Cancel", btnOpen};
int selectedOption = JOptionPane.showOptionDialog(null,
"This is the first dialog!", "The title",
JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if(selectedOption == 0) // Clicking "OK"
{
}
else if(selectedOption == 1) // Clicking "Cancel"
{
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new MultipleDialogs();
});
}
}
}
您可以将
“这是第一个对话框!”
和“这是第二个对话框!”
替换为JPanel
,它可以包含您想要的任何swing组件。您可以通过以下方式实现:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
public class MultipleDialogs
{
public MultipleDialogs()
{
JButton btnOpen = new JButton("Open another dialog!");
btnOpen.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "This is the second dialog!");
}
});
Object[] options = {"OK", "Cancel", btnOpen};
int selectedOption = JOptionPane.showOptionDialog(null,
"This is the first dialog!", "The title",
JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null, options, options[0]);
if(selectedOption == 0) // Clicking "OK"
{
}
else if(selectedOption == 1) // Clicking "Cancel"
{
}
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new MultipleDialogs();
});
}
}
}
您可以将
“这是第一个对话框!”
和“这是第二个对话框!”
替换为JPanel
,它可以包含您想要的任何swing组件。也许此代码可以说明您的问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SuperConstructor extends JFrame {
private static final long serialVersionUID = 1L;
public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
Container cp = getContentPane();
JButton b = new JButton("Show dialog");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
}
});
cp.add(b, BorderLayout.SOUTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
add(bClose, BorderLayout.NORTH);
pack();
setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}
private class FirstDialog extends JDialog {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent) {
super(parent, "FirstDialog");
setPreferredSize(new Dimension(200, 200));
setLocationRelativeTo(parent);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
JButton bNext = new JButton("Show next dialog");
bNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
SecondDialog secondDialog = new SecondDialog(parent, false);
}
});
add(bNext, BorderLayout.NORTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
private int i;
private class SecondDialog extends JDialog {
private static final long serialVersionUID = 1L;
SecondDialog(final Frame parent, boolean modal) {
//super(parent); // < --- Makes this dialog
//unfocusable as long as FirstDialog is visible
setPreferredSize(new Dimension(200, 200));
setLocation(300, 50);
setModal(modal);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("SecondDialog " + (i++));
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类超构造函数扩展JFrame{
私有静态最终长serialVersionUID=1L;
公共超级构造函数(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(新尺寸(300300));
setTitle(“超级构造函数”);
容器cp=getContentPane();
JButton b=新JButton(“显示对话框”);
b、 addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
FirstDialog FirstDialog=新的FirstDialog(SuperConstructor.this);
}
});
cp.add(b,南部边界布局);
JButton bClose=新JButton(“关闭”);
bClose.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
系统出口(0);
}
});
添加(bClose,BorderLayout.NORTH);
包装();
setVisible(真);
}
公共静态void main(字符串参数[]){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
超级构造函数超级构造函数=新超级构造函数();
}
});
}
私有类FirstDialog扩展JDialog{
私有静态最终长serialVersionUID=1L;
第一个对话框(最终帧父对象){
超级(父级,“第一对话”);
setPreferredSize(新尺寸(200200));
setLocationRelativeTo(父级);
setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
JButton bNext=新JButton(“显示下一个对话框”);
bNext.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
SecondDialog SecondDialog=新建SecondDialog(父级,false);
}
});
添加(bNext,BorderLayout.NORTH);
JButton bClose=新JButton(“关闭”);
bClose.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
setVisible(假);
}
});
添加(bClose,BorderLayout.SOUTH);
包装();
setVisible(真);
}
}
私人互联网i;
私有类SecondDialog扩展JDialog{
私有静态最终长serialVersionUID=1L;
第二个对话框(最终帧父对象,布尔模式){
//超级(父级);//<---创建此对话框
//只要FirstDialog可见,就不可聚焦
setPreferredSize(新尺寸(200200));
设定位置(300,50);
设置模态(模态);
setDefaultCloseOperation(JDialog.DISPOSE\u ON\u CLOSE);
setTitle(“第二个对话框”+(i++);
JButton bClose=新JButton(“关闭”);
bClose.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
setVisible(假);
}
});
添加(bClose,BorderLayout.SOUTH);
包装();
setVisible(真);
}
}
}
也许这段代码可以说明您的问题
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SuperConstructor extends JFrame {
private static final long serialVersionUID = 1L;
public SuperConstructor() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(300, 300));
setTitle("Super constructor");
Container cp = getContentPane();
JButton b = new JButton("Show dialog");
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
FirstDialog firstDialog = new FirstDialog(SuperConstructor.this);
}
});
cp.add(b, BorderLayout.SOUTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
System.exit(0);
}
});
add(bClose, BorderLayout.NORTH);
pack();
setVisible(true);
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
SuperConstructor superConstructor = new SuperConstructor();
}
});
}
private class FirstDialog extends JDialog {
private static final long serialVersionUID = 1L;
FirstDialog(final Frame parent) {
super(parent, "FirstDialog");
setPreferredSize(new Dimension(200, 200));
setLocationRelativeTo(parent);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setModalityType(Dialog.ModalityType.DOCUMENT_MODAL);
JButton bNext = new JButton("Show next dialog");
bNext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
SecondDialog secondDialog = new SecondDialog(parent, false);
}
});
add(bNext, BorderLayout.NORTH);
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
private int i;
private class SecondDialog extends JDialog {
private static final long serialVersionUID = 1L;
SecondDialog(final Frame parent, boolean modal) {
//super(parent); // < --- Makes this dialog
//unfocusable as long as FirstDialog is visible
setPreferredSize(new Dimension(200, 200));
setLocation(300, 50);
setModal(modal);
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
setTitle("SecondDialog " + (i++));
JButton bClose = new JButton("Close");
bClose.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
setVisible(false);
}
});
add(bClose, BorderLayout.SOUTH);
pack();
setVisible(true);
}
}
}
import java.awt.*;
导入java.awt.event.*;
导入javax.swing.*;
公共类超构造函数扩展JFrame{
私有静态最终长serialVersionUID=1L;
公共超级构造函数(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(新尺寸(300300));
setTitle(“超级构造函数”);
容器cp=getContentPane();
JButton b=新JButton(“显示对话框”);
b、 addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
FirstDialog FirstDialog=新的FirstDialog(SuperConstructor.this);
}
});
cp.add(b,南部边界布局);
JButton bClose=新JButton(“关闭”);
bClose.addActionListener(新ActionListener(){
@凌驾
已执行的公共无效操作(操作事件evt){
系统出口(0);
}
});
添加(b关闭,边框)