JTabbedPane Tabbed Panel Example

Use hidden panel

 

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

// create the icon for the tab

m_tabimage = new ImageIcon("tabimage.gif");

// create three icons

m_tab1 = new ImageIcon("1.gif");

m_tab2 = new ImageIcon("2.gif");

m_tab3 = new ImageIcon("3.gif");

// ===========================

// The following is the creation process of the function button panel

// ===========================

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("Add Tab");

m_removeButton = new JButton("Remove Tab");

// add event listener

m_topButton.addActionListener(this);

m_bottomButton.addActionListener(this);

m_leftButton.addActionListener(this);

m_rightButton.addActionListener(this);

m_addButton.addActionListener(this);

m_removeButton.addActionListener(this);

// Add four function buttons to the function button panel

buttonPanel.add(m_topButton);

buttonPanel.add(m_bottomButton);

buttonPanel.add(m_leftButton);

buttonPanel.add(m_rightButton);

buttonPanel.add(m_addButton);

buttonPanel.add(m_removeButton);

 

// Add the tab pane container and function button panel to the content pane container

m_tabbedPane = new JTabbedPane(SwingConstants.TOP);

getContentPane().add("South", buttonPanel);

getContentPane().add("Center", m_tabbedPane);

 

// create three tabs

createTab();

createTab();

createTab();

// set the first tab to be displayed

m_tabbedPane.setSelectedIndex(0);

}

 

// create tab

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

// Add the newly created tab to the tab container

m_tabbedPane.addTab("Tab #" + m_tabbedPane.getTabCount(), m_tabimage,

label);

}

 

// delete tab

public void deleteTab() {

// delete the last tab

if (m_tabbedPane.getTabCount() > 0)

m_tabbedPane.removeTabAt(m_tabbedPane.getTabCount() - 1);

}

 

// handle button events

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

// redraw the tab container

m_tabbedPane.revalidate();

m_tabbedPane.repaint();

}

 

// entry method of the program

public static void main(String[] args) {

TabbedPaneDemo frame = new TabbedPaneDemo();

// Set the event listener of the frame form (close the form event)

frame.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

// show frame form

frame.pack();

frame.setVisible(true);

}

}

 

Source: http://lvdong5830.iteye.com/blog/574695

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326721019&siteId=291194637