eva*_*obm 2 java swing jtabbedpane
我想改变我的标签的背景颜色JTabbedPane
.我试着JTabbedPane.setBackgroudAt(0, Color.GRAY)
和JTabbedPane.setBackgroud(Color.GRAY)
和前景也一样,但没有任何反应.我更改了选项卡内面板的背景,仍然没有.
编辑1:我正在使用,UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
如果这可以帮助解决方案
编辑2:链接到示例,https://www.dropbox.com/s/0krn9vikvkq46mz/JavaApplication4.rar
tra*_*god 18
您可以更改使用该选项卡的背景颜色setBackgroundAt()
,如图所示这里.
您可以更改使用选项卡的内容的背景颜色setBackground()
,如图所示这里.通常,您必须在选项卡的内容上执行此操作,因为封闭的JTabbedPane
背景颜色会被内容遮盖.
如果您还有问题,请编辑您的问题包括SSCCE基础上表现出你envounter问题两个例子.
附录:结合这些方法也是可能的:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class JTabbedTest {
private static JTabbedPane jtp;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jtp = new JTabbedPane();
jtp.setPreferredSize(new Dimension(320, 200));
jtp.addTab("Reds", new ColorPanel(0, Color.RED));
jtp.setBackgroundAt(0, Color.RED);
jtp.addTab("Greens", new ColorPanel(1, Color.GREEN));
jtp.setBackgroundAt(1, Color.GREEN);
jtp.addTab("Blues", new ColorPanel(2, Color.BLUE));
jtp.setBackgroundAt(2, Color.BLUE);
f.add(jtp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
}
});
}
private static class ColorPanel extends JPanel implements ActionListener {
private final Random rnd = new Random();
private final Timer timer = new Timer(1000, this);
private Color color;
private Color original;
private int mask;
private JLabel label = new JLabel("Stackoverflow!");
private int index;
public ColorPanel(int index, Color color) {
super(true);
this.color = color;
this.original = color;
this.mask = color.getRGB();
this.index = index;
this.setBackground(color);
label.setForeground(color);
this.add(label);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
color = new Color(rnd.nextInt() & mask);
this.setBackground(color);
jtp.setBackgroundAt(index, original);
}
}
}
Run Code Online (Sandbox Code Playgroud)
大多数方法JTabbedPane
都在API中受到保护,而不能从Swing方法中访问
必须寻找Custom XxxTabbedPaneUI
,覆盖这些方法,并可从外部访问
正确的方法是仅实现Custom Look&Feel,其中一部分覆盖JTabbedPane
例如,对于自定义XxxTabbedPaneUI
归档时间: |
|
查看次数: |
20951 次 |
最近记录: |