【问题标题】:Java return from a ShowOptionDialog from an inner JPanelJava 从内部 JPanel 的 ShowOptionDialog 返回
【发布时间】:2013-07-22 08:26:37
【问题描述】:

我用过

JOptionPane.showOptionDialog(null, 新的我的面板(), “进口”, JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, 空,新对象[]{},空);

因为我不想要 OptionDialog 提供的默认按钮,并且我在 MyPanel extends JPanel 中创建了我的按钮所以我现在的问题是如何从由 ActionEvent 触发的 MyPanel 内部关闭该 OptionDialog?我不在乎返回值,只要该对话框消失即可。而且我意识到这可能不是最好的设计,但我已经为此工作了很多次,所以我更喜欢在结构上尽可能少改变的修复。谢谢!

【问题讨论】:

  • 只是想澄清一下,你不喜欢按钮是因为 L&F 吗?因为我假设你知道我们可以传入我们自己的按钮文本?
  • @user2507946 是的,我知道。默认按钮将在单击时关闭对话框。我不想那样。

标签: java swing jpanel joptionpane jdialog


【解决方案1】:

使用JOptionPane.createDialog(String title)JOptionPane 转换为JDialog

JOptionPane optionPane = new JOptionPane(getPanel(),
                        JOptionPane.PLAIN_MESSAGE,
                        JOptionPane.DEFAULT_OPTION, 
                        null,
                        new Object[]{}, null);
dialog = optionPane.createDialog("import");
dialog.setVisible(true);

现在在actionPerformed(ActionEvent ae) 方法中,简单地写:

dialog.dispose();

看看这个工作示例:

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.*;
import javax.imageio.ImageIO;

public class JOptionPaneExample
{
    private JDialog dialog;

    private void displayGUI()
    {
        JOptionPane optionPane = new JOptionPane(getPanel(),
                        JOptionPane.PLAIN_MESSAGE,
                        JOptionPane.DEFAULT_OPTION, 
                        null,
                        new Object[]{}, null);
        dialog = optionPane.createDialog("import");
        dialog.setVisible(true);
    }

    private JPanel getPanel()
    {
        JPanel panel = new JPanel();
        JLabel label = new JLabel("Java Technology Dive Log");
        ImageIcon image = null;
        try
        {
            image = new ImageIcon(ImageIO.read(
                    new URL("http://i.imgur.com/6mbHZRU.png")));
        }
        catch(MalformedURLException mue)
        {
            mue.printStackTrace();
        }
        catch(IOException ioe)
        {
            ioe.printStackTrace();
        } 
        label.setIcon(image);

        JButton button = new JButton("EXIT");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent ae)
            {
                dialog.dispose();
            }
        });

        panel.add(label);
        panel.add(button);

        return panel;
    }
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new JOptionPaneExample().displayGUI();
            }
        });
    }
}

【讨论】:

  • @YankeeWhisky :请注意,我已经更改了一些选项,从您的 JOptionPane 到这里和那里,尽管它们仍然存在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
相关资源
最近更新 更多