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. UVA12304 2D Geometry 110 in 1! 计算几何

    计算几何: 堆几何模版就能够了. . .. Description Problem E 2D Geometry 110 in 1! This is a collection of 110 (in bi ...

  2. 命令行參数选项处理:getopt()及getopt_long()函数使用

         在执行某个程序的时候,我们通常使用命令行參数来进行配置其行为.命令行选项和參数控制 UNIX 程序,告知它们怎样动作. 当 gcc的程序启动代码调用我们的入口函数 main(int argc ...

  3. Android多线程文件下载器

    本应用实现的是输入文件的网络的地址,点击button開始下载,下载过程中有进度条和后面的文本提示进度, 下载过程中button不可点击,防止反复的下载,完成下载后会进行Toast的提示显示, 而且回复 ...

  4. properties editor for eclipse安装使用

    properties editor 是 Eclipse下编辑properties文件的插件,用来写国际化程序非常方便,自动保存为ASCII码,支持Unicode. 安装过程: 打开eclispe编辑器 ...

  5. vim netrw

    我们现在试一下vim文件功能,当你使用vim尝试打开目录时,vim会自动调用netrw.vim插件打开该目录(从操作系统的视角来看,目录其实是一种特殊的文件).例如,我们在vim中执行命令”:e -/ ...

  6. POJ 2299 Ultra-QuickSort (求序列的逆序对数)

    题意:废话了一大堆就是要你去求一个序列冒泡排序所需的交换的次数. 思路:实际上是要你去求一个序列的逆序队数 看案例: 9 1 0 5 4 9后面比它小的的数有4个 1后面有1个 0后面没有 5后面1个 ...

  7. POJ 1182 :食物链(并查集)

    食物链 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43526   Accepted: 12679 Description ...

  8. [Ext.Net]GridPanel之Access数据库分页显示

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  9. 点击 下载 pdf

     <iframe id="fileDownFrame" src="" style="display:none; visibility:hidde ...

  10. ASP.NET回车提交事务

    浅析ASP.NET回车提交事件[转] ASP.NET回车提交事件其实说到底并不是ASP.NET 的编程问题,却是关于html form 中的submit 按钮就是如何规划的具体讨论. 也可归于ASP. ...