⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tabbedpanel.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.sshtools.ui.awt;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * <p>
 * AWT component to provide a tabbed pane similar to Swings JTabbedPane (albeit somewhat simpler!).
 * </p>
 *
 * <p>
 * Each component added except the first will be invisible until the user clicks on the tab heading either above, below. to the
 * left, or to the right of the visible component. Clicking on the heading will hide the current tab and make the new selection
 * visible.
 * </p>
 *
 * @author $Author: brett $
 * @version $Revision: 1.8 $
 */

public class TabbedPanel
    extends Container {

  /**
   * Tabs are placed above the components
   */
  public final static int TOP = 0;

  /**
   * Tabs are placed to the left of the components
   */
  public final static int LEFT = 1;

  /**
   * Tabs are placed below the components
   */
  public final static int BOTTOM = 2;

  /**
   * Tabs are placed to the right of the components
   */
  public final static int RIGHT = 3;

  //
  private final static Insets DEFAULT_INSETS = new Insets(2, 2, 2, 2);
  private final static int HORIZONTAL_GAP = 3;
  private final static int VERTICAL_GAP = 3;
  private final static int IMAGE_TEXT_GAP = 2;

  //  Private instance variables
  private int position;
  private FontMetrics metrics;
  private Hashtable tabs;
  private int sel;
  private TabbedLayout layout;
  private Vector listenerList;

  /**
   * <p>
   * Create a tabbed panel with the tabs at the specified position. Can be one of :-
   * </p>
   * <ul>
   * <li>TabbedPanel.TOP</li>
   * <li>TabbedPanel.LEFT</li>
   * <li>TabbedPanel.BOTTOM</li>
   * <li>TabbedPanel.RIGHT</li>
   * </ul>
   *
   * <p>
   * <b><font color="#ff0000">Note, only <i>TOP</i> is currently supported</font></b>
   * </p>
   *
   * @param position position
   */
  public TabbedPanel(int position) {
    super();

    tabs = new Hashtable();
    listenerList = new Vector();
    sel = -1;

    setLayout(layout = new TabbedLayout(3, 3));
    setPosition(position);

    addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent evt) {
        for (Enumeration e = tabs.elements(); e.hasMoreElements(); ) {
          TabWrapper w = (TabWrapper) e.nextElement();
          if (w.bounds != null && w.bounds.contains(evt.getX(), evt.getY())) {
            int idx = indexOfTab(w);
            if (idx != sel) {
              setSelectedTab(idx);
            }
            break;
          }
        }

      }
    });

    //        enableEvents(AWTEvent.MOUSE_EVENT_MASK);
  }

  /**
   * Add an <code>ActionListener</code> to be informed when the user selects
   * a tab.
   *
   * @param l listener to add
   */
  public void addActionListener(ActionListener l) {
    listenerList.addElement(l);
  }

  /**
   * Remove an <code>ActionListener</code> so as to no longer be informed when
   * the user selects a tab.
   *
   * @param l listener to remove
   */
  public void removeActionListener(ActionListener l) {
    listenerList.removeElement(l);
  }

  /**
   * Set the title of a tab at the given index
   *
   * @param i index of tab
   * @param title new tab title
   */
  public void setTitleAt(int i, String title) {
    Component c = getComponent(sel);
    TabWrapper t = (TabWrapper) tabs.get(c);
    t.text = title;
    repaint();
  }

  /**
   * Set the selected tab given its index.
   *
   * @param idx index of tab to select
   */
  public void setSelectedTab(int idx) {
    sel = idx;
    if (sel != -1) {
      int s = listenerList.size();
      ActionEvent aevt = null;
      for (int i = s - 1; i >= 0; i--) {
        if (aevt == null) {
          aevt = new ActionEvent(TabbedPanel.this, ActionEvent.ACTION_PERFORMED,
                                 "");
        }
        ( (ActionListener) listenerList.elementAt(i)).actionPerformed(aevt);
      }
      Component c = getComponent(sel);
      TabWrapper t = (TabWrapper) tabs.get(c);
      if(t != null) {
          layout.show(this, (String) t.name);
      }
      repaint();
    }
  }

  /**
   * Get the index of the currently selected tab
   *
   * @return selected tab index
   */
  public int getSelectedTab() {
    return sel;
  }

  /**
   * <p>
   * Create a tabbed panel with the tabs at the specified position. Can be one of :-
   * </p>
   * <ul>
   * <li>TabbedPanel.TOP</li>
   * <li>TabbedPanel.LEFT</li>
   * <li>TabbedPanel.BOTTOM</li>
   * <li>TabbedPanel.RIGHT</li>
   * </ul>
   *
   * <p>
   * <b><font color="#ff0000">Note, only <i>TOP</i> is currently supported</font></b>
   * </p>
   *
   * @param position position
   */
  public void setPosition(int position) {
    doLayout();
    repaint();
  }

  public void remove(int idx) {
    Component c = getComponent(idx);
    TabWrapper t = (TabWrapper) tabs.get(c);
    tabs.remove(c);
    if (sel == idx) {
      setSelectedTab(tabs.size() - 1);
    }
    super.remove(idx);
  }

  /**
   * Add a tab.
   *
   * @param comp component
   * @param constraints tab name
   * @param index tab index
   * @param image tab image
   */
  public void add(Component comp, Object constraints, int index, Image image) {
    TabWrapper w = new TabWrapper(comp.getName(), String.valueOf(constraints),
                                  image, comp, constraints);
    tabs.put(comp, w);
    super.addImpl(comp, comp.getName(), index);
    if (sel == -1) {
      setSelectedTab(0);
    }
    else {
      repaint();
    }
  }

  public int getPosition() {
    return position;
  }

  public void removeNotify() {
    super.removeNotify();
    if (getComponentCount() == 0) {
      sel = -1;
    }
  }

  protected void addImpl(Component comp, Object constraints, int index) {
    add(comp, constraints, index, null);
  }

  public void add(Component comp, Object constraints, Image image) {
    add(comp, constraints, -1, image);
  }

  public Insets getInsets() {
    return DEFAULT_INSETS;
  }

  public void addNotify() {
    super.addNotify();
    metrics = getFontMetrics(getFont());
  }

  public void paint(Graphics g) {
    super.paint(g);

    Dimension s = getSize();
    Rectangle r = getHeadingBounds();
    int ncomponents = getComponentCount();
    int sel = getSelectedTab();

    // Work out the colors for the border
    Color c = getBackground();
    Color midlight = c.brighter();
    Color highlight = midlight.brighter();
    Color lowlight = c.darker();
    Color shadow = lowlight.darker();
    Color darkShadow = shadow.darker();

    //
    switch (position) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -