【问题标题】:How to make a JFrame scrollable in Java?如何在 Java 中使 JFrame 可滚动?
【发布时间】: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 中实现了它的功能,这是一个很好的步骤。

【问题讨论】:

标签: java swing user-interface jframe jscrollpane


【解决方案1】:

试试这个:

JScrollPane sp = new JScrollPane();
this.add(sp).
sp.add( *GUI elements for your applications.*)

类似的东西应该适合你。也可以看看this

【讨论】:

  • 向滚动窗格添加元素通常不是您想要的。通常,您将容器面板传递给 JScrollPane 的构造函数,或者使用setViewportView
【解决方案2】:

使 JPanel 可滚动并将其用作容器,如下所示:

JPanel container = new JPanel();
JScrollPane scrPane = new JScrollPane(container);
add(scrPane); // similar to getContentPane().add(scrPane);
// Now, you can add whatever you want to the container

【讨论】:

  • 我在尝试添加元素时遇到了 scrPane 的布局问题,我该如何做与原始代码类似的事情?
  • @JoeLewis 不要对内容窗格执行任何操作,而是在 Eng.Fouad 的答案中描述的容器上执行此操作,然后只需将滚动窗格添加到内容窗格(保留默认布局 BorderLayout,如内容窗格的布局管理器)
  • @Guillaume Polet ok 完美的 Eng.Fouad 的代码在我在原始帖子中编辑时部分工作,它工作但仍不能滚动......
  • @JoeLewis 我根据您的代码和 Eng.Fouad 提案发布和回答。
【解决方案3】:

扩展@Eng.Fouad 答案:

public class Sniffer_GUI extends JFrame {
    Canvas c = new Canvas();
    ConnectorPropertiesPanel props;
    public Sniffer_GUI() {
        super("JConnector demo");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel container = new JPanel();
        JScrollPane scrPane = new JScrollPane(container);
        getContentPane().add(scrPane);
        container.setLayout(new GridBagLayout());
        init();

        container.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));
        container.add(initConnectors(),
                             new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(5, 5, 5, 5), 0, 0));
        container .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);

    }
}

【讨论】:

  • 我的英雄... xD 现在我只需要让它垂直滚动,因为它只是水平的...:P
  • @JoeLewis 你可以设置垂直和水平滚动条策略来调整这个东西。考虑阅读ScrollPane tutorial 及其JavaDoc
  • 我的问题是,iniConnectors() 检索一个 JPanel,我需要使该面板可滚动,因为它是最重要的,但是我什至无法将 initConnector 添加到另一个 JScrollPane 然后执行container.add("新的 JScrollPane", etc...);
  • @JoeLewis 我怀疑您在这里需要嵌套的 ScrollPane,但如果没有看到您的所有代码,我无法确定。如果您只想滚动应用到您的 JPanel initConnector,请保留您的原始代码,但使用以下代码:getContentPane().add(new JScrollPane(initConnectors()),... 而不是 getContentPane().add(initConnectors(),...
【解决方案4】:

要使 JFrame 的组件可滚动,请将组件包装在 JScrollPane 中:

    JScrollPane myJScrollPane = new JScrollPane(myJLabel,
         JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
         JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

并用 myJScrollPane 替换提及 myJLabel。为我工作。

【讨论】:

  • 感谢您在 JScrollPane 的构造函数中显示 JScrollPane.VERTICAL_SCROLLBAR_​​AS_NEEDED 和 JScrollPane.HORIZONTAL_SCROLLBAR_​​AS_NEEDED。在我的answer 中,我改用 setVerticalScrollBarPolicy 和 setHorizo​​ntalScrollBarPolicy 方法。感谢您提供这种可能性。
【解决方案5】:

只是插话,如果我偏离了基础,请随时纠正我,但使用这样的 JScrollPane 会导致需要更频繁地调整窗口大小的意外后果。

例如,我有一个程序,我在其中设置了类似的 JFrame 范围的滚动窗格。我还在框架中的一个选项卡中有一个 JTextArea,它与内容窗格的大小相同。这个 textArea 也在它自己的滚动窗格中(这更像是一个搞砸的项目而不是其他任何东西)。当我从文件加载内容以存放在 textArea 中时,它触发了文本区域周围的滚动条。

结果是,我的,我们称之为 innerScrollPane,现在比 JFrame 大,因为滚动条以前不可见。然后这触发了我现在要调用的外层滚动窗格,以显示其滚动条,然后覆盖内部滚动条。

这很容易通过在我的文件打开方法的末尾添加一个额外的 window.pack() 参数来解决,但我只是想把它扔掉。如果您不小心,滚动条可能会掩盖窗口中的内容。但是......好吧,有一百万种方法可以防止这个问题,所以这不是什么大不了的事。只是需要注意的事情。

【讨论】:

    【解决方案6】:

    也许这会有所帮助...

    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    
    // add something to you panel...
    // panel.add(...);
    
    // add the panel to a JScrollPane
    JScrollPane jScrollPane = new JScrollPane(panel);
    // only a configuration to the jScrollPane...
    jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
    jScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    
    // Then, add the jScrollPane to your frame
    frame.getContentPane().add(jScrollPane);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-05
      • 2017-10-28
      • 1970-01-01
      • 2015-01-11
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      相关资源
      最近更新 更多