1

I'm playing around with some swing guis and am trying to create a basic program. This program is going to have a tabbed pane with a varying amount of tabs depending on the size of an array. (My end goal is to have users change the amount of items in the array, therefore changing the amount of tabs).

Each tab is going to have the exact same components, text area, table and a few buttons and labels. What I would like to do is instead of coding these tabs individually and rewriting my code over and over what I want to do is create a class to put all my components into.

I am however kind of stumped. This is my class for creating the tabs:

public class LocaleTab {

public LocaleTab(){
    JPanel tab = new JPanel();
    JLabel label = new JLabel();
    label.setPreferredSize(new Dimension(300, 300));

    tab.add(label);
}
}

And here's my code that I'm trying to call with it:

    LocaleTab tab1 = new LocaleTab();
    JTabbedPane localesTabPane = new JTabbedPane();

    localesTabPane.add(tab1);

I'm getting an error when I try and compile this. I'm thinking my methodology is probably completely wrong.

The method add(Component) in the type JTabbedPane is not applicable 
    for the arguments (LocaleTab)

One are that concerns me is when I try to use the data in the tables and text areas in each tab(event listeners is what I'll be using i think? I haven't gotten to that stage yet though!) how will I target the individual tabs components?

4

2 に答える 2

3

への変更:

public class LocaleTab extends JPanel {
    public LocaleTab(){
        JLabel label = new JLabel();
        label.setPreferredSize(new Dimension(300, 300));
        add(label);
    }
}
于 2012-05-02T16:29:47.740 に答える
0

おそらくあなたはこれに近いものを探しています:

public class LocaleTab {

    private JPanel tab;

    public LocaleTab() {
        tab = new JPanel();
        JLabel label = new JLabel();
        label.setPreferredSize(new Dimension(300, 300));

        tab.add(label);
    }

    public JPanel getTabPanel() {
        return tab;
    }
}

そしてLocaleTab、下図のように使用します。

LocaleTab tab1 = new LocaleTab();
JTabbedPane localesTabPane = new JTabbedPane();

localesTabPane.add(tab1.getTabPanel());

さらに、JTabbedPane がどのように機能するかについては、こちらをご覧ください:タブ付きペインの使用方法

于 2012-05-02T17:45:53.387 に答える