图形不显示在JLayeredPane(java swing) [英] Graphics not showing in JLayeredPane (java swing)

查看:580
本文介绍了图形不显示在JLayeredPane(java swing)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

然而,我的图形似乎并没有显示出来,我不明白为什么。我也尝试将它添加到 JPanel 中,并将 JPanel 添加到 JLayeredPane 然而,这也没有奏效。

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?

推荐答案

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

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

这篇关于图形不显示在JLayeredPane(java swing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆