集册 Java实例教程 获取JTabbedPane选项卡区域边界

获取JTabbedPane选项卡区域边界

欢马劈雪     最近更新时间:2020-01-02 10:19:05

544
获取JTabbedPane选项卡区域边界


//package com.nowjava;

import javax.swing.*;//时 代 J a v a 公 众 号 - nowjava.com

import java.awt.*;


public class Main {

    static Rectangle getTabAreaBounds(JTabbedPane tabbedPane) {

        Rectangle tabbedRect = tabbedPane.getBounds();

        //pointed out by daryl. NullPointerException: i.e. addTab("Tab",null)

        //Rectangle compRect   = getSelectedComponent().getBounds();

        Component comp = tabbedPane.getSelectedComponent();

        int idx = 0;

        while (comp == null && idx < tabbedPane.getTabCount()) {

            comp = tabbedPane.getComponentAt(idx++);

        }/**来 自 N o w J a v a . c o m - 时  代  Java**/

        Rectangle compRect = (comp == null) ? new Rectangle() : comp

                .getBounds();

        int tabPlacement = tabbedPane.getTabPlacement();

        if (tabPlacement == SwingConstants.TOP) {

            tabbedRect.height = tabbedRect.height - compRect.height;

        } else if (tabPlacement == SwingConstants.BOTTOM) {

            tabbedRect.y = tabbedRect.y + compRect.y + compRect.height;

            tabbedRect.height = tabbedRect.height - compRect.height;

        } else if (tabPlacement == SwingConstants.LEFT) {

            tabbedRect.width = tabbedRect.width - compRect.width;

        } 
展开阅读全文