我有一个带有 JTextArea 的 JTabbedPane。方法创建另一个选项卡并将其添加到 JTabbedPane。现在我想实现,新创建的选项卡会获得另一种颜色,直到第一次打开它,就像在聊天室中显示有来自特定用户的新消息。 我真的不知道如何实现这一点。我尝试使用 while 循环,但它不起作用
String name = "...";
JTabbedPane tabs = new JTabbedPane();
JTextArea textarea = new JTextArea();
textarea.setEditable(false);
textarea.setLineWrap(true);
JScrollPane jScrollPane = new JScrollPane(textarea);
jScrollPane.setPreferredSize(new Dimension(300, 300));
tabs.add(name, jScrollPane);
tabs.setBackgroundAt(tabs.indexOfTab(name),Color.GREEN);
while(true){
if(tabs.getSelectedIndex() == tabs.indexOfTab(name)){
tabs.setBackgroundAt(tabs.indexOfTab(name),Color.GRAY);
break;
}
}
最佳答案
从这里开始example ,以下更改会产生如下所示的效果。每个选项卡均以 Color.lightGray
开头,并且 ChangeListener
在每个选项卡首次打开时将背景颜色更改为 Color.red.darker()
已选择。您可以在应用程序数据模型的监听器中执行类似的操作;研究了几种方法here .
public TabColors() {
for (int i = 0; i < MAX; i++) {
Color color = Color.lightGray;
pane.add("Tab " + String.valueOf(i), new TabContent(i, color));
pane.setBackgroundAt(i, color);
}
pane.setSelectedIndex(-1);
pane.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
pane.setBackgroundAt(pane.getSelectedIndex(), Color.red.darker());
}
});
this.add(pane);
}
关于java - JTabbedPane:第一次打开之前具有不同颜色的新选项卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38297001/