Java 在 JLabel 上使用 JScrollPane
Using JScrollPane on JLabel in Java
不知何故我没有显示滚动窗格。我需要更改什么?
bigP = new JLabel();
setLayout(new BorderLayout());
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
helper.setPreferredSize(new Dimension(500,600));
helper.add(new JScrollPane(bigP, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
picPane = new JPanel(new BorderLayout());
picPane.add(helper,BorderLayout.CENTER);
picPane.setMaximumSize(new Dimension(500, 600));
picPane.setVisible(true);
add(picPane, BorderLayout.CENTER);
选择图像后,此行称为:
bigP.setIcon(img);
我发现我肯定需要辅助面板,因为 BorderLayout 只需要一个组件(据我所知)。
不幸的是,尽管图片显示了,但我的滚动窗格根本不会显示。
helper.setPreferredSize(new Dimension(500,600));
不要硬编码首选尺寸。面板将根据添加到面板的组件确定自己的首选尺寸。
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
sc = new JScrollPane(bigP,JScrollPane
另外一个组件只能有一个父组件。在上面的代码中,您尝试将 "bigP" 添加到 "helper"。但随后在下一条语句中将其添加到滚动窗格,因此 "bigP" 从 "helper" 面板中删除,只会出现在滚动窗格中。
//pic.add(bigP,BorderLayout.CENTER);
pic.add(helper,BorderLayout.CENTER);
此外,您永远不会将滚动窗格添加到 "pic" 面板。代码应该是:
//pic.add(bigP,BorderLayout.CENTER);
//pic.add(helper,BorderLayout.CENTER);
pic.add(sc, BorderLayout.CENTER);
所以现在你应该有一个如下所示的结构:
- pic
- sc
- bigP
如果您使用更具描述性的名称,这样每个人都知道这些变量是什么,这也会有所帮助。
不知何故我没有显示滚动窗格。我需要更改什么?
bigP = new JLabel();
setLayout(new BorderLayout());
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
helper.setPreferredSize(new Dimension(500,600));
helper.add(new JScrollPane(bigP, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS));
picPane = new JPanel(new BorderLayout());
picPane.add(helper,BorderLayout.CENTER);
picPane.setMaximumSize(new Dimension(500, 600));
picPane.setVisible(true);
add(picPane, BorderLayout.CENTER);
选择图像后,此行称为:
bigP.setIcon(img);
我发现我肯定需要辅助面板,因为 BorderLayout 只需要一个组件(据我所知)。 不幸的是,尽管图片显示了,但我的滚动窗格根本不会显示。
helper.setPreferredSize(new Dimension(500,600));
不要硬编码首选尺寸。面板将根据添加到面板的组件确定自己的首选尺寸。
JPanel helper = new JPanel(new FlowLayout());
helper.add(bigP);
sc = new JScrollPane(bigP,JScrollPane
另外一个组件只能有一个父组件。在上面的代码中,您尝试将 "bigP" 添加到 "helper"。但随后在下一条语句中将其添加到滚动窗格,因此 "bigP" 从 "helper" 面板中删除,只会出现在滚动窗格中。
//pic.add(bigP,BorderLayout.CENTER);
pic.add(helper,BorderLayout.CENTER);
此外,您永远不会将滚动窗格添加到 "pic" 面板。代码应该是:
//pic.add(bigP,BorderLayout.CENTER);
//pic.add(helper,BorderLayout.CENTER);
pic.add(sc, BorderLayout.CENTER);
所以现在你应该有一个如下所示的结构:
- pic
- sc
- bigP
如果您使用更具描述性的名称,这样每个人都知道这些变量是什么,这也会有所帮助。