如何在Java中使JFrame可滚动?

时间:2012-05-29 14:23:37

标签: java swing user-interface 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);

}

提前致谢。

我编辑添加部分似乎有用的代码......

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中使它的功能是一个很好的步骤。

6 个答案:

答案 0 :(得分: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

答案 1 :(得分: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);

    }
}

答案 2 :(得分: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);

答案 3 :(得分:2)

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

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

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

答案 4 :(得分:1)

如果我不在基础上,请随时加入,并且随意纠正我,但使用像这样的JScrollPane会产生意想不到的后果,即需要更频繁地调整窗口大小。

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

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

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

答案 5 :(得分:0)

试试这个:

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

这样的事情对你有用。看看this

相关问题