1
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;
}
2
package Swing高级组件.选项卡面板;
import java.awt.EventQueue;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
@SuppressWarnings("serial")
public class TabbledPaneFrame extends JFrame {
/**
* @param args
*/
TabbledPaneFrame(){
setTitle("创建选项卡面板");
setSize( 400, 300 );
final JTabbedPane tabbledPane = new JTabbedPane();
final JButton button_1 = new JButton("按钮一号");
final JButton button_2 = new JButton("按钮二号");
final JButton button_3 = new JButton("按钮三号");
final JButton button_4 = new JButton("按钮四号");
tabbledPane.addTab("选项卡一", button_1);
tabbledPane.addTab("选项卡二", button_2);
tabbledPane.addTab("选项卡三", button_3);
//准备在选项卡四上面加图片
Icon icon = new ImageIcon("mm.jpg");
tabbledPane.addTab("选项卡四", icon, button_4);
getContentPane().add( tabbledPane );
}
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater( new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
TabbledPaneFrame frame = new TabbledPaneFrame();
frame.setVisible( true );
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
});
}
}