2

Как добавить scroll на JLayerdPane для картинки в Jlabel(находится на Jpanel), как я сделал не работает, что не так?

  JLayeredPane formaPanel = new JLayeredPane();
    formaPanel.setBackground(Color.GRAY);
    formaPanel.setBounds(96, 0, 700, 700);
    formaPanel.setLayout(new BoxLayout(formaPanel, BoxLayout.Y_AXIS));
    formaPanel.setLayout(null);

BufferedImage img;
    JLabel label;
    
    try {
        img = ImageIO.read(new File(patch));
        ImageIcon icon = new ImageIcon(img);
        label = new JLabel(icon);
        label.setBounds(1, 1, 484, 693);
        formaPanel.add(label);
        formaPanel.setLayer(label, 1);
        JScrollPane  bar = new JScrollPane(label);
        bar.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
        formaPanel.add(bar);
    } catch (IOException e) {
    
        e.printStackTrace();
    }
    

1 ответ 1

2
+50

введите сюда описание изображения

    import javax.swing.*;  
    import java.awt.*;  
    import javax.imageio.*;
    import java.awt.Image;
    import java.io.File;
    import java.io.IOException;
    import java.awt.image.BufferedImage;
    
    public class LayeredPaneExample extends JFrame {  
      public LayeredPaneExample() {  
        super("LayeredPane Example");  
          setSize(500, 500);   
          JLayeredPane pane = getLayeredPane();  
          JLayeredPane formaPanel = new JLayeredPane();
          formaPanel.setBounds(0, 0, 400, 400);
          formaPanel.setLayout(new BoxLayout(formaPanel, BoxLayout.Y_AXIS));
          formaPanel.setLayout(null);
          BufferedImage img;
          JLabel label;
          JScrollPane scroll;
          try {
              img = ImageIO.read(new File("./1609607949310.jpeg"));
              ImageIcon icon = new ImageIcon(img);
              label = new JLabel(icon);

              // добавление скролбара к label 
              scroll = new JScrollPane(label, 
                JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
                JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
              formaPanel.add(scroll);
              scroll.setBounds(1, 1, 300, 300); // важно

              pane.add(formaPanel);  
          } catch (IOException e) {
              e.printStackTrace();
          }
      }  
      public static void main(String[] args) {  
          LayeredPaneExample panel = new  LayeredPaneExample();  
          panel.setVisible(true);  
      }  

}   
0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.