繁体   English   中英

如何在Java中使JFrame可滚动?

[英]How to make a JFrame scrollable in Java?

我有这个代码,我试图适应可滚动的面板(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中使它的功能是一个很好的一步。

使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

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

    }
}

也许这会有所帮助......

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

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

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

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

只是简单地说,如果我不在基础上,请随时纠正我,但使用像这样的JScrollPane有一个意想不到的后果,需要更频繁的窗口大小调整。

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

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

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

试试这个:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM