Как добавить scroll на JLayerdPane для картинки в Jlabel?
Как добавить 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 шт):
Автор решения: Daniil Loban
→ Ссылка
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);
}
}
