java swing 图层,图形不显示在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);

}

}

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?

解决方案

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

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值