5

JTabbedPaneNimbus のルック アンド フィールを使用してアプリケーションを作成していました

このコードを使用してタブを配置しました:

pane.addTab("Welcome",new ImageIcon("resources\\1.png"),mainPanel,"Takes to the welcome page");

アイコンを左側に表示したいのですが、

アプリケーションのスクリーンショット

4

2 に答える 2

9

JTabbedPane.setTabComponentAt(int index, Component component)メソッドを使用して、タブ タイトルをレンダリングするためのカスタム コンポーネントを設定できます。

指定されたタブのタイトルのレンダリングを担当するコンポーネントを設定します。null 値はJTabbedPane、指定されたタブのタイトルやアイコンをレンダリングすることを意味します。null 以外の値は、コンポーネントがタイトルをJTabbedPaneレンダリングし、タイトルやアイコンをレンダリングしないことを意味します。

注: コンポーネントは、開発者が既にタブ付きペインに追加したものであってはなりません。

たとえば、これを行うことができます:

JLabel label = new JLabel("Tab1");
label.setHorizontalTextPosition(JLabel.TRAILING); // Set the text position regarding its icon
label.setIcon(UIManager.getIcon("OptionPane.informationIcon"));

JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);
tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, label); // Here set the custom tab component

スクリーンショット 1:

ここに画像の説明を入力


注:この機能を使用すると、必要に応じて設定できComponentます。たとえば、 aJPanelで aを作成しJButtonてタブを閉じることができます。

final JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.LEFT);

ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton)e.getSource();
        for(int i = 0; i < tabbedPane.getTabCount(); i++) {
            if(SwingUtilities.isDescendingFrom(button, tabbedPane.getTabComponentAt(i))) {
                tabbedPane.remove(i);
                break;
            }
        }
    }
};

JLabel label = new JLabel("Tab1", UIManager.getIcon("OptionPane.informationIcon"), JLabel.RIGHT);        
JButton closeButton = new JButton("X");
closeButton.addActionListener(actionListener);

JPanel tabComponent = new JPanel(new BorderLayout());
tabComponent.add(label, BorderLayout.WEST);
tabComponent.add(closeButton, BorderLayout.EAST);

tabbedPane.addTab(null, new JPanel());
tabbedPane.setTabComponentAt(0, tabComponent); // Here set the custom tab component

スクリーンショット 2:

ここに画像の説明を入力


アップデート

このトピックもご覧になることをお勧めします: JTabbedPane: タブの配置が LEFT に設定されていますが、アイコンが整列されていません

于 2013-11-05T11:53:16.253 に答える