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

发布于 2024-12-12 21:45:42 字数 1291 浏览 0 评论 0原文

我正在尝试根据用户输入逐步构建图像。我想做的是创建一堆图形并将它们添加为图层,但是我遇到了一些问题,因为它们不会显示。这是我正在使用的代码:

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 中,但这也不起作用。

请问有人可以帮我吗?

I'm trying to gradually build up an image based on user inputs. What I'm trying to do is create a bunch of graphics and add them as layers however I'm having some issues as they won't show up. Here is the code I'm using:

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);
    }
}

However my graphic doesn't seem to show up and I don't understand why. I have also tried add it to a JPanel first, adding that JPanel to the JLayeredPane however that didn't work either.

Please can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

堇色安年 2024-12-19 21:45:42

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

例如,

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);
   }
}

If you add a component to a JLayeredPane, it's like adding it to a null layout using container: you must fully specify the component's size and position.

e.g.,

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);
   }
}
把时间冻结 2024-12-19 21:45:42

请参阅在分层窗格中布置组件,来自< em>Java 教程。

另外,有时您需要设置首选大小:

layeredPane.setPreferredSize(new Dimension(width, height));

See Laying Out Components in a Layered Pane, from The Java Tutorials.

Also, sometimes you need to set the preferred size:

layeredPane.setPreferredSize(new Dimension(width, height));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文