JTabbedPane_1

JTabbedPane_1

package com.han;

import java.awt.BorderLayout;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class JTabbedPane_1 extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4058924020221196702L;

	public JTabbedPane_1() {
		// TODO Auto-generated constructor stub
		final JTabbedPane tabbedPane = new JTabbedPane();
		tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
		
		tabbedPane.addChangeListener(new ChangeListener() {

			@Override
			public void stateChanged(ChangeEvent arg0) {
				// TODO Auto-generated method stub
				int selectedIndex = tabbedPane.getSelectedIndex();
				String title = tabbedPane.getTitleAt(selectedIndex);
				System.out.println(title);
			}
			
		});
		
		URL resource = JTabbedPane_1.class.getResource("/images/Luxun.jpg");
		BufferedImage imageScaled = null;
		try {
			String[] readerFormatNames = ImageIO.getReaderFormatNames();
			for (String name : readerFormatNames) {
				System.out.print(name + "\t");
			}
			System.out.println();
			String[] writerFormatNames = ImageIO.getWriterFormatNames();
			for (String name : writerFormatNames) {
				System.out.print(name + "\t");
			}
			System.out.println();
			BufferedImage in = ImageIO.read(resource);
			imageScaled = ImageScale.scale(in, 0.1, 0.1, 1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ImageIcon imageIcon = new ImageIcon(imageScaled);
		JLabel tabLabelA = new JLabel("选项卡A");
		JLabel tabLabelB = new JLabel("选项卡B");
		JLabel tabLabelC = new JLabel("选项卡C");
		tabbedPane.addTab("选项卡A", imageIcon, tabLabelA, "点击查看选项卡A");
		tabbedPane.addTab("选项卡B", imageIcon, tabLabelB, "点击查看选项卡B");
		tabbedPane.addTab("选项卡C", imageIcon, tabLabelC, "点击查看选项卡C");
		tabbedPane.setSelectedIndex(2); // 初始化界面时默认的是选中第一个选项卡即 index = 0
		tabbedPane.setEnabledAt(0, false);
		
		getContentPane().add(tabbedPane, BorderLayout.CENTER);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		JTabbedPane_1 frame = new JTabbedPane_1();
		frame.setTitle("选项卡面板");
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// frame.pack();
		frame.setBounds(100, 100, 500, 300);
	}

}


你可能感兴趣的:(tab)