我使用 JTabbedPane 设计了以下布局,并且想要更改突出显示部分的颜色。但我没有办法做到这一点。
我的问题
如何改变其背景颜色?
我尝试了什么?
我尝试更改 background-color
和Foreground-color
JPanel 和 JTabbedPane 都有,但没有运气。
我也尝试过这个,同时保留 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
。它既不起作用,也不起作用 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 );
现在选项卡占据的区域将具有“父”面板的背景颜色。
如果这不起作用,则说明您遇到了外观问题。
解决方案 更改外观后问题得到解决
关于java - 更改 JTabbedPane 侧 View 的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60068019/