package swing.tabbedpane;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JToggleButton;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; /*2015-7-12*/
public class TabbedPaneTest {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() { @Override
public void run() {
JFrame frame = new TabbedPaneFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
}
});
} } class TabbedPaneFrame extends JFrame {
private static final long serialVersionUID = -7748936498904415868L;
private static final int DEFAULT_WIDTH = 400;
private static int DEFAULT_HEIGHT = 400;
private JTabbedPane tabbedPane; public TabbedPaneFrame() {
setTitle(getClass().getSimpleName());
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT); tabbedPane = new JTabbedPane();
ImageIcon icon = new ImageIcon(getClass().getResource("/swing/tabbedpane/blue-ball.gif").getPath());
tabbedPane.addTab("Mercury", icon, null);
tabbedPane.addTab("Venus", icon, null);
tabbedPane.addTab("Earth", icon, null);
tabbedPane.addTab("Mars", icon, null);
tabbedPane.addTab("Jupiter", icon, null);
tabbedPane.addTab("Saturn", icon, null);
tabbedPane.addTab("Uranus", icon, null);
tabbedPane.addTab("Neptune", icon, null);
tabbedPane.addTab("Pluto", null, null); final int plutoIndex = tabbedPane.indexOfTab("Pluto");
JPanel plutoPanel = new JPanel();
plutoPanel.add(new JLabel("Pluto", icon, SwingConstants.LEADING));
JToggleButton plutoCheckBox = new JCheckBox();
plutoCheckBox.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
tabbedPane.remove(plutoIndex);
}
}); plutoPanel.add(plutoCheckBox);
tabbedPane.setTabComponentAt(plutoIndex, plutoPanel); // add(tabbedPane, "Center");
add(tabbedPane, BorderLayout.CENTER); tabbedPane.addChangeListener(new ChangeListener() { @Override
public void stateChanged(ChangeEvent e) {
if (tabbedPane.getSelectedComponent() == null) {
int n = tabbedPane.getSelectedIndex();
loadTab(n);
} }
}); loadTab(0);
JPanel buttonPanel = new JPanel();
ButtonGroup buttonGroup = new ButtonGroup();
JRadioButton wrapButton = new JRadioButton("Wrap tabs");
wrapButton.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
}
});
buttonPanel.add(wrapButton);
buttonGroup.add(wrapButton); wrapButton.setSelected(true);
JRadioButton scrollButton = new JRadioButton("Scroll tabs");
scrollButton.addActionListener(new ActionListener() { @Override
public void actionPerformed(ActionEvent e) {
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
}); buttonPanel.add(scrollButton);
buttonGroup.add(scrollButton);
add(buttonPanel, BorderLayout.SOUTH); } protected void loadTab(int n) {
String title = tabbedPane.getTitleAt(n);
String filePath = getClass().getResource("/swing/tabbedpane/" + title.toLowerCase() + ".png").getPath();
ImageIcon planetIcon = new ImageIcon(filePath);
tabbedPane.setComponentAt(n, new JLabel(planetIcon));
tabbedPane.setIconAt(n, new ImageIcon(getClass().getResource("/swing/tabbedpane/red-ball.gif").getPath()));
} }

TabbedPaneDemo的更多相关文章

  1. menu JPopupMenu JTabbedPane

    菜单是GUI中最常用的组件,菜单不是Component类的子类,不能放置在普通容器中,不受布局管理器的约束,只能放置在菜单栏中. 菜单组件由菜单栏 (MenuBar).菜单(Menu)和菜单项(Men ...

随机推荐

  1. hdu1828(线段树——矩形周长并)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 分析:与面积不同的地方是还要记录竖的边有几个(num记录),并且当边界重合的时候需要合并(用lb ...

  2. openfire插件开发的几点说明

    1.关于插件的目录结构 这个网上的资料很多,但是我觉得要看懂也不太容易,我这里上一个包括了jsp和servlet的图,希望大家能马上看懂: ME的Navigator视图下的截图: build path ...

  3. 提高SQL执行效率

    原文地址:http://www.cnblogs.com/hlxs/archive/2012/05/07/2487082.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ...

  4. Windows Phone开发(47):轻松调用Web Service

    原文:Windows Phone开发(47):轻松调用Web Service 众所周知(除了没用过VS的),在VS里面调用Web Service是一件很愉快的事情,不解释,相信很多朋友在以前的项目中肯 ...

  5. Android 下拉刷新上拉载入效果功能

    应用场景: 在App开发中,对于信息的获取与演示.不可能所有将其获取与演示,为了在用户使用中,给予用户以友好.方便的用户体验,以滑动.下拉的效果动态载入数据的要求就会出现. 为此.该效果功能就须要应用 ...

  6. 构建轻量级的Table View注意事项[UIKit]

    參考文章来自objcio站点 一.使用ChildViewController 将Table ViewController作为Child View Controller加入到其它View Control ...

  7. UVA 11427 - Expect the Expected(概率递归预期)

    UVA 11427 - Expect the Expected 题目链接 题意:玩一个游戏.赢的概率p,一个晚上能玩n盘,假设n盘都没赢到总赢的盘数比例大于等于p.以后都不再玩了,假设有到p就结束 思 ...

  8. php文件操作基本使用方法

    <?php /* $fp=fopen("tmp.html","r"); $str=fread($fp,filesize("tmp.html&qu ...

  9. (算法入门经典大赛 优先级队列)LA 3135(之前K说明)

    A data stream is a real-time, continuous, ordered sequence of items. Some examples include sensor da ...

  10. Sencha Architect 2 的使用

    俗话说的好, 工欲善其事必先利其器, 用Sencha开发的语言, 自己可能不太熟悉, 写出来很麻烦, 于是给大家介绍一个工具. 启动程序第一个界面: 单击第一个Go按钮, 创建一个项目.进入以后, 单 ...