Java JTabbedPanel选项卡面板

简介

JTabbedPane选项卡面板实现了一个多卡片的用户界面,通过它可以将一个复杂的对话框分割成若干个选项卡,实现对信息的分类显示和管理,使界面更简洁大方,还可以有效地减少窗体的个数。

构造方法

JTabbedPane():创建一个默认的选项卡面板,默认情况下标签在选项卡的上方,布局方式为限制布局

JTabbedPane(int tabPlacement):创建一个指定标签显示位置的选项卡面板,入口参数tabPlacement:为选项卡标题的位置, 值为TOP(选项卡上方,默认值)、BOTTOM(选项卡下方)、LEFT(选项卡左侧)、RIGHT(选项卡右侧)

JTabbedPane(int tabPlacement, int tabLayoutPolicy):创建一个既指定标签显示位置,又指定选项卡布局方式的选项卡面板,入口参数tabPlacement为选项卡标题的显示位置,入口参数tabLayoutPolicy: 选项卡位置不能放入所有的选项卡时,放置选项卡的策略,值为WRAP_TAB_LAYOU(限制布局,默认值)、SCROLL_TAB_LAYOUT(滚动布局)

常用方法

1、addTab(String title, Component component):添加一个标签为title的选项卡

2、addTab(String title, Icon icon, Component component):添加一个标签为title、图标为icon的选项卡

3、addTab(String title, Icon icon, Component component, String tip):添加一个标签为title、图标为icon、提示为tip的选项卡

4、InsertTab(String title, Icon icon, Component component, String tip, int index):在索引位置index处插入一个标题为title、图标为icon、提示为tip的选项卡。索引值从0开始

5、setTabPlacement(int tabPlacement):设置选项卡标签的显示位置

6、setTabLayoutPolicy(int tabLayoutPolicy):设置选项卡标签的布局方式

7、setSelectedIndex(int index):设置指定索引位置的选项卡被选中

8、setEnabledAt(int index, boolean enabled):设置 index 位置的选项卡是否可用

9、getSelectedComponent():获取当前选中的选项卡对应的内容组件

10、setComponentAt(int index, Component component):将 index 位置的内容组件设置为 component

11、setTabComponentAt(int index, Component titleComponent):自定义选项卡标题位置的组件, 这里的 titleComponent 将放到选项卡标题位置

12、removeTabAt(int index)或remove(int index):移除指定位置的选项

13、remove(Component component):移除指定内容控件的选项卡

14、removeAll():移除所有选项卡

状态监听器

void addChangeListener(ChangeListener l):添加选项卡选中状态改变的监听器

import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.net.*;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.tree.*;

public class t3 extends JFrame{
	
	private URL resource = t3.class.getResource("tab.JPG");//映射根目录下的icon图标地址链接
	//private ImageIcon imageIcon = new ImageIcon("F:\\Test\\Java\\testJava8\\bin\\tab.jpg");//或是直接将icon图标的地址链接上
	private ImageIcon imageIcon = new ImageIcon(resource);//图标
	
	private JTabbedPane tabbedPane = new JTabbedPane();
	
	public t3() {
		setTitle("选项卡面板");
		setBounds(400, 400, 400, 400);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);//设置选项卡标签的布局方式为滚动布局
		tabbedPane.addChangeListener(new ChangeListener() {//添加时间监听器
			
			@Override
			public void stateChanged(ChangeEvent e) {
				// TODO Auto-generated method stub
				
				int selectedIndex = tabbedPane.getSelectedIndex();//获得被选中选项卡的索引
				String title = tabbedPane.getTitleAt(selectedIndex);//获得指定索引的选项卡标签
				System.out.println(title);
			}
		});
		
		tabbedPane.addTab("选项卡A", imageIcon, new JLabel("选项卡A"), "点击查看选项卡A");//将标签组件添加到选项卡中,并且要求有提示
		tabbedPane.addTab("选项卡B", imageIcon, new JLabel("选项卡B"), "点击查看选项卡B");
		tabbedPane.addTab("选项卡C", imageIcon, new JLabel("选项卡C"), "点击查看选项卡C");
		
		tabbedPane.setSelectedIndex(2); //设置索引为2的选项卡被选中
		tabbedPane.setEnabledAt(0, false); //设置索引为0的选项卡不可用(也就是选项卡A不可用)
		
		add(tabbedPane);
		setVisible(true);
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		t3 test = new t3();

	}

}

猜你喜欢

转载自blog.csdn.net/qq_36761831/article/details/81460476