【发布时间】:2012-06-03 19:00:38
【问题描述】:
我有这段代码,我试图在其中安装一个可滚动面板 (JPanel),但我不明白。这是我的代码:
public class Sniffer_GUI extends JFrame {
Canvas c = new Canvas();
ConnectorPropertiesPanel props;
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new GridBagLayout());
init();
getContentPane().add(new JLabel("Connectors example. You can drag the connected component to see how the line will be changed"),
new GridBagConstraints(0, 0, 2, 1, 1, 0, GridBagConstraints.NORTHWEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 0, 5), 0, 0));
getContentPane().add(initConnectors(),
new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
getContentPane().add(props,
new GridBagConstraints(1, 1, 1, 1, 0, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.VERTICAL, new Insets(5, 0, 5, 5), 0, 0));
setSize(800, 600);
setLocationRelativeTo(null);
}
提前致谢。
我编辑添加了部分似乎有效的代码...
public Sniffer_GUI() {
super("JConnector demo");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane);
scrPane.setLayout(new ScrollPaneLayout());
init();
add(initConnectors());
setSize(800, 600);
setLocationRelativeTo(null);
}
但它仍然不能滚动,至少它在 JScrollPane 中实现了它的功能,这是一个很好的步骤。
【问题讨论】:
-
尝试使用 JScrollPane 的代码在哪里?
-
@Puce 也许他不知道 JScrollPane。
-
我只知道声明 JScrollPane 变量... =(
-
@JoeLewis 我什至在您的代码中都没有看到这个 JScrollPane 变量。我建议阅读 Swing 教程:docs.oracle.com/javase/tutorial/ui/index.htmldocs.oracle.com/javase/tutorial/ui/features/components.html
-
@Puce,我用 JScrollPane 添加了编辑后的代码,但它仍然不能滚动......
标签: java swing user-interface jframe jscrollpane