簡體   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