0

そのタブを閉じたいボタンが付いたJTappedPaneがあります。私はそのようにやっています:

jTabbedPane1.addTab(title, null, panel, null);

JPanel pnl = new JPanel();
    JButton close = new JButton();
    try {
        Image img = ImageIO.read(getClass().getResource("x.png"));
        close.setIcon(new ImageIcon(img));
      } catch (IOException ex) {
          ex.printStackTrace();
      }
    close.setPreferredSize(new Dimension(10, 10));
    close.setBorderPainted(false);
    close.addActionListener(new java.awt.event.ActionListener(){

        public void actionPerformed(ActionEvent evt) {
                    //TODO CLOSE THE TAP WHEN BUTTON IS PRESSED

                }

        }});
    JLabel lab = new JLabel(s);

     pnl.setOpaque(false);
     pnl.add(lab);
      pnl.add(close);

jTabbedPane1.setTabComponentAt(jTabbedPane1.getTabCount() - 1, pnl);

ボタンが押されたタブのタブのタイトルを取得しようとしています。close.getContaining()のようなことをして、それが置かれているタブを返すことができると思いましたが、間違っていました。

何か案は?

4

2 に答える 2

2

私があなたを正しく理解しているなら、あなたはボタンの親をtabComponentとして持つタブのインデックスを見つけたいと思うでしょう:

public void actionPerformed(ActionEvent evt) {
    JComponent source = (JComponent) evt.getSource();
    Container tabComponent = source.getParent();
    int tabIndex = jTabbedPane1.indexOfTabComponent(tabComponent);
    jTabbedPane1.removeTabAt(tabIndex);
}
于 2012-11-28T12:33:27.023 に答える
0

あなたは単に書くことができます:

jTabbedPane1.removeTabAt(jTabbedPane1.getSelectedIndex());
于 2012-11-28T11:57:14.153 に答える