我想向JLayeredPane
添加JScrollPane
。
JLayeredPane
有一个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();
}
}
});
}
}