0

实际上,我正在将 Jlayeredpane 添加到 Jscrollpane 中,然后再次将 Jlabel 添加到 Jlayeredpane 中。我明确地看到 jlabel 的大小,因为 jlayeredpane 的布局为空,在 Jlabel 中我正在设置图像。

但问题是这里没有显示滚动条。请帮助解决设计缺陷或指导我寻找其他替代方案。

4

1 回答 1

0

its may be you expected. but don't use null layout. JScrollPane not work with null layout.

import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class ScrollLabel extends JFrame {

    public ScrollLabel() throws Exception {
        setSize(300, 300);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panel = new JPanel(new GridLayout());
        pane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        label = new JLabel();
        ImageIcon icon = new ImageIcon(ImageIO.read(new File(getClass().getResource("baby.jpg").toURI())));
        label.setIcon(icon);
        panel.add(label);
        add(pane);
        repaint();
    }

    public static void main(String[] args) throws Exception {
        new ScrollLabel();
    }
    JPanel panel;
    JLabel label;
    JScrollPane pane;
}
于 2013-10-28T15:40:09.710 回答