Swing的带图标JTabbedPane示例

说明

TabbedPane类允许有选项卡式屏幕。

代码示例

package com.yiidian;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

public class SwingTester {
   public static void main(String[] args) {
      createWindow();
   }

   private static void createWindow() {    
      JFrame frame = new JFrame("一点教程网:Swing Tester");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      createUI(frame);
      frame.setSize(560, 200);      
      frame.setLocationRelativeTo(null);  
      frame.setVisible(true);
   }

   private static void createUI(final JFrame frame){
      JTabbedPane tabbedPane = new JTabbedPane();
      ImageIcon arrowIcon = null;
      java.net.URL imgURL = SwingTester.class.getResource("arrow.jpg");
      if (imgURL != null) {
         arrowIcon = new ImageIcon(imgURL);
      } else {
         JOptionPane.showMessageDialog(frame, "Icon image not found.");
      }
      JPanel panel1 = new JPanel(false);
      JLabel filler = new JLabel("Tab 1");
      filler.setHorizontalAlignment(JLabel.CENTER);
      panel1.setLayout(new GridLayout(1, 1));
      panel1.add(filler);
      tabbedPane.addTab("Tab 1", arrowIcon, panel1,"Tab 1 tooltip");
      tabbedPane.setMnemonicAt(0, KeyEvent.VK_1);
      JPanel panel2 = new JPanel(false);
      JLabel filler2 = new JLabel("Tab 2");
      filler2.setHorizontalAlignment(JLabel.CENTER);
      panel2.setLayout(new GridLayout(1, 1));
      panel2.add(filler2);
      tabbedPane.addTab("Tab 2", null, panel2,"Tab 2 tooltip");
      tabbedPane.setMnemonicAt(0, KeyEvent.VK_2);     
      frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);    
   }
}

执行效果如下:

热门文章

优秀文章