Swing 的选项卡面板

来源:互联网 发布:qq三国js怎么搭配灵魄 编辑:程序博客网 时间:2024/04/29 00:53

选项卡面板是一个很常用的Swing组件,在window下,右击我的电脑,查看属性,就是一个典型的选修卡面板。当然还有最经典的谷歌浏览器,也是选项卡的一个典型。Swing中的选项卡使用JTabbedPane类来实现,下面就来介绍JTabbedPane的使用:

   1.构建一个JTabbedPane对象
       JTabbedPane tab = new JTabbedPane();
   2.向其中添加选项卡,一个选项卡就是一个Compnent组件,常用一个JPane面板把需要的组件组织起来, 其实Swing的思想也是这样的,他把组件分为两类一个是普通的组件一个是可以存放组件的组件被称为容器,最外面的frame通过布局方式把容器组织起来,各个容器又把自己的组件通过布局方式组织起来,所以Swing的使用只要把容器、组件、布局方式搞定就可以了,呵呵,这里扯多了,下面是添加的方法:
       tab.addTab(String title,Component compnent);
       tab.addTab(String title,Icon icon,Component compnent);
       tab.addTab(String title,Icon icon,Component compnent,String tooltip);
    title就是选项卡的标题,compnent当然就是选项卡的内容了,icon是图标tooltip是工具提示。addTab方法是按照顺序添加到选项卡集的最后,我们知道选项卡面板实际是一个选项卡的集合,每个选项卡从0开始计数,也就是说第一个选项卡的编号为0.于是我们可以把一个选项卡添加到选项卡集的任何一个位置上,
       tab.addTab(String title,Icon icon,Component compnent,String tooltip,int index);
       当然也可以根据编号删除一个选项卡,
       tab.removeTabAt(int index);    
    那么这么多选项卡,一次只能显示一个,如何显示指定的选项卡呢?
       tab.setSelectedIndex(int index);
    如果选项卡太多,可以选择他们的显示方式,隐藏或者滚动
       tab.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
       tab.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    当你选中一个选项卡时怎么相应事件更新界面呢?要给面板添加一个ChangeListener,它只有一个方法叫stateChanged。
    下面看一个demo吧

  1. public class TabbedPaneTest
  2. {  
  3.    public static void main(String[] args)
  4.    {  
  5.        System.out.println(new File(".").getAbsolutePath());
  6.       JFrame frame = new TabbedPaneFrame();
  7.       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  8.       frame.setVisible(true);
  9.    }
  10. }
  11. class TabbedPaneFrame extends JFrame
  12. {  
  13.    public TabbedPaneFrame()
  14.    {  
  15.       setTitle("TabbedPaneTest");
  16.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
  17.       tabbedPane = new JTabbedPane();
  18.       
  19.       ImageIcon icon = new ImageIcon("./chapter6/TabbedPaneTest/yellow-ball.gif");
  20.       tabbedPane.addTab("Mercury", icon, null);
  21.       tabbedPane.addTab("Venus", icon, null);
  22.       tabbedPane.addTab("Earth", icon, null);
  23.       tabbedPane.addTab("Mars", icon, null);
  24.       tabbedPane.addTab("Jupiter", icon, null);
  25.       tabbedPane.addTab("Saturn", icon, null);
  26.       tabbedPane.addTab("Uranus", icon, null);
  27.       tabbedPane.addTab("Neptune", icon, null);
  28.       tabbedPane.addTab("Pluto", icon, null);
  29.       add(tabbedPane, "Center");
  30.       tabbedPane.addChangeListener(new
  31.          ChangeListener()
  32.          {
  33.             public void stateChanged(ChangeEvent event)
  34.             {  
  35.                if (tabbedPane.getSelectedComponent() == null)
  36.                {  
  37.                   int n = tabbedPane.getSelectedIndex();
  38.                   loadTab(n);
  39.                }
  40.             }
  41.          });
  42.       loadTab(0);
  43.       JPanel buttonPanel = new JPanel();
  44.       ButtonGroup buttonGroup = new ButtonGroup();
  45.       JRadioButton wrapButton = new JRadioButton("Wrap tabs");
  46.       wrapButton.addActionListener(new
  47.          ActionListener()
  48.          {
  49.             public void actionPerformed(ActionEvent event)
  50.             {
  51.                tabbedPane.setTabLayoutPolicy(JTabbedPane.WRAP_TAB_LAYOUT);
  52.             }
  53.          });
  54.       buttonPanel.add(wrapButton);
  55.       buttonGroup.add(wrapButton);
  56.       wrapButton.setSelected(true);
  57.       JRadioButton scrollButton = new JRadioButton("Scroll tabs");
  58.       scrollButton.addActionListener(new
  59.          ActionListener()
  60.          {
  61.             public void actionPerformed(ActionEvent event)
  62.             {
  63.                tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
  64.             }
  65.          });
  66.       buttonPanel.add(scrollButton);
  67.       buttonGroup.add(scrollButton);
  68.       add(buttonPanel, BorderLayout.SOUTH);
  69.    }
  70.    private void loadTab(int n)
  71.    {
  72.       String title = tabbedPane.getTitleAt(n);
  73.       ImageIcon planetIcon = new ImageIcon("./chapter6/TabbedPaneTest/"+title + ".gif");
  74.       tabbedPane.setComponentAt(n, new JLabel(planetIcon));
  75.       tabbedPane.setIconAt(n, new ImageIcon("./chapter6/TabbedPaneTest/red-ball.gif"));
  76.    }
  77.    private JTabbedPane tabbedPane;
  78.    private static final int DEFAULT_WIDTH = 400;
  79.    private static final int DEFAULT_HEIGHT = 300;
  80. }
原创粉丝点击