我正在使用JTabbedPane设计以下布局,并希望更改突出显示部分的颜色。但是我没有办法做到这一点。我的问题如何改变背景色?我尝试了什么?我试过更改JPanel和JTabbedPane的background-color
和Foreground-color
,但运气不佳。我还在从属性中保留Opaque = false
的同时尝试了此操作,但是没有运气。jTabbedPane1.setForegroundAt(1, Color.yellow);
jTabbedPane1.setForegroundAt(2, Color.yellow);
String[] tabNames = {"Signin", "General", "Call Rate", "Audio Device","tab 5","tab 6","tab 7","tab 8","tab 9"};
for (int i = 0; i < tabNames.length; i++) {
JLabel lab = new JLabel("<html><font color=white>" + tabNames[i] + "</font></html>", SwingConstants.CENTER);
lab.setForeground(Color.red);
lab.setBackground(Color.BLUE);
lab.setPreferredSize(new Dimension(200, 50));
lab.setFont(new Font("Times New Roman", Font.BOLD, 22));
jTabbedPane1.setTabComponentAt(i, lab);
}
for (int i = 0; i < this.jTabbedPane1.getTabCount(); i++) {
jTabbedPane1.setBackgroundAt(i,Color.DARK_GRAY);
}
我尝试添加JLabel
来更改显示我的标签名称的组件的大小,然后尝试设置setBackgroundAt
。它既不起作用,也不对Label起作用setBackground
和setForeground
。我的外观
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Settings.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
基本逻辑是:
JPanel tab1 = new JPanel();
tab1.background( Color.RED );
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add(tab1, "Tab1");
JPanel parent = new JPanel( new BorderLayout() );
parent.setBackground( Color.GREEN );
parent.add( tabbedPane );
frame.add( parent );
现在选项卡占用的区域将具有“父”面板的背景颜色。
如果这不起作用,则说明外观问题。