无法在Java中设置TabbedPane大小

时间:2016-05-04 10:51:17

标签: java jtabbedpane

我正在尝试创建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++;

}
}

结果如下:

enter image description here

1 个答案:

答案 0 :(得分:1)

pnlTemp = new JPanel(); JPanel的默认布局为FlowLayout。因此,在FlowLayout中,您无法按setBounds()设置排名。您可以了解如何使用布局管理器here。选择其中一个来实现您想要的设计。