更改 JTabbedPane 选项卡后面的背景颜色?
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
class 并覆盖 paint
。
- 然后你可以设置UI:
tabbedPane.setUI(new CustomTabbedUI(Color.RED));
这真的old answer我挖出来的是部分正确的。显然是不完整的。
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...
}
}
I intend to change the color of the area marked with an 'X'
我唯一看到的 "X" 是在框架的标题栏中。您不能使用 Swing 更改它。
如果您想更改绘制选项卡的背景,则需要更改添加选项卡式窗格的组件的背景。在您的示例中,您可以使用:
frame.getContentPane().setBackground(Color.RED)
frame.add(tabbedPane);
问题中提到的问题。我打算更改下图中标有 '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
class 并覆盖paint
。 - 然后你可以设置UI:
tabbedPane.setUI(new CustomTabbedUI(Color.RED));
这真的old answer我挖出来的是部分正确的。显然是不完整的。
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...
}
}
I intend to change the color of the area marked with an 'X'
我唯一看到的 "X" 是在框架的标题栏中。您不能使用 Swing 更改它。
如果您想更改绘制选项卡的背景,则需要更改添加选项卡式窗格的组件的背景。在您的示例中,您可以使用:
frame.getContentPane().setBackground(Color.RED)
frame.add(tabbedPane);