Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.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
图形未显示在JLayeredPane(java swing)中_Java_Swing_Graphics_Jlayeredpane - Fatal编程技术网

图形未显示在JLayeredPane(java swing)中

图形未显示在JLayeredPane(java swing)中,java,swing,graphics,jlayeredpane,Java,Swing,Graphics,Jlayeredpane,我正试图逐步建立一个基于用户输入的图像。我试图做的是创建一组图形并将它们添加为图层,但是我遇到了一些问题,因为它们不会显示出来。以下是我正在使用的代码: public class ClassA { protected final static int dimesionsY = 1000; private static int dimesionsX; private static JFrame window; private static JLayeredPane

我正试图逐步建立一个基于用户输入的图像。我试图做的是创建一组图形并将它们添加为图层,但是我遇到了一些问题,因为它们不会显示出来。以下是我正在使用的代码:

public class ClassA 
{
    protected final static int dimesionsY = 1000;
    private static int dimesionsX;
    private static JFrame window;
    private static JLayeredPane layeredPane;

    public void init()
    {
        window = new JFrame("Foo");
        dimesionsX = // some user input
        window.setPreferredSize(new Dimension(dimesionsX, dimesionsY));
        window.setLayout(new BorderLayout());

            layeredPane = new JLayeredPane();
        layeredPane.setBounds(0, 0, dimesionsX, dimesionsY);
        window.add(layeredPane, BorderLayout.CENTER);

            ClassB myGraphic = new ClassB();    
        myGraphic.drawGraphic();

        layeredPane.add(myGrpahic, new Integer(0), 0);

        window.pack();
        window.setVisible(true);
    }
}



public class ClassB extends JPanel
{
    public void drawGraphic()
    {
        repaint();
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        g.setColor(Color.BLACK);
        g.fillRect(10, 10, 100, 100);
    }
}
然而,我的图形似乎没有出现,我不明白为什么。我还尝试先将它添加到
JPanel
中,然后将
JPanel
添加到
JLayeredPane
中,但是这也不起作用


有人能帮我吗?

如果将组件添加到JLayeredPane,就像使用容器将其添加到空布局:必须完全指定组件的大小和位置

e、 g

请参见Java教程中的

此外,有时还需要设置首选尺寸:


layeredPane.setPreferredSize(新尺寸(宽度、高度))

此外,如果您将JPanel放置在彼此的顶部,希望您注意不透明设置为false。解决您问题的另一个解决方案包括将缓冲区映像相互叠置。感谢您在这方面的帮助。我注意到您在添加图形时使用了
JLayeredPane.DEFAULT\u LAYER
。我只是想知道,在我想添加额外图形的情况下,什么是最好的使用方法?我会使用任何逻辑来达到最佳效果。如果我有一些我知道会在底部的东西,我会把它放在默认层。好的,这很有意义。我该如何添加一个层,使其位于所有内容之上?@JonW09:将其添加到JLayeredPane时,它只需具有最高的整数(…)常量即可。还可以查看JLayeredPane API,了解Swing使用的默认常量以及您也可以使用的默认常量。
import java.awt.*;

import javax.swing.*;

public class ClassA {
   protected final static int dimesionsY = 800;
   protected final static int dimesionsX = 1000; //!!
   private static JFrame window;
   private static JLayeredPane layeredPane;

   public void init() {
      window = new JFrame("Foo");
      // !! dimesionsX = // some user input

      //!! window.setPreferredSize(new Dimension(dimesionsX, dimesionsY));
      window.setLayout(new BorderLayout());

      layeredPane = new JLayeredPane();
      //!! layeredPane.setBounds(0, 0, dimesionsX, dimesionsY);
      layeredPane.setPreferredSize(new Dimension(dimesionsX, dimesionsY));
      window.add(layeredPane, BorderLayout.CENTER);

      ClassB myGraphic = new ClassB();
      myGraphic.drawGraphic();

      myGraphic.setSize(layeredPane.getPreferredSize());
      myGraphic.setLocation(0, 0);
      //!! layeredPane.add(myGraphic, new Integer(0), 0);
      layeredPane.add(myGraphic, JLayeredPane.DEFAULT_LAYER);

      window.pack();
      window.setVisible(true);
   }

   public static void main(String[] args) {
      new ClassA().init();
   }
}

class ClassB extends JPanel {
   public void drawGraphic() {
      repaint();
   }

   public void paintComponent(Graphics g) {
      super.paintComponent(g);

      g.setColor(Color.BLACK);
      g.fillRect(10, 10, 100, 100);
   }
}