TabbedPaneDemo










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的更多相关文章
- menu JPopupMenu JTabbedPane
菜单是GUI中最常用的组件,菜单不是Component类的子类,不能放置在普通容器中,不受布局管理器的约束,只能放置在菜单栏中. 菜单组件由菜单栏 (MenuBar).菜单(Menu)和菜单项(Men ...
随机推荐
- hdu1011(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs& ...
- Android---App Widget(一)
本文译自:http://developer.android.com/guide/topics/appwidgets/index.html App Widgets是一些较小的应用程序窗口,它们能够被嵌入 ...
- Service组件 总结 + 绑定理Service三种实现方式 Messager + Binder + AIDL
在Android中进程按优先级可以分为五类,优先级从高到低排列: - 前台进程 该进程包含正在与用户进行交互的界面组件,比如一个Activity - 可视进程 该进程中的组件虽然没有和用户交互,但是仍 ...
- python手记(52)
python将信息加密进图片 从图片中解密信息 >>> runfile(r'K:\testpro\test1.py', wdir=r'K:\testpro') http://blog ...
- 怎样让你的安卓手机瞬间变Firefox os 畅玩firefox os 应用
Firefox os 手机迟迟不能在国内大面积上市.如今能买到的Firefox os手机国内就一款Firefox os ZET OPEN C ,但这款手机配置确实还不如人意.价格方面也不实惠,对于我们 ...
- VS2010,原来还有这些快捷键,果断记下来!
一直认为VS的快捷键跟eclipse比起来差远了,那些由于不知道还有如此多有效却不知的快捷键阿 1. 检查括号匹配(在左右括号间切换): Ctrl +] 2. 选中从光标起到行首(尾)间的代码: Sh ...
- Ubuntu12.10 下搭建基于KVM-QEMU的虚拟机环境(十三)
今天我们实验libvirt提供的快照功能,快照可以用于系统恢复,防止安装了某些软件或中病毒等情况导致系统损毁的情况. 一.快照类型 1) 磁盘快照 内部的:快照驻留在原来的镜像文件内部 ...
- SecureCRT学习之道:SecureCRT 常用技巧
快捷键: 1. ctrl + a : 移动光标到行首 2. ctrl + e :移动光标到行尾 3. ctrl + d :删除光标之后的一个字符 4. ctrl + w : 删除行首到当前光标所在位 ...
- SQLiteDatabase和Contentprovider
SQLiteDatabase和Contentprovider这两个数据库,我一般是用前面一个,喜欢它的操作数据库的语句,简单明了,可惜有时遇到数据库同步的问题,有时我们需要在一个数据库下建立多个表,多 ...
- PHP_保留两位小数而且四舍五入_保留两位小数而且不四舍五入
php保留两位小数而且四舍五入 $num = 123213.666666; echo sprintf("%.2f", $num); php保留两位小数而且不四舍五入 $num = ...