要保持JTabbedPane的指定颜色,可以使用以下代码示例:
import javax.swing.*;
import java.awt.*;
public class CustomJTabbedPane extends JTabbedPane {
private Color tabColor;
public CustomJTabbedPane(Color tabColor) {
this.tabColor = tabColor;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// 设置选项卡的背景颜色
g.setColor(tabColor);
g.fillRect(0, 0, getWidth(), getHeight());
}
public static void main(String[] args) {
JFrame frame = new JFrame("Custom JTabbedPane Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 创建自定义JTabbedPane,并设置选项卡的背景颜色为红色
CustomJTabbedPane tabbedPane = new CustomJTabbedPane(Color.RED);
// 添加选项卡
tabbedPane.addTab("Tab 1", new JPanel());
tabbedPane.addTab("Tab 2", new JPanel());
tabbedPane.addTab("Tab 3", new JPanel());
frame.add(tabbedPane);
frame.setSize(400, 300);
frame.setVisible(true);
}
}
在上面的代码中,我们创建了一个名为CustomJTabbedPane的自定义类,继承自JTabbedPane类。在CustomJTabbedPane类中,我们重写了paintComponent方法,并在其中设置选项卡的背景颜色为指定的颜色。然后,我们创建了一个CustomJTabbedPane对象,并将其添加到一个JFrame中。
在main方法中,我们创建了一个JFrame对象,并设置其标题为“Custom JTabbedPane Example”。然后,我们创建了一个CustomJTabbedPane对象,并将选项卡的背景颜色设置为红色。接下来,我们添加了几个选项卡到CustomJTabbedPane对象中,并将其添加到JFrame中。最后,我们设置JFrame的大小并显示出来。
运行上面的代码,将会显示一个带有指定颜色选项卡的JTabbedPane。
下一篇:保持聚类时的列表顺序