簡體   English   中英

不顯示JScrollPane

[英]JScrollPane isn't displayed

String test[] = {"1", "2", "3", "4", "5", "6", "7", "75" };
    list = new JList(test);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setLayoutOrientation(JList.VERTICAL);
    list.setVisibleRowCount(5);
    list.setLocation(50,159);
    list.setSize(50, 100);

    JScrollPane jScrollPane1= new JScrollPane();
    jScrollPane1.setViewportView(list);

    add(jScrollPane1);

編輯:更新我的代碼,你們說。 還是行不通 :(

所以這基本上是我列表的整個代碼。 當我運行程序時,您可以看到列表。 但你只能看到前5個數字,不能滾動或類似的東西。 我知道,顯示前5個數字是有意的。

那么如果沒有滾動條,我該怎么辦?

順便說一句,我用谷歌搜索多年,所以不要帶着這樣一個惱人的答案......

感謝閱讀和幫助我^^

嘗試這個

JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
getContentPane().setLayout(null);// here u specify layout put the layout what u want
getContentPane().add(jScrollPane1);// add to the contentPane
jScrollPane1.setBounds(126, 63, 100, 100);// here we set coordinates x, y width height cause we have null layout
pack();// Size the frame.

注意您應始終提供layoutManager

所以你不必將setBounds“硬編碼”

JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
setLayout(new BorderLayout());
add(jScrollPane1);// add to the frame
pack();// Size the frame.

另外一個好的做法是使用頂級容器添加到Frame,例如,你可以使用JPanel

setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new VerticalLayout()); // this layout is in swingx package
JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
setLayout(new BorderLayout());
panel.add(jScrollPane1);// add to the frame
add(panel);
pack();// Size the frame.

我試過這個測試應用程序(確實完全是nachokk建議的)並且我有一個功能完善的滾動面板

package seronis.testing;

import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;

@SuppressWarnings("serial")
public class FrameTest extends JFrame {

    public FrameTest(String string) {
        super(string);
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(800,600));

        String test[] = {"alpha", "bravo", "charlie", "delta", "echo", "omega", "zeta" };
        JList list = new JList(test);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        list.setLayoutOrientation(JList.VERTICAL);
        list.setVisibleRowCount(5);
        list.setBounds(50, 150, 75, 90);

        JScrollPane jScrollPane1= new JScrollPane();
        jScrollPane1.setViewportView(list);

        panel.add(jScrollPane1);

        this.add(panel);
        this.pack();
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setResizable(false);
        this.setFocusable(true);
        this.setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new FrameTest("Quick Test");
                frame.setVisible(true);
            }
        });
    }

}

如果沒有進一步的證據,您可能會放棄使用布局管理器(或使用錯誤的布局管理器)。

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout()); // This is the default layout, but I added it as an example

String test[] = {"1", "2", "3", "4", "5", "6", "7", "75" };
list = new JList(test);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisibleRowCount(5);

JScrollPane jScrollPane1= new JScrollPane()
jScrollPane1.setViewportView(jList1);
add(jScrollPane1);// add to the contentPane
frame.pack();// Size the frame.

布局管理器為您的應用程序提供重要支持,並克服大多數UI開發人員面臨的最煩人和最耗時的問題,幾乎任何語言; 簡單地扔掉它們是一個非常糟糕的主意,不應該這么做。

很容易想到沒有它們就可以做到; 但是當你的UI變得復雜並且你想支持可調整大小的窗口時(以及處理滾動窗格時,你應該),布局管理器將挽救你的生命。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM