java选项卡JTabbedPane的使用

选项卡面板由javax.swing.JTabbedPane 类实现,下面是代码的演示

package com.demo;
import java.awt.BorderLayout;
import java.net.*;

import javax.swing.*;
import javax.swing.event.*;

public class XuanXiangKa extends JFrame {
    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String args[]) {
        XuanXiangKa frame = new XuanXiangKa();
        frame.setVisible(true);
    }
    
    public XuanXiangKa() {
        super();
        getContentPane().setFocusCycleRoot(true);
        setTitle("选项卡控制面板");
        setBounds(100, 100, 400, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final JTabbedPane tabbedPane = new JTabbedPane();
        // 设置选项卡标签的布局方式
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
        tabbedPane.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                // 获得被选中选项卡的索引
                int selectedIndex = tabbedPane.getSelectedIndex();
                // 获得指定索引的选项卡标签
                String title = tabbedPane.getTitleAt(selectedIndex);
                System.out.println(title);
            }
        });
        getContentPane().add(tabbedPane, BorderLayout.CENTER);
        URL resource = XuanXiangKa.class.getResource("/tu.JPG");
        ImageIcon imageIcon = new ImageIcon(resource);
        final JLabel tabLabelA = new JLabel();
        tabLabelA.setText("选项卡A");
        // 将上面标签组件添加到选项卡中
        tabbedPane.addTab("选项卡A", imageIcon, tabLabelA, "点击查看选项卡A");
        final JLabel tabLabelB = new JLabel();
        tabLabelB.setText("选项卡B");
        tabbedPane.addTab("选项卡B", imageIcon, tabLabelB, "点击查看选项卡B");
        final JLabel tabLabelC = new JLabel();
        tabLabelC.setText("选项卡C");
        tabbedPane.addTab("选项卡C", imageIcon, tabLabelC, "点击查看选项卡C");
        tabbedPane.setSelectedIndex(2); // 设置索引为2的选项卡被选中
        tabbedPane.setEnabledAt(0, false); // 设置索引为0的选项卡不可用
    }
}
最后测试的结果:

  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值