问题
The problem is referred to in the question. I intend to change the color of the area marked with an 'X' in the image below but I don't know how to do it. I've tried to change the JTabbedPane
background but it didn't work.
I apologize in advance for any English mistakes I may have made.
Minimal reproducible example
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);
}
}
回答1:
OK, I figured it out. You need to specify your own custom UI.
- You need to extend the
BasicTabbedPaneUI
class and overridepaint
. - Then you can set the UI:
tabbedPane.setUI(new CustomTabbedUI(Color.RED));
This really old answer I dug up was partially correct. Obviously it was incomplete.
Launcher.java
package q60855752;
import javax.swing.*;
public class Launcher {
public static void main(String[] args) {
SwingUtilities.invokeLater(new App());
}
}
App.java
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;
}
}
CustomTabbedUI.java
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...
}
}
回答2:
I intend to change the color of the area marked with an 'X'
The only "X" I see is in the title bar of the frame. You can't change that using Swing.
If you want to change the background behind where the tabs are painted then you need to change the background of the component you add the tabbed pane to. In your example you can use:
frame.getContentPane().setBackground(Color.RED)
frame.add(tabbedPane);
来源:https://stackoverflow.com/questions/60855752/change-the-background-color-behind-the-tabs-of-a-jtabbedpane