【问题标题】:Modal JFrame on top of full screened JFrame全屏 JFrame 之上的模态 JFrame
【发布时间】:2012-12-26 03:58:52
【问题描述】:
package javaapplication1;

import java.awt.Color;
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JavaApplication1 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        final NewJPanel p = new NewJPanel();
        frame.setTitle("Frame");
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(frame);
        device.setDisplayMode(new DisplayMode(800, 600, 32, 60));


        JButton btn = new JButton();
        btn.setText("Button");
        JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);

        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFrame f = new JFrame();
                JPanel p = new JPanel();
                f.setSize(300, 300);
                p.setBackground(Color.red);

                f.add(p);
                f.setLocationRelativeTo(null);
                f.setAlwaysOnTop(true);
                f.setVisible(true);
            }
        });
    }
}

我希望f.setVisible(true); 在我单击按钮时设置为模态的全屏 JFrame 中弹出。我怎样才能做到这一点?因为在该代码中,当我单击按钮时,f.setVisible(true); 会显示在全屏 JFrame 之外。期待您的回答。

【问题讨论】:

    标签: java swing jframe jpanel fullscreen


    【解决方案1】:

    所以你想在你的主要 JFrame 中添加类似 JInternalFrame 的东西:

    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.DisplayMode;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JDesktopPane;
    import javax.swing.JFrame;
    import javax.swing.JInternalFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class JavaApplication1 {
    
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    final JFrame frame = new JFrame();
                    final JDesktopPane desktopPane = new JDesktopPane();
                    frame.setTitle("Frame");
                    frame.setSize(300, 300);
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    final GraphicsDevice device = GraphicsEnvironment
                        .getLocalGraphicsEnvironment().getDefaultScreenDevice();
                    device.setFullScreenWindow(frame);
                    device.setDisplayMode(new DisplayMode(800, 600, 32, 60));
    
                    JButton btn = new JButton();
                    btn.setText("Button");
                    JPanel panel = new JPanel();
    
                    panel.add(btn);
                    frame.add(panel, BorderLayout.NORTH);
                    frame.add(desktopPane, BorderLayout.CENTER);
    
                    btn.addActionListener(new ActionListener() {
    
                        @Override
                        public void actionPerformed(ActionEvent e) {
                            JInternalFrame f = new JInternalFrame();
                            JPanel p = new JPanel();
                            p.setBackground(Color.red);
                            f.setSize(300, 300);
                            f.setResizable(true);
                            f.add(p);
                            f.setVisible(true);
                            desktopPane.add(f);
                        }
                    });
                }
            });
    
        }
    }
    

    更多关于JInternalFrame你可以找到here

    【讨论】:

    • +1,他可以生成输出 :-) 我的意思是它不需要附加图像
    • 是否需要边框布局?因为在我的应用程序中它已经固定了设计和布局。
    • 我刚刚删除了边框布局。组件未显示。 :(
    • 您必须在某处添加 JDesktopPane
    • 嗯?有什么问题?顺便说一句...不要像您在上一条评论中所说的那样使用“模态 JFrame”。如果要在主框架内显示某种对话框,可以使用JOptionPane.showInternalMessageDialog(),如下所示:stackoverflow.com/questions/13859258/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2012-02-16
    • 2011-10-12
    相关资源
    最近更新 更多