您的位置:首页 > 其它

Swing 的选项卡面板

2009-04-07 13:43 141 查看
选项卡面板是一个很常用的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吧

public class TabbedPaneTest

{

public static void main(String[] args)

{

System.out.println(new File(".").getAbsolutePath());

JFrame frame = new TabbedPaneFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

}

class TabbedPaneFrame extends JFrame

{

public TabbedPaneFrame()

{

setTitle("TabbedPaneTest");

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

tabbedPane = new JTabbedPane();

ImageIcon icon = new ImageIcon("./chapter6/TabbedPaneTest/yellow-ball.gif");

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", icon, null);

add(tabbedPane, "Center");

tabbedPane.addChangeListener(new

ChangeListener()

{

public void stateChanged(ChangeEvent event)

{

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()

{

public void actionPerformed(ActionEvent event)

{

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()

{

public void actionPerformed(ActionEvent event)

{

tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);

}

});

buttonPanel.add(scrollButton);

buttonGroup.add(scrollButton);

add(buttonPanel, BorderLayout.SOUTH);

}

private void loadTab(int n)

{

String title = tabbedPane.getTitleAt(n);

ImageIcon planetIcon = new ImageIcon("./chapter6/TabbedPaneTest/"+title + ".gif");

tabbedPane.setComponentAt(n, new JLabel(planetIcon));

tabbedPane.setIconAt(n, new ImageIcon("./chapter6/TabbedPaneTest/red-ball.gif"));

}

private JTabbedPane tabbedPane;

private static final int DEFAULT_WIDTH = 400;

private static final int DEFAULT_HEIGHT = 300;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: