簡體   English   中英

JTable沒有顯示在UI中?

[英]JTable doesn't show up in UI?

我想在UI上創建一個示例表。 但是無論我嘗試什么都不會出現。 也許有人可以幫我嗎?

public void createGUI(){
        JFrame myframe = new JFrame("Frame");
        JButton firstButton = new JButton("Connect");


        myframe.setLayout(null);
        myframe.setVisible(true);
        myframe.setSize(500, 500);
        //myframe.add(firstButton);


        firstButton.addActionListener(new handler("ConnectButton"));
        firstButton.setSize(150, 100);
        firstButton.setLocation(100, 100);

        String[] columnNames = {"First Name",
                "Last Name",
                "Sport",
                "# of Years",
                "Vegetarian"};

        Object[][] data = {
                {"Kathy", "Smith",
                 "Snowboarding", new Integer(5), new Boolean(false)},
                {"John", "Doe",
                 "Rowing", new Integer(3), new Boolean(true)},
                {"Sue", "Black",
                 "Knitting", new Integer(2), new Boolean(false)},
                {"Jane", "White",
                 "Speed reading", new Integer(20), new Boolean(true)},
                {"Joe", "Brown",
                 "Pool", new Integer(10), new Boolean(false)}
            };

        JTable table = new JTable(data, columnNames);
        table.setVisible(true);

        //JScrollPane scrollPane = new JScrollPane(table);
        //scrollPane.setVisible(true);
        table.setFillsViewportHeight(true);
        myframe.add(table);
    }

問題是當您在以下行中將layout設置為null時:

myframe.setLayout(null);

只需刪除此行,它將很好地工作。 因為無法在布局設置為null的情況下顯示窗口。 因此,一旦刪除此行,將使用默認布局。

這是您可能需要閱讀的有關布局管理器的更多信息: https : //docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

我刪除的第二件事是這一行:

table.setFillsViewportHeight(true);

您應該使用myframe.pack(); 而是將所有組件包裝在框架上。

所以我最后說:

public static void createGUI() {
        JFrame myframe = new JFrame("Frame");
        JButton firstButton = new JButton("Connect");

        myframe.setSize(500, 500);

        String[] columnNames = {"First Name",
            "Last Name",
            "Sport",
            "# of Years",
            "Vegetarian"};

        Object[][] data = {
            {"Kathy", "Smith",
                "Snowboarding", new Integer(5), new Boolean(false)},
            {"John", "Doe",
                "Rowing", new Integer(3), new Boolean(true)},
            {"Sue", "Black",
                "Knitting", new Integer(2), new Boolean(false)},
            {"Jane", "White",
                "Speed reading", new Integer(20), new Boolean(true)},
            {"Joe", "Brown",
                "Pool", new Integer(10), new Boolean(false)}
        };

        JTable table = new JTable(data, columnNames);
        table.setVisible(true);

        myframe.add(table);
        myframe.pack();           // added this
        myframe.setVisible(true); // and moved this from top
    }

因此最終結果如下所示:

在此處輸入圖片說明

您沒有使用布局管理器[myframe.setLayout(null)],因此必須自己處理位置和大小。

嘗試添加:

  table.setLocation(1, 1);
  table.setSize(200,200);

它會工作。

暫無
暫無

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

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