1. package swing.tabbedpane;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6.  
  7. import javax.swing.ButtonGroup;
  8. import javax.swing.ImageIcon;
  9. import javax.swing.JCheckBox;
  10. import javax.swing.JFrame;
  11. import javax.swing.JLabel;
  12. import javax.swing.JPanel;
  13. import javax.swing.JRadioButton;
  14. import javax.swing.JTabbedPane;
  15. import javax.swing.JToggleButton;
  16. import javax.swing.SwingConstants;
  17. import javax.swing.SwingUtilities;
  18. import javax.swing.event.ChangeEvent;
  19. import javax.swing.event.ChangeListener;
  20.  
  21. /*2015-7-12*/
  22. public class TabbedPaneTest {
  23. public static void main(String[] args) {
  24. SwingUtilities.invokeLater(new Runnable() {
  25.  
  26. @Override
  27. public void run() {
  28. JFrame frame = new TabbedPaneFrame();
  29. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  30. frame.setVisible(true);
  31. frame.setLocationRelativeTo(null);
  32. }
  33. });
  34. }
  35.  
  36. }
  37.  
  38. class TabbedPaneFrame extends JFrame {
  39. private static final long serialVersionUID = -7748936498904415868L;
  40. private static final int DEFAULT_WIDTH = 400;
  41. private static int DEFAULT_HEIGHT = 400;
  42. private JTabbedPane tabbedPane;
  43.  
  44. public TabbedPaneFrame() {
  45. setTitle(getClass().getSimpleName());
  46. setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  47.  
  48. tabbedPane = new JTabbedPane();
  49. ImageIcon icon = new ImageIcon(getClass().getResource("/swing/tabbedpane/blue-ball.gif").getPath());
  50. tabbedPane.addTab("Mercury", icon, null);
  51. tabbedPane.addTab("Venus", icon, null);
  52. tabbedPane.addTab("Earth", icon, null);
  53. tabbedPane.addTab("Mars", icon, null);
  54. tabbedPane.addTab("Jupiter", icon, null);
  55. tabbedPane.addTab("Saturn", icon, null);
  56. tabbedPane.addTab("Uranus", icon, null);
  57. tabbedPane.addTab("Neptune", icon, null);
  58. tabbedPane.addTab("Pluto", null, null);
  59.  
  60. final int plutoIndex = tabbedPane.indexOfTab("Pluto");
  61. JPanel plutoPanel = new JPanel();
  62. plutoPanel.add(new JLabel("Pluto", icon, SwingConstants.LEADING));
  63. JToggleButton plutoCheckBox = new JCheckBox();
  64. plutoCheckBox.addActionListener(new ActionListener() {
  65.  
  66. @Override
  67. public void actionPerformed(ActionEvent e) {
  68. tabbedPane.remove(plutoIndex);
  69. }
  70. });
  71.  
  72. plutoPanel.add(plutoCheckBox);
  73. tabbedPane.setTabComponentAt(plutoIndex, plutoPanel);
  74.  
  75. // add(tabbedPane, "Center");
  76. add(tabbedPane, BorderLayout.CENTER);
  77.  
  78. tabbedPane.addChangeListener(new ChangeListener() {
  79.  
  80. @Override
  81. public void stateChanged(ChangeEvent e) {
  82. if (tabbedPane.getSelectedComponent() == null) {
  83. int n = tabbedPane.getSelectedIndex();
  84. loadTab(n);
  85. }
  86.  
  87. }
  88. });
  89.  
  90. loadTab(0);
  91. JPanel buttonPanel = new JPanel();
  92. ButtonGroup buttonGroup = new ButtonGroup();
  93. JRadioButton wrapButton = new JRadioButton("Wrap tabs");
  94. wrapButton.addActionListener(new ActionListener() {
  95.  
  96. @Override
  97. public void actionPerformed(ActionEvent e) {
  98. tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
  99. }
  100. });
  101. buttonPanel.add(wrapButton);
  102. buttonGroup.add(wrapButton);
  103.  
  104. wrapButton.setSelected(true);
  105. JRadioButton scrollButton = new JRadioButton("Scroll tabs");
  106. scrollButton.addActionListener(new ActionListener() {
  107.  
  108. @Override
  109. public void actionPerformed(ActionEvent e) {
  110. tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  111. }
  112. });
  113.  
  114. buttonPanel.add(scrollButton);
  115. buttonGroup.add(scrollButton);
  116. add(buttonPanel, BorderLayout.SOUTH);
  117.  
  118. }
  119.  
  120. protected void loadTab(int n) {
  121. String title = tabbedPane.getTitleAt(n);
  122. String filePath = getClass().getResource("/swing/tabbedpane/" + title.toLowerCase() + ".png").getPath();
  123. ImageIcon planetIcon = new ImageIcon(filePath);
  124. tabbedPane.setComponentAt(n, new JLabel(planetIcon));
  125. tabbedPane.setIconAt(n, new ImageIcon(getClass().getResource("/swing/tabbedpane/red-ball.gif").getPath()));
  126. }
  127.  
  128. }

TabbedPaneDemo的更多相关文章

  1. menu JPopupMenu JTabbedPane

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

随机推荐

  1. 将svnkit转成dlls时的问题

    未处理 System.TypeInitializationException Message="“org.tmatesoft.svn.core.internal.wc.DefaultSVNO ...

  2. SQLServer 复制和数据库镜像 具体配置部署

    SQLserver 能够把镜像和复制同一时候部署,结合了两方的高可用性.能够使数据库得到更好的高可用性和容灾的保证. 关于镜像:f=255&MSPPError=-2147217396" ...

  3. C++习题 虚函数-计算图形面积

    C++习题 虚函数-计算图形面积 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 86 [cid=1143&pid=6 ...

  4. HttpApplication处理对象与HttpModule处理模块

    HttpApplication处理对象与HttpModule处理模块 (第三篇) 一.HttpApplication对象简述 在HttpRuntime创建了HttpContext对象之后,HttpRu ...

  5. Tomcat6 Session建立机制简要

    底:  测试部门做压力测试, 结果没多久新闻,出现OutOfMemory. 查找原因,通过监视工具,查找StandardSession(org.apache.catalina.session.Stan ...

  6. JAVA insert() 插入字符串 reverse() 颠倒 delete()和deleteCharAt() 删除字符 replace() 替换 substring() 截取子串

    insert() 插入字符串 StringBuffer insert(int index,String str) StringBuffer insert(int index,char ch) Stri ...

  7. 主要的核心思想是取cookie然后发查询请求,不需要浏览器做代理(转)

    需求是催生项目和推进项目的不竭动力. 背景: 最近,因为媳妇要做个B超检查,想着去大医院查查应该更放心,所以就把目标瞄准在A医院.早已耳闻A院一号难求万人空巷,所以把所有能接触到的机会都看了一遍,线下 ...

  8. 简单fcgi程序

    1.头文件 #include <fcgi_stdio.h> 2.while(FCGI_Accept()>=0)//这里进入循环,前台每请求一次fcgi服务,就循环一次 循环内处理: ...

  9. WPF之Binding深入探讨--Darren

    1,Data Binding在WPF中的地位 程序的本质是数据+算法.数据会在存储.逻辑和界面三层之间流通,所以站在数据的角度上来看,这三层都很重要.但算法在3层中的分布是不均匀的,对于一个3层结构的 ...

  10. 【HTML+CSS】(1)基本语法

    HTML基金会 <em>他强调标签,<strong>加粗标签 <q>短文本引用.<blockquote>长文本引用,这两个标签会让文字带双引號.   空 ...