在 Java 中将所有 JTabbedPane 标签标题显示在控制台上
要显示 JTabbedPane 中所有标签的标题,我们首先要获取标签的数量 −
int count = tabbedPane.getTabCount();
现在,循环遍历 JTabbedPane 中的标签数。使用 getTitleAt() 获取每个标签的标题 −
for (int i = 0; i < count; i++) { String str = tabbedPane.getTitleAt(i); System.out.println(str); }
下面是一个在控制台中显示 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.blue); tabbedPane.setForeground(Color.white); 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); int count = tabbedPane.getTabCount(); for (int i = 0; i < count; i++) { String str = tabbedPane.getTitleAt(i); System.out.println(str); } frame.add(tabbedPane); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600,350); frame.setVisible(true); } }
运行后,控制台将显示所有标签的值 −
输出
广告