如何在Java中使JFrame可滚动?

Joe*_*wis 9 java user-interface swing jframe jscrollpane

我有这个代码,我试图适应可滚动的面板(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);

}
Run Code Online (Sandbox Code Playgroud)

提前致谢.

我编辑添加部分似乎工作的代码...

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);

}
Run Code Online (Sandbox Code Playgroud)

但它仍然不是可滚动的,至少它在JScrollPane中使它的功能是一个很好的一步.

Eng*_*uad 19

使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
Run Code Online (Sandbox Code Playgroud)

  • @JoeLewis而不是使用内容窗格执行的操作,在Eng.Fouad的答案中描述的容器上执行此操作,只需将滚动窗格添加到内容窗格(保留默认布局BorderLayout,作为内容窗格的布局管理器) (2认同)

Gui*_*let 6

扩展@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);

    }
}
Run Code Online (Sandbox Code Playgroud)


Fil*_*ito 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);
Run Code Online (Sandbox Code Playgroud)


Log*_*ard 0

尝试这个:

JScrollPane sp = new JScrollPane();
this.add(sp).
sp.add( *GUI elements for your applications.*)
Run Code Online (Sandbox Code Playgroud)

类似的东西应该对你有用。也看看这个:

  • 将元素添加到滚动窗格通常不是您想要的。通常,您将容器面板传递给 JScrollPane 的构造函数,或者使用 setViewportView (5认同)