Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 组件添加到JPanel的顺序是什么?_Java_Swing_Paintcomponent - Fatal编程技术网

Java 组件添加到JPanel的顺序是什么?

Java 组件添加到JPanel的顺序是什么?,java,swing,paintcomponent,Java,Swing,Paintcomponent,我有一个小应用程序,可以演示不透明属性如何在Swing中工作。 然而,让我感到困惑的是调用paintComponent()的顺序。我认为组件是按照添加的顺序绘制的(先添加什么,先绘制什么),但是在本例中,paintComponent()方法是按照相反的顺序绘制的(最后添加的内容先绘制) 有人能解释一下这种行为吗,谢谢 public class TwoPanels { public static void main(String[] args) { JPanel p =

我有一个小应用程序,可以演示不透明属性如何在Swing中工作。 然而,让我感到困惑的是调用
paintComponent()
的顺序。我认为组件是按照添加的顺序绘制的(先添加什么,先绘制什么),但是在本例中,
paintComponent()
方法是按照相反的顺序绘制的(最后添加的内容先绘制) 有人能解释一下这种行为吗,谢谢

public class TwoPanels {

    public static void main(String[] args) {

        JPanel p = new JPanel();
        // setting layout to null so we can make panels overlap
        p.setLayout(new BorderLayout());

        CirclePanel topPanel = new CirclePanel("topPanel1");
        // drawing should be in blue
        topPanel.setForeground(Color.blue);
        // background should be black, except it's not opaque, so 
        // background will not be drawn
        topPanel.setBackground(Color.black);
        // set opaque to false - background not drawn
        topPanel.setOpaque(false);
        topPanel.setBounds(50, 50, 100, 100);
        // add topPanel - components paint in order added, 
        // so add topPanel first
        p.add(topPanel);

        CirclePanel bottomPanel = new CirclePanel("buttomPanel1");
        // drawing in green
        bottomPanel.setForeground(Color.green);
        // background in cyan
        bottomPanel.setBackground(Color.cyan);
        // and it will show this time, because opaque is true
        bottomPanel.setOpaque(true);
        bottomPanel.setBounds(30, 30, 100, 100);
        // add bottomPanel last...
        p.add(bottomPanel);

        // frame handling code...
        JFrame f = new JFrame("Two Panels");
        f.setContentPane(p);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(300, 300);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    // Panel with a circle drawn on it.
    private static class CirclePanel extends JPanel {
        String objName;
        public CirclePanel(String objName) {

            this.objName = objName;
        }

        // This is Swing, so override paint*Component* - not paint
        protected void paintComponent(Graphics g) {
            System.out.println(objName);
            // call super.paintComponent to get default Swing 
            // painting behavior (opaque honored, etc.)
            super.paintComponent(g);
            int x = 10;
            int y = 10;
            int width = getWidth() - 20;
            int height = getHeight() - 20;
            g.fillArc(x, y, width, height, 0, 360);
        }
    }
}
发件人:

引擎盖下面出了什么问题?

容器包含一个包含所有子组件的数组。对于绘制,Swing(更精确地说是
JComponent#paintChildren())
以相反的顺序在数组上迭代-这意味着最后将绘制第一个添加的组件。Z顺序修改此数组中的子位置。如果布局管理器使用
容器#getComponents()
(就像许多Swing core布局管理器一样),则不能保证数组顺序表示组件添加到容器中的顺序

通常,在Swing中,您可以通过应用组件Z顺序来指定绘制顺序(请参见
容器#setComponentZOrder)
。只要使用空布局或使用约束的布局管理器,此方法就很有用

使用
#setComponentZOrder
的缺点是它会影响组件的位置

来自:

引擎盖下面出了什么问题?

容器包含一个包含所有子组件的数组。对于绘制,Swing(更精确地说是
JComponent#paintChildren())
以相反的顺序在数组上迭代-这意味着最后将绘制第一个添加的组件。Z顺序修改此数组中的子位置。如果布局管理器使用
容器#getComponents()
(就像许多Swing core布局管理器一样),则不能保证数组顺序表示组件添加到容器中的顺序

通常,在Swing中,您可以通过应用组件Z顺序来指定绘制顺序(请参见
容器#setComponentZOrder)
。只要使用空布局或使用约束的布局管理器,此方法就很有用

使用
#setComponentZOrder
的缺点是它会影响组件的位置