JScrollPane中的JLayeredPane并没有显示JLabel

时间:2014-07-13 18:12:11

标签: java swing jlabel jlayeredpane

我想向JLayeredPane添加JScrollPaneJLayeredPane有一个JPanel,图片为默认图层,上面有JLabel个图片。 我无法在JLabel上显示JPanel。你能救我吗?

我的代码是:

public class Ok extends JLayeredPane {

    private BufferedImage pag;

    /**
     * Create the panel.
     */
    public Ok(BufferedImage pagina) {
        this.pag=pagina;
        setLayout(new OverlayLayout(this));
        this.setPreferredSize(new Dimension(this.pag.getWidth(), this.pag.getHeight()));
        this.setLocation(0, 0);
        this.add(new ImagePanel(pag.getImg()), JLayeredPane.DEFAULT_LAYER);

        //this.add(new ImagePanel(pagina.getImg()), new Integer(1),0);
        JLabel lb= new JLabel();


        lb.setName("ciao");
        lb.setLocation(50, 50);
        lb.setSize(500, 300);
        lb.setBorder(BorderFactory.createLineBorder(Color.RED));
        lb.setBackground(Color.RED);


        this.add(lb, new Integer(2),0);
        lb.setVisible(true);
        setOpaque(true);
        setVisible(true);

    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            private String path;

            public void run() {

                JFrame frame= new JFrame();
                frame.setTitle("Finestra");
                frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                frame.setBounds(100, 100, 553, 489);
                BufferedImage pag=null;
                try {
                    pag  =ImageIO.read(new File("image.bmp"));
                    Ok layered = new Ok(pag);
                    JScrollPane sp= new JScrollPane(layered);
                    sp.setPreferredSize(new Dimension(pagina.getWidth(),pagina.getHeight()));
                    sp.setBorder(new EmptyBorder(5, 5, 5, 5));
                    sp.setVisible(true);
                    frame.setContentPane(sp);
                    frame.setVisible(true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

0 个答案:

没有答案