当所有的tab被移除之后,背景上还留有原JTabbedPane所停留的痕迹(灰色边框)。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.border.EmptyBorder;
public class TabbedPaneExample extends JFrame {
JTabbedPane tabbedPane = new JTabbedPane();
JPanel tabbedPanePanel = new JPanel();
public void createAndShowGUI() {
tabbedPanePanel.setLayout(new BorderLayout());
tabbedPanePanel.setBackground(new Color(35, 119, 179));
tabbedPanePanel.setBorder(new EmptyBorder(5, 8, 5, 10));
tabbedPane.setBackground(new Color(79, 147, 196));
tabbedPane.setBorder(new EmptyBorder(0, 0, 0, 0));
tabbedPane.add(new JLabel("tab1"), "tab1");
tabbedPane.add(new JLabel("tab2"), "tab2");
tabbedPanePanel.add(tabbedPane, BorderLayout.CENTER);
JButton btn = new JButton("close all");
btn.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
tabbedPane.removeAll();
}
});
tabbedPanePanel.add(btn, BorderLayout.NORTH);
this.setLayout(new BorderLayout());
this.add(tabbedPanePanel, BorderLayout.CENTER);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(new Dimension(300, 200));
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
TabbedPaneExample example = new TabbedPaneExample();
example.createAndShowGUI();
}
});
}
}