无法在 Java 中设置 TabbedPane 大小

Can't set to TabbedPane size in Java

我正在尝试创建 tabPane 并从我的数据库中添加标签。但是我在设置 tabbedPane 的大小和位置时遇到问题。这是我的代码;

JPanel pnlTemp;
JTabbedPane tabPaneTemp;
JButton btnTemp;

int categoryCount= site.getCategoryCount();

try {

//sql. is a class which I created for connection to database. 

    ResultSet resultCategory= sql.select("SELECT * FROM CATEGORY");

    for (int i = 0; i < categoryCount; i++) {
        resultCategory.next();

        pnlTemp = new JPanel();
        tabPaneAnasayfa.addTab(resultCategory.getString("AD"), pnlTemp);

        tabPaneTemp = new JTabbedPane();
        pnlTemp.add(tabPaneTemp);

       tabPaneTemp.setSize(950, 460);
       tabPaneTemp.setBounds(35,22,950, 460);

       tabPaneTemp.setTabPlacement(JTabbedPane.BOTTOM);

       createButtonToTab(tabPaneTemp);


    }

} catch (SQLException ex) {
    Logger.getLogger(Anasayfa.class.getName()).log(Level.SEVERE, null, ex);
}



private void createButtonToTab(JTabbedPane tabPane) {
JPanel pnlTemp;
JButton btnTemp;

ResultSet resultHaber = sql.select("SELECT * FROM HABER");
ArrayList<Haber> haberList = new ArrayList();
try {
    while (resultHaber.next()) {
        int haberID;
        haberID = resultHaber.getInt("ID");
        haberList.add(site.getHaber(haberID));

    }
} catch (SQLException ex) {
    Logger.getLogger(Anasayfa.class
           .getName()).log(Level.SEVERE, null, ex);
}

int listIndexi = 0;
int haberSirasi = 1; 

int topHaberSayisi = haberList.size();
for (int i = 0; i < topHaberSayisi; i++) {

    pnlTemp = new JPanel();
    btnTemp = new JButton();

    btnTemp.setSize(pnlTemp.getSize());

    pnlTemp.setLayout(new GridLayout());

    if (!haberList.get(listIndexi).haberSonDakikaMi) {
        tabPane.addTab(String.valueOf(haberSirasi), pnlTemp);
        Haber tempHaber;
        tempHaber = haberList.get(listIndexi);

        pnlTemp.add(btnTemp);
        btnTemp.setText(haberList.get(listIndexi).haberBasligi);
        haberSirasi++;

        btnTemp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                haberOkuCagir(tempHaber.haberID);
            }
        });
    }
    listIndexi++;

}
}

结果如下:

pnlTemp = new JPanel(); JPanel 的默认布局是 FlowLayout。所以在 FlowLayout 中你不能通过 setBounds() 设置位置。您可以学习如何使用布局管理器 here。选择其中之一来实现您想要的设计。