首页 文章

新手JLayeredPane问题

提问于
浏览
2

我只是无法通过JLayeredPanes的方方面面 . (请参阅我的original question of yesterday . 我一直在研究JLayeredPane教程和API . 这些教程与我最终尝试制作的内容有些不同 .

回到第一个方面,我使用了Oracle的JFrame示例并将其修改为包含分层窗格 .

这是代码:

package components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/* FrameDemo.java requires no other files. */
public class FrameDemo {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("FrameDemo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainLayer = new JPanel(new BorderLayout());
        mainLayer.setPreferredSize(new Dimension(640, 480));
        frame.setContentPane(mainLayer);
        frame.getLayeredPane().add(mainLayer, JLayeredPane.DEFAULT_LAYER, 0);

        JLabel emptyLabel = new JLabel("LABEL");
        emptyLabel.setPreferredSize(new Dimension(320, 240));
        mainLayer.add(emptyLabel, BorderLayout.NORTH);

        JPanel subLayer = new JPanel(new BorderLayout());
        JLabel subLabel = new JLabel("SUBLABEL");
        subLabel.setPreferredSize(new Dimension( 200, 100));
        subLabel.setBackground(Color.YELLOW);
        subLayer.add(subLabel, BorderLayout.SOUTH);
        subLayer.setVisible(true);
        subLabel.setVisible(true);
        frame.getLayeredPane().add(subLayer, JLayeredPane.PALETTE_LAYER, 0);


        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

为什么不起作用? IOW,为什么子标签不显示?它比主层更高 .

一个想法是为什么我将mainLayer添加到内容窗格和分层窗格?如果我不这样做,什么都不会出现 . 即,通过评论这一行,我只得到一个空白框架 .

//        frame.setContentPane(mainLayer);

显然,我不理解某些事情 . 但它是什么?

我应该补充一点,显然,这个简单的演示可以在没有分层窗格的情况下完成 . 但我的最终目标是拥有一个可以以编程方式打开和关闭的图层 . 但我甚至无法让这个简单的案例工作 . 如果我能克服这个驼峰,我认为剩下的会更容易 .

附录:

下面的代码说明了我想要实现的内容,它与下面的TrashGod设置非常相似,并且可以正常运行 . 有一个带有常量层的JLayeredPane(在Integer(0)处分层)和一个最初在Integer(-1)处分层的浮动层,但可以通过Integer(-1)层和Integer(1)之间的F7和F8键击来切换层,从而允许它漂浮在恒定层之上或之下 .

package components;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;


/* MyLayeredPaneDemo.java requires no other files. */
public class MyLayeredPaneDemo {
    private JFrame frame;
    private JLayeredPane mainPanel;
    private JPanel constantLayer;
    private JPanel floatingLayer;
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private MyLayeredPaneDemo() {}
    private void createAndShowGUI() {
        //Create and set up the window.
        this.frame = new JFrame("MyLayeredPaneDemo");
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.frame.setPreferredSize(new Dimension(640, 480));
        mainPanel = new JLayeredPane();
        constantLayer = new JPanel(new BorderLayout(0,0));
        floatingLayer = new JPanel(new BorderLayout(0,0));
//        constantLayer.setPreferredSize();
        constantLayer.setOpaque(true);
        constantLayer.setBackground(Color.BLUE);


        JLabel constantLabel = new JLabel("MAIN LAYER");
        constantLayer.setPreferredSize(new Dimension(640, 480));
        constantLayer.add(constantLabel, BorderLayout.CENTER);

        JLabel subLabel = new JLabel("SUB LAYER");
        floatingLayer.setBackground(Color.YELLOW);
        floatingLayer.add(subLabel, BorderLayout.SOUTH);
        floatingLayer.setOpaque(true);
        floatingLayer.setVisible(true);
        floatingLayer.setVisible(true);
        subLabel.setBackground(Color.YELLOW);

        mainPanel.add(constantLayer, new Integer(0), 0);
        constantLayer.setBounds(0,0,640,480);
        mainPanel.add(floatingLayer, new Integer(-1), 0);
        floatingLayer.setBounds(100, 360, 300, 90 );

        frame.add(mainPanel, BorderLayout.CENTER);

        //Display the window.
        mapKeyToAction(frame.getRootPane(), 
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
                KeyStroke.getKeyStroke(KeyEvent.VK_F7, 0),
                "Hide Layer", 
                new AbstractAction() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("F7 pressed");
                        mainPanel.setLayer(floatingLayer, new Integer(-1));
                    }       
                }); 
        mapKeyToAction(frame.getRootPane(), 
                JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT,
                KeyStroke.getKeyStroke(KeyEvent.VK_F8, 0),
                "Show Layer", 
                new AbstractAction() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        System.out.println("F8 pressed");
                        mainPanel.setLayer(floatingLayer, new Integer(1));
                    }       
                }); 
        frame.pack();
        frame.setVisible(true);
        frame.getRootPane().setFocusable(true);
        boolean ok = frame.getRootPane().requestFocusInWindow();
        System.out.println("focus ok: " + ok);

    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new MyLayeredPaneDemo().createAndShowGUI();
            }
        });
    }

    private static void mapKeyToAction(JComponent component, 
            int whichMap, KeyStroke keystroke,String key, Action action) {
        component.getInputMap(whichMap).put(keystroke, key);
        component.getActionMap().put(key, action);
    }

}

但是,在我的实际案例中,我无法使其工作 . 两者之间的区别在于,在这里,我的JLayeredPane由Frame拥有,而在我的实际应用程序中,我希望JaayeredPane由JPanel拥有,是在Frame的包含层次结构中的某些级别,其大小是由其父节点中的GridBagLoyout设置,因此在调用其构造函数时大小是不可知的,这使得我很难调用我需要对JLayeredPane的子节点执行的setBounds() .

进一步补充 . 我知道Oracle Tutorials提到了case where Layouts rather than absolute positioning is used with a JLayeredPane . 这种情况与我的区别在于,在我的情况下,层在不同层上占据相同的水平空间,而在这种情况下,不同层上的组件占据不同的水平空间 . 这几乎就像我们需要一个3D布局管理器!

2 回答

  • 3

    "By default, a layered pane has no layout manager." - How to Use Layered Panes

    附录:我需要避免使用 Frame 的分层窗格,而是在窗口中添加分层窗格 .

    是的,The Root PaneJRootPane 的一个实例,其中包含 JLayeredPane . 特别是"The layered pane contains the menu bar and content pane, and enables Z-ordering of other components."

    enter image description here

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FrameDemo {
    
        private static void createAndShowGUI() {
            JFrame frame = new JFrame("FrameDemo");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            JLayeredPane mainLayer = new JLayeredPane();
            frame.add(mainLayer, BorderLayout.CENTER);
    
            JLabel label = new JLabel("LABEL", JLabel.CENTER);
            label.setBounds(100, 100, 200, 100);
            label.setOpaque(true);
            label.setBackground(Color.cyan);
            mainLayer.add(label, 1);
    
            JPanel subLayer = new JPanel(new BorderLayout());
            JLabel subLabel = new JLabel("SUBLABEL", JLabel.CENTER);
            subLabel.setBounds(20, 20, 200, 100);
            subLabel.setOpaque(true);
            subLabel.setBackground(Color.yellow);
            subLayer.add(subLabel, BorderLayout.SOUTH);
            mainLayer.add(subLabel, 2);
    
            frame.pack();
            frame.setSize(320, 240);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    
        public static void main(String[] args) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    }
    
  • 1

    我提出的解决方案,感谢trashgod,我期望这也是一个好建议,就是实现ComponentListener并捕获组件resize事件 . 此时,您可以获取容器的实际边界,并使用它来设置JPanels层的实际边界,这些边界始终与包含它们的组件的边界有某种固定的关系 . 有用 .

    Trashgod的解决方案也可行,我相信但我没有尝试过 .

相关问题