隐藏面板使用
package Assis;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TabbedPaneDemo extends JFrame implements ActionListener {
private static final long serialVersionUID = 1L;
private ImageIcon m_tabimage;
private ImageIcon m_tab1;
private ImageIcon m_tab2;
private ImageIcon m_tab3;
private JTabbedPane m_tabbedPane;
private JButton m_topButton;
private JButton m_bottomButton;
private JButton m_leftButton;
private JButton m_rightButton;
private JButton m_addButton;
private JButton m_removeButton;
public TabbedPaneDemo() {
// 创建选项卡的图标
m_tabimage = new ImageIcon("tabimage.gif");
// 创建三个图标
m_tab1 = new ImageIcon("1.gif");
m_tab2 = new ImageIcon("2.gif");
m_tab3 = new ImageIcon("3.gif");
// ===========================
// 下面是功能按钮面板的创建过程
// ===========================
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 6));
m_topButton = new JButton("顶部");
m_bottomButton = new JButton("底部");
m_leftButton = new JButton("左边");
m_rightButton = new JButton("右边");
m_addButton = new JButton("添加选项卡");
m_removeButton = new JButton("删除选项卡");
// 添加事件监听器
m_topButton.addActionListener(this);
m_bottomButton.addActionListener(this);
m_leftButton.addActionListener(this);
m_rightButton.addActionListener(this);
m_addButton.addActionListener(this);
m_removeButton.addActionListener(this);
// 把四个功能按钮加入功能按钮面板中
buttonPanel.add(m_topButton);
buttonPanel.add(m_bottomButton);
buttonPanel.add(m_leftButton);
buttonPanel.add(m_rightButton);
buttonPanel.add(m_addButton);
buttonPanel.add(m_removeButton);
// 把选项卡窗格容器和功能按钮面板加入到内容窗格容器中
m_tabbedPane = new JTabbedPane(SwingConstants.TOP);
getContentPane().add("South", buttonPanel);
getContentPane().add("Center", m_tabbedPane);
// 创建三个选项卡
createTab();
createTab();
createTab();
// 设置显示第一个选项卡
m_tabbedPane.setSelectedIndex(0);
}
// 创建选项卡
public void createTab() {
JLabel label = null;
switch (m_tabbedPane.getTabCount() % 3) {
case 0:
label = new JLabel("Tab #" + m_tabbedPane.getTabCount(), m_tab1,
SwingConstants.CENTER);
break;
case 1:
label = new JLabel("Tab #" + m_tabbedPane.getTabCount(), m_tab2,
SwingConstants.CENTER);
break;
case 2:
label = new JLabel("Tab #" + m_tabbedPane.getTabCount(), m_tab3,
SwingConstants.CENTER);
break;
}
label.setVerticalTextPosition(SwingConstants.BOTTOM);
label.setHorizontalTextPosition(SwingConstants.CENTER);
label.setOpaque(true);
label.setBackground(Color.white);
// 把新创建的选项卡添加到选项卡容器中
m_tabbedPane.addTab("Tab #" + m_tabbedPane.getTabCount(), m_tabimage,
label);
}
// 删除选项卡
public void deleteTab() {
// 删除最后一个选项卡
if (m_tabbedPane.getTabCount() > 0)
m_tabbedPane.removeTabAt(m_tabbedPane.getTabCount() - 1);
}
// 处理按钮事件
public void actionPerformed(ActionEvent e) {
if (e.getSource() == m_topButton)
m_tabbedPane.setTabPlacement(SwingConstants.TOP);
else if (e.getSource() == m_bottomButton)
m_tabbedPane.setTabPlacement(SwingConstants.BOTTOM);
else if (e.getSource() == m_leftButton)
m_tabbedPane.setTabPlacement(SwingConstants.LEFT);
else if (e.getSource() == m_rightButton)
m_tabbedPane.setTabPlacement(SwingConstants.RIGHT);
else if (e.getSource() == m_addButton)
createTab();
else if (e.getSource() == m_removeButton)
deleteTab();
// 重新绘制选项卡容器
m_tabbedPane.revalidate();
m_tabbedPane.repaint();
}
// 程序的入口方法
public static void main(String[] args) {
TabbedPaneDemo frame = new TabbedPaneDemo();
// 设置框架窗体的事件监听(关闭窗体事件)
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// 显示框架窗体
frame.pack();
frame.setVisible(true);
}
}
出处 : http://lvdong5830.iteye.com/blog/574695
相关推荐
有注释 下载后复制到eclipse中直接运行
通过java swing 自定义JTabbedPane组件外观 演示自定义外观组件的方法
一个通过自定义JPanel来实现关闭按钮的JTabbedPane,因为使用了适配器去继承父类的方法,所以代码比较多但功能一般
关于jtabbedPane实现多程序集合
一个简单的标签页实例
Java中JTabbedPane(选项卡)的用法实例,对于喜欢界面设计的朋友来说是个不错的例子!~
带关闭按钮可定制的JTabbedPane,首先解决了java自带的UI丑的不行的问题,然后附带了可关闭按钮,这个可关闭按钮,可以在添加标签时,自定义是否出现.该资源属于网络资源,谢谢作者的无私奉献.象征性的收1点积分,因为我的...
java swing jTabbedPane 自定义的U,包括(宽、高、点击放大、文本字体、背景颜色、选项卡间距等) 简单易懂带注释可直接运行
Java中的JTabbedPane无自带关闭按钮,Netbeans的jar包中实现了自带关闭功能,比自己编写控件方便多了。
简单的swing tab用法.直接放在ide里就可以看到效果
JTabbedPane是Swing库提供的一个选项卡面板组件。它可以在图形界面中创建多个选项卡,并在用户点击不同选项卡时显示相应的内容。
JTabbedPanel的美化标签很详细,找了很久
本技巧提供JTabbedPane增强版本的代码,该代码允许开发人员将工具栏按钮添加到选项卡集的开头或结尾。
主要介绍了java Swing实现选项卡功能(JTabbedPane)实例代码的相关资料,学习java 基础的朋友可以参考下这个简单示例,需要的朋友可以参考下
具体要求如下:使用JTabbedPane,建立三个选项卡“成绩输入”、“成绩查询”、“成绩排序” ① 成绩输入:从界面上输入学生的学号和成绩,点击“确认”按钮进行保存。 ② 成绩查询:输入学生的学号,点击“查询”...
import javax.swing.JTabbedPane; public class MainFrame extends JFrame { /** * */ private static final long serialVersionUID = 1L; private JTabbedPane tabPane = null;// 选项卡面板 /** * 构造...
动态删减选项卡的效果示例! 值得下载看看!资源免费,大家分享!! 更多免费资源 http://ynsky.download.csdn.net/
标签化窗格:JTabbedPane 拆分窗格:JSplitPane 滚动窗格:JScrollPane 工具栏:JToolBar 桌面窗格:JDesktopPane 内部框架:JInternalFrame 分层窗格:JLayeredPane 标签:JLabel 按钮:JButton 开启/关闭按钮:...
如何使用面板组件 使用JPanel 使用JScrollPane 使用JSplitPane 使用JTabbedPane 使用JIntemalFrame 使用JLayeredPane 使用JRootPane