Java 在 JTabbedPane 容器中設定單個標籤文字顏色的程式
要設定單個標籤文字的顏色,請使用 setForegroundAt() 方法。這提供了提及索引和顏色的選項。這裡的索引是你想要給文字著色的特定標籤的索引。
我們先建立一個 JTabbedPane −
JTabbedPane tabbedPane = new JTabbedPane();
現在,為索引為 2 的其中一個標籤設定背景色 −
tabbedPane.setForegroundAt(2, Color.RED);
以下是將在 JTabbedPane 中更新單個標籤的前景色示例 −
示例
package my;
import javax.swing.*;
import java.awt.*;
public class SwingDemo {
public static void main(String args[]) {
JFrame frame = new JFrame("Devices");
JTabbedPane tabbedPane = new JTabbedPane();
JTextArea text = new JTextArea(100,100);
JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
panel1 = new JPanel();
panel2 = new JPanel();
panel2.add(text);
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
panel6 = new JPanel();
panel7 = new JPanel();
panel8 = new JPanel();
tabbedPane.setBackground(Color.yellow);
tabbedPane.setForeground(Color.black);
tabbedPane.addTab("Laptop", panel1);
tabbedPane.addTab("Desktop ", panel2);
tabbedPane.addTab("Notebook", panel3);
tabbedPane.addTab("Echo ", panel4);
tabbedPane.addTab("Tablet", panel5);
tabbedPane.addTab("Alexa ", panel6);
tabbedPane.addTab("Notebook", panel7);
tabbedPane.addTab("iPad", panel8);
tabbedPane.setForegroundAt(2, Color.RED);
frame.add(tabbedPane);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550,350);
frame.setVisible(true);
}
}輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP