如何更改JTabbedPane的背景颜色?
发布时间:2020-05-24 19:51:02 所属栏目:Java 来源:互联网
导读:我知道你可以 modify the LaF properties,但是如果不这样做你怎么做到这一点?我只是问,因为setBackground似乎没有这样做. 请注意,我正在寻找更改以下属性: TabbedPane.background(或TabbedPane.contentAreaColor?) TabbedPane.tabAreaBackground 以 TabCom
|
我知道你可以 modify the LaF properties,但是如果不这样做你怎么做到这一点?我只是问,因为setBackground似乎没有这样做. 请注意,我正在寻找更改以下属性: > TabbedPane.background(或TabbedPane.contentAreaColor?) 解决方法以TabComponentsDemo为例,setBackgroundAt()似乎有效:
private void initTabComponent(int i) {
pane.setTabComponentAt(i,new ButtonTabComponent(pane));
pane.setBackgroundAt(i,Color.getHSBColor((float)i/tabNumber,1,1));
}
附录:正如@camickr所做的那样,目标组件必须是opaque. import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/** @see https://stackoverflow.com/questions/8752037 */
public class TabColors extends JPanel {
private static final int MAX = 5;
private final JTabbedPane pane = new JTabbedPane();
public TabColors() {
for (int i = 0; i < MAX; i++) {
Color color = Color.getHSBColor((float) i / MAX,1);
pane.add("Tab " + String.valueOf(i),new TabContent(i,color));
pane.setBackgroundAt(i,color);
}
this.add(pane);
}
private static class TabContent extends JPanel {
private TabContent(int i,Color color) {
setOpaque(true);
setBackground(color);
add(new JLabel("Tab content " + String.valueOf(i)));
}
@Override
public Dimension getPreferredSize() {
return new Dimension(320,240);
}
}
private void display() {
JFrame f = new JFrame("TabColors");
f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
f.add(this);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new TabColors().display();
}
});
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
