简体   繁体   English

在JScrollable JPanel中添加JLabel

[英]Add JLabel in JScrollable JPanel

i am new on Java. 我是Java新手。 In fact i put a J Label inside a J Panel. 实际上,我在J面板中放置了J标签。 I have set the layout of J Panel to null , then i have set the bounds of J Label greater than the width and height of the panel . 我将J Panel的布局设置为null,然后将J Label的边界设置为大于面板的宽度和高度。 Surely it will not be visible , but i have add a JScrollPane to the Panel , but it is still not visible . 当然,它不会显示,但是我已经向面板添加了JScrollPane,但是它仍然不可见。 Is it possible or impossible , or is there any other method? 是可能还是不可能,或者还有其他方法吗?

    JPanel pnel = new JPanel();
    pnel.setLayout(null);
    JScrollPane txtstmtPane = new JScrollPane(pnel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);

    ImageIcon icon=new ImageIcon ( "icon/filter.jpg" );
    Image image = ImageIO.read(new File("icon/filter.jpg"));
    JLabel label = new JLabel();
    label.setIcon(icon);
    label.setBounds(500, 500, 40, 30);
    pnel.add(label);

Do this 做这个

  1. Forget the null layout, take that out. 忘记null布局,把它拿出来。
  2. Forget the setBounds take that out. 忘了setBounds把它拿出来。
  3. Put your icon directory straight in the src --> src/icon/filter.jpg icon目录直接放在src > src/icon/filter.jpg
  4. Use 采用

     ImageIcon icon = new ImageIcon(getClass.getResource("/icon/filter.jpg"); JLabel label = new JLabel(icon); pnel.add(label); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM