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 ...
随机推荐
- Oracle单表的复杂查询
Oracle单表的复杂查询 select avg(sal),max(sal),deptnofrom empgroupby deptno; orderby deptno; 查询工资高于500或者是岗位为 ...
- ACM-简单的主题Ignatius and the Princess II——hdu1027
转载请注明出处:http://blog.csdn.net/lttree Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Othe ...
- 《JavaScript设计模式与开发实践》读书笔记之模板方法模式
1. 模板方法模式 1.1 面向对象方式实现模板方法模式 以泡茶和泡咖啡为例,可以整理为下面四步 把水煮沸 用沸水冲泡饮料 把饮料倒进杯子 加调料 首先创建一个抽象父类来表示泡一杯饮料 var Bev ...
- 经常使用的正則表達式归纳—JavaScript正則表達式
来源:http://www.ido321.com/856.html 1.正则优先级 首先看一下正則表達式的优先级,下表从最高优先级到最低优先级列出各种正則表達式操作符的优先权顺序: 2.经常使用的正則 ...
- 哈希表之bkdrhash算法解析及扩展
BKDRHASH是一种字符哈希算法,像BKDRHash,APHash.DJBHash,JSHash,RSHash.SDBMHash.PJWHash.ELFHash等等,这些都是比較经典的,通过http ...
- springmvc + excel代
1.xml简介 <!-- excel use start --> <bean class="org.springframework.web.servlet.view.Bea ...
- Three.js 3D打印数据模型文件(.STL)载入中
3DPrint是现在和未来10年度科技产品的主流之中.广泛的. 对于电子商务类3D打印网站.一个主要功能就是商品3D呈现的方式,那是,3D数据可视化技术. HTML5(WebGL)它可以用于构建3D查 ...
- Windows IOT
Windows IOT 开发入门(准备工作) 终于抽出空来了,将最近研究的东西记录下来,物联网,万物皆可联网.然后可以做到智能家居,智能生活,智能城市....一大堆.吹牛的就不说了. 在实际应用中 ...
- Cocos2dx 3.0开发环境的搭建--Eclipse建立在Android工程
一.前言: 这部分描述了Cocos2d-x 3.0的一些基础内容,以及在Eclipse上上编译我们的Cocos2d-x项目,成功把Helloworld执行起来了.看完本篇博客之后.你就会知道Cocos ...
- IntelliJ IDEA 问题总结之中的一个 —— jar包、assets、maven、git
因为工作须要,这几天開始弃用eclipse,换idea.用了几天,idea确实有些地方比較方便.可是麻烦也是不少.并且网上相应的资料并没有eclipse那么多,非常多都是自己琢磨解决的,所以想弄个帖子 ...