我目前正在尝试创建一组切换按钮,这些按钮类似于Eclipse的格式化程序首选项中使用的按钮:

目前我已通过以下方式尝试此操作:
public class Exercise extends JFrame {
private String[] buttonNames = {"A", "B", "C", "D", "E"};
Exercise() {
final JPanel topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
int tabCount = 0;
final ButtonGroup topButtonGroup = new ButtonGroup();
for (String buttonName : buttonNames) {
JToggleButton tabButton = new JToggleButton(buttonName);
topButtonGroup.add(tabButton);
c.fill = GridBagConstraints.HORIZONTAL;
c.insets = new Insets(0, -6, 0, -7); // Questionable line
c.gridx = tabCount;
c.gridy = 0;
topPanel.add(tabButton, c);
tabCount++;
}
this.add(topPanel);
this.setVisible(true);
this.pack(); …Run Code Online (Sandbox Code Playgroud) 我一直在努力解决这个问题.
我试图在JTabbedPane中出现一个淡蓝色背景.我已经尝试了一切,似乎没有任何工作.
以下是我的代码.如果您运行它,它将显示选项卡,当选择浅蓝色背景和顶部的东西蓝色边框.我想控制这种颜色.但是怎么样?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.ColorUIResource;
public class Main extends JFrame {
JTabbedPane tab=new JTabbedPane();
public Main() {
setSize(300,300);
setTitle("Test Tab pane");
tab.add("First",new myPanel("First"));
tab.add("Second",new myPanel("Second"));
tab.add("Third",new myPanel("Third"));
tab.add("Fourth",new myPanel("Fourth"));
tab.addChangeListener(new ChangeTab());
getContentPane().add(tab,BorderLayout.CENTER);
setVisible(true);
for(int i=0;i<tab.getTabCount();i++){
if(i != tab.getSelectedIndex())
tab.setBackgroundAt(i,Color.orange);
tab.setForeground(Color.BLACK);
}
tab.setOpaque(true);
UIManager.put("TabbedPane.contentAreaColor ",ColorUIResource.GREEN);
UIManager.put("TabbedPane.selected",ColorUIResource.GREEN);
UIManager.put("TabbedPane.background",ColorUIResource.GREEN);
UIManager.put("TabbedPane.shadow",ColorUIResource.GREEN);
}
public static void main(String[] args) {
Main main = new Main();
}
class ChangeTab implements ChangeListener{
public void stateChanged(ChangeEvent e){
tab.validate();
System.out.println(tab.getSelectedIndex());
for(int i=0;i<tab.getTabCount();i++){ …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用JTabbedPane.我添加了两个选项卡,它们是自定义类"ContentPanel"的实例.这扩展了JPanel并设置了背景,边框等等.基本上它意味着我不必设置我想要应用此颜色方案的每个JPanel的属性.我注意到它们的边框不仅会出现,而且还有另一个边框(我认为是蓝色 - 至少在我的屏幕上),这个边框出现在边框周围,连接到标签"选择器"本身(即你点击的按钮以获得适当的观点).我想改变这个边界,因为它看起来很奇怪对金/棕色配色方案.有谁知道怎么做?我尝试过JTabbedPane.setBorder(边框b),但这不起作用.这只是围绕整个事物设置边框,包括选项卡选择器..不是我想要的.
任何有关这方面的帮助将不胜感激.