[英]Change the background color behind the tabs of a JTabbedPane?
问题中提到了这个问题。 我打算更改下图中标有“X”的区域的颜色,但我不知道该怎么做。 我尝试更改JTabbedPane
背景,但没有奏效。
对于我可能犯的任何英语错误,我提前道歉。
public class MainClass {
public static void main(String[] args) {
JFrame frame = new JFrame("TabbedPane Error"); // creates 2. JFrame
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setBackground(Color.RED);
tabbedPane.addTab("Browser",new JPanel());
tabbedPane.addTab("Asset Notifications",new JPanel());
frame.setSize(new Dimension(500,200));
frame.add(tabbedPane);
frame.setVisible(true);
}
}
好的,我想通了。 您需要指定自己的自定义 UI。
BasicTabbedPaneUI
类并覆盖paint
。tabbedPane.setUI(new CustomTabbedUI(Color.RED));
我挖出的这个非常古老的答案是部分正确的。 显然它是不完整的。
package q60855752;
import javax.swing.*;
public class Launcher {
public static void main(String[] args) {
SwingUtilities.invokeLater(new App());
}
}
package q60855752;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
public class App extends JPanel implements Runnable {
public App() {
super(new BorderLayout());
JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.setUI(new CustomTabbedUI(Color.RED));
// Modified: https://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html
addPanel(tabbedPane, "Tab 1", "Panel #1", "Does nothing", KeyEvent.VK_1, null);
addPanel(tabbedPane, "Tab 2", "Panel #2", "Does twice as much nothing", KeyEvent.VK_2, null);
addPanel(tabbedPane, "Tab 3", "Panel #3", "Still does nothing", KeyEvent.VK_3, null);
addPanel(tabbedPane, "Tab 4", "Panel #4", "Panel #4 (has a preferred size of 410 x 50).", KeyEvent.VK_4, new Dimension(410, 50));
add(tabbedPane, BorderLayout.CENTER);
}
@Override
public void run() {
JFrame frame = new JFrame("App");
frame.setPreferredSize(new Dimension(500, 200));
frame.setContentPane(new App());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
protected void addPanel(JTabbedPane tabbedPane, String title, String content, String tooltip, int mnemonic, Dimension dimensions) {
JComponent panel = createPanel(title);
if (dimensions != null) panel.setPreferredSize(dimensions);
tabbedPane.addTab(content, null, panel, tooltip);
tabbedPane.setMnemonicAt(tabbedPane.getTabCount() - 1, mnemonic);
}
protected JComponent createPanel(String text) {
JPanel panel = new JPanel(false);
JLabel filler = new JLabel(text);
filler.setHorizontalAlignment(JLabel.CENTER);
panel.setLayout(new GridLayout(1, 1));
panel.add(filler);
return panel;
}
}
package q60855752;
import javax.swing.*;
import javax.swing.plaf.basic.BasicTabbedPaneUI;
import java.awt.*;
public class CustomTabbedUI extends BasicTabbedPaneUI {
private Color backgroundColor;
public CustomTabbedUI(Color backgroundColor) {
super();
this.backgroundColor = backgroundColor;
}
public void setBackgroundColor(Color backgroundColor) {
this.backgroundColor = backgroundColor;
}
@Override
public void paint(Graphics g, JComponent c) {
Rectangle bounds = tabPane.getBounds();
g.setColor(this.backgroundColor);
g.fillRect(0, 0, bounds.width, bounds.height);
super.paint(g, c); // Call parent...
}
}
我打算更改标有“X”的区域的颜色
我看到的唯一“X”在框架的标题栏中。 您无法使用 Swing 更改它。
如果要更改选项卡绘制位置后面的背景,则需要更改添加选项卡式窗格的组件的背景。 在您的示例中,您可以使用:
frame.getContentPane().setBackground(Color.RED)
frame.add(tabbedPane);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.