欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Swing选项卡(JTabbedPane)自定义选项卡样式

程序员文章站 2024-01-31 23:23:16
...

Demo34_JTabbedPane.java:

package swing;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
/**
 * 选项卡面板  JTabbedPane
 */
public class Demo34_JTabbedPane extends JPanel{
	public static void main(String[] args) {
		JFrame frame=new JFrame("我的电脑 - 属性");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(new Demo34_JTabbedPane(),BorderLayout.CENTER);
        SwingUtils.setCenter(frame);//设置窗体大小600*800并居中
        frame.setVisible(true);
	}
	public Demo34_JTabbedPane() {
		super(new GridLayout(1,1));
	    JTabbedPane tabbedPane=new JTabbedPane();
	    tabbedPane.setTabPlacement(JTabbedPane.TOP);
	    tabbedPane .setUI(new Demo34_JTabbedPane_01());
	    JComponent panel1=makeTextPanel("计算机名");
	    tabbedPane.addTab("计算机名",null, panel1,"Does nothing");
	    JComponent panel2=makeTextPanel("硬件");
	    tabbedPane.addTab("硬件",null,panel2,"Does twice as much nothing");
	    JComponent panel3=makeTextPanel("高级");
	    tabbedPane.addTab("高级",null,panel3,"Still does nothing");
	    JComponent panel4=makeTextPanel("系统保护");
	    panel4.setPreferredSize(new Dimension(410,50));
	    tabbedPane.addTab("系统保护",null,panel4,"Does nothing at all");
	    add(tabbedPane);
	}
	protected JComponent makeTextPanel(String text){
	    JPanel panel=new JPanel(false);
	    JLabel filler=new JLabel(text);
	    filler.setFont(new Font("宋体", 1, 50));
	    filler.setHorizontalAlignment(JLabel.CENTER);
	    panel.setLayout(new GridLayout(1,1));
	    panel.add(filler);
	    return panel;
	}
	protected static ImageIcon createImageIcon(String path){
	    java.net.URL imgURL=Demo34_JTabbedPane.class.getResource(path);
	    if(imgURL!=null){
	        return new ImageIcon(imgURL);
	    }
	    else{
	        System.err.println("Couldn't find file: "+path);
	        return null;
	    }
	}
}

Demo34_JTabbedPane_UI.java:

package swing;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.LayoutManager;
import javax.swing.JComponent;
import javax.swing.UIManager;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicTabbedPaneUI;

public class Demo34_JTabbedPane_UI extends BasicTabbedPaneUI
{
	private Color defaultColor = new Color(192, 192, 192);

	private Color selectedColor = new Color(155, 159, 232);

	public static ComponentUI createUI(JComponent c)
	{

		return new Demo34_JTabbedPane_UI();
	}

	protected void paintTabBackground(Graphics g, int tabPlacement,
			int tabIndex, int x, int y, int w, int h, boolean isSelected)
	{
		g.setColor(!isSelected || selectedColor == null ? defaultColor
				: selectedColor);

		switch (tabPlacement)
		{
		case LEFT:
			g.fillRect(x + 1, y + 1, w - 1, h - 3);
			break;
		case RIGHT:
			g.fillRect(x, y + 1, w - 2, h - 3);
			break;
		case BOTTOM:
			g.fillRect(x + 1, y, w - 3, h - 1);
			break;
		case TOP:

		default:
			g.fillRect(x + 1, y + 1, w - 3, h - 1);

		}

	}

	protected LayoutManager createLayoutManager()
	{


		return new TabbedPaneLayout();

	}// 设置Layout

	public class TabbedPaneLayout extends BasicTabbedPaneUI.TabbedPaneLayout
	{

		protected void calculateTabRects(int tabPlacement, int tabCount)
		{
			super.calculateTabRects(tabPlacement, tabCount);

			setRec(10);
			// 设置间距
			tabInsets.bottom = 10;//设置选项卡高度
		}

		public void setRec(int rec)
		{
			for (int i = 0; i < rects.length; i++)
			{
				rects[i].x = rects[i].x + rec * i;
			}
		}
	}

	public void setDefaultColor(Color defaultColor)
	{
		this.defaultColor = defaultColor;
	}

	public void setSelectedColor(Color selectedColor)
	{
		this.selectedColor = selectedColor;
	}

	protected void paintContentBorder(Graphics g, int tabPlacement,
			int selectedIndex)
	{
		int width = tabPane.getWidth();
		int height = tabPane.getHeight();
		Insets insets = tabPane.getInsets();
		Insets tabAreaInsets = getTabAreaInsets(tabPlacement);

		int x = insets.left;
		int y = insets.top;
		int w = width - insets.right - insets.left;
		int h = height - insets.top - insets.bottom;

		switch (tabPlacement)
		{
		case LEFT:
			x += calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);

			w -= (x - insets.left);
			break;
		case RIGHT:
			w -= calculateTabAreaWidth(tabPlacement, runCount, maxTabWidth);

			w += tabAreaInsets.left;

			break;
		case BOTTOM:
			h -= calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);

			h += tabAreaInsets.top;

			break;
		case TOP:



		default:
			y += calculateTabAreaHeight(tabPlacement, runCount, maxTabHeight);

			y -= tabAreaInsets.bottom;

			h -= (y - insets.top);
		}

		// Fill region behind content area
		Color color = UIManager.getColor("TabbedPane.contentAreaColor");
		if (color != null)
		{
			g.setColor(color);
		}
		else if (selectedColor == null)
		{
			g.setColor(tabPane.getBackground());
		}
		else
		{
			g.setColor(selectedColor);
		}
		g.fillRect(x, y, w, h);
		g.setColor(selectedColor);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setStroke(new BasicStroke(10.0f));

		g.drawLine(x+6, y+5,w, y+5);
		g2d.setStroke(new BasicStroke(1.0f));

	}

}

效果如下:

Swing选项卡(JTabbedPane)自定义选项卡样式

相关标签: # Swing