我有一个带有按钮的 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() 的事情来返回它所在的选项卡,但我错了。
有任何想法吗?