【问题标题】:Java Fullscreen Modal DialogsJava 全屏模式对话框
【发布时间】:2011-05-24 19:34:26
【问题描述】:

如何创建可用作内部对话框的自定义模态 JDialog?用于 FullscreenExclusiveMode。

我有一个 JScrollPane(带有一个巨大的滚动条),里面装满了像这样的大按钮:

+----------------------+------+
|         FOO          |  /\  |
|______________________+------+
|                      |______|
|         BAR          |      |
|______________________|  ==  |
|                      |______|
|         BIZ          |      |
+______________________+------+
|                      |  \/  |
|----------------------+------+

我需要用户使用巨大的滚动条来滚动并点击一个特定的按钮来选择它并关闭对话框。该对话框处于全屏独占模式。关闭按钮需要被禁用,并且它不需要有确定或取消按钮,无论他们单击哪个按钮都需要更新一个值,然后在对话框上调用 frame.dispose()。

现在我正在使用一个内部框架,但由于我没有使用 JDesktop,所以该框架没有出现在其他所有内容的前面。我也尝试过 JDialog,但它会最小化应用程序。

JOptionPane.showInternalDialog() 有效,但我如何以相同的方式构建自己的内部对话框以便显示它们?如果我制作一个内部框架,然后将其添加到一个组件中,它只是位于该组件内,而不是位于所有内容之上。

编辑:查看了这些类并尝试了弹出工厂,但弹出窗口似乎不能在全屏模式下可靠地工作。

编辑:在这里尝试 JOptionPane.createInternalFrame() 是我正在使用的演示,但它似乎还没有工作。

public class FullscreenDialog {

    public static final void main(final String[] args) throws Exception {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());//uses os window manager

        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(800,600));

        final JLabel label = new JLabel("Something to say.");
        panel.add(label);
        final JFrame fullscreenFrame = new JFrame();
        fullscreenFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        fullscreenFrame.setUndecorated(true);//To remove the bars around the frame.
        fullscreenFrame.setResizable(false);//resizability causes unsafe operations.
        fullscreenFrame.setContentPane(panel);
        fullscreenFrame.validate();
        GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(fullscreenFrame);//actually applies the fullscreen.

        final JOptionPane optionPane = new JOptionPane();
        optionPane.add(new JLabel("Some alert"));
        final JButton button = new JButton();
        button.setText("Press me.");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                label.setText("worked");
                optionPane.setValue(button);
            }
        });
        JInternalFrame frame  = optionPane.createInternalFrame(panel, "Internal Dialog");
        frame.setVisible(true);
    }
}

【问题讨论】:

    标签: java swing modal-dialog fullscreen jdialog


    【解决方案1】:

    JOptionPane 构造函数的 message 参数可以是 Component 以及字符串,例如可能是你的JScrollPane

    要从选项窗格中删除标准按钮,请使用空数组调用 setOptions(Object[])

    【讨论】:

    • 无论如何要去掉窗饰或者用x不能关闭?
    • @dah,取决于你如何显示它。如果您使用createDialog(),则在生成的JDialog 上尝试setUndecorated(true)
    • 希望我可以为这些按钮添加操作监听器 this.getParent().getParent().hide() 或 .dispose()
    • @dah,如果对话框是模态的,那么当您从 ActionListener 调用 optionPane.setValue(nameOfThisButton) 时,对话框将关闭。
    • 这一切都超级有用。我担心我无法得到解决方案。似乎没有多少人将 swing 用于全屏应用。
    【解决方案2】:

    JOptionPane.showXXXDialog(...) 允许在创建自定义内部对话框时进行大量自定义。

    【讨论】:

    • 我正在寻找一个模态框架,需要一个带有一堆大按钮的 ScrollPane。
    • @dah 使用 JOptionPane 有什么问题?如果您不喜欢它,您可以随时创建自己的 JDialog 并以类似的方式使用它。
    • 我不认为我可以将 JOptionPane 与一堆复杂的组件一起使用,比如 JScrollPane 里面有 20 个 (100x40 ) 按钮。我还需要删除 OK 和 Cancel 按钮。 JDialog 在全屏模式下不起作用。它会导致应用程序失去全屏模式和焦点并将我返回到桌面,因为它是用于只能从远程桌面使用键盘终止的触摸屏界面,这是一个主要问题。
    • 是否可以删除确定和取消按钮,禁用右上角的关闭按钮并将其传递给 JScrollPane 作为其中心的组件?
    【解决方案3】:

    试试这个..

    .
    .
    .
                final JOptionPane optionPane = new JOptionPane();
                optionPane.add(new JLabel("Some alert"));
                final JButton button = new JButton();
                button.setText("Press me.");
                button.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e) {
                        label.setText("worked");
                        fullscreenFrame.invalidate();
                        fullscreenFrame.repaint();
                        optionPane.setValue(button);
                    }
                });
                JInternalFrame frame  = optionPane.createInternalFrame(panel, "Internal Dialog");
                frame.getContentPane().removeAll();
                JPanel pnl = new JPanel(new BorderLayout());
                pnl.add( button, BorderLayout.SOUTH );
                pnl.add( new JScrollBar(), BorderLayout.CENTER );
                frame.getContentPane().add( pnl );
                frame.setVisible(true);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-13
      • 2020-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      相关资源
      最近更新 更多