swing之选项卡面板实例

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



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值