package com.niit.javagui; import java.awt.Color; import java.awt.MenuBar; import java.awt.event.InputEvent; import java.awt.event.KeyEvent; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JTabbedPane; import javax.swing.KeyStroke; /** * @author: Annie * @date:2016年5月30日 * @description: */ public class JTabbedPaneDemo extends JFrame{ public static void main(String[] args) { new JTabbedPaneDemo(); } JTabbedPane tabbedPane; JPanel jp1,jp2,jp3; JMenuBar menuBar; JMenu filemenu,editMenu,formatMenu,helpmenu,checkMenu; JMenuItem itemNew,itemOpen,itemSave,itemSaveAs,itemSet,itemPrint,itemExit, itemAutomatic,itemFont,itemaStatusBar,itemaCheckHelp,itemAbout; public JTabbedPaneDemo(){ setVisible(true); setSize(400,400); setTitle("选项卡面板例子"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); instantiationObject(); keyWord(); addFunction(); jp1.setBackground(Color.CYAN); jp2.setBackground(Color.RED); jp3.setBackground(Color.BLUE); } /** * 实例化对象方法 * */ public void instantiationObject(){ jp1 = new JPanel(); jp2 = new JPanel(); jp3 = new JPanel(); tabbedPane = new JTabbedPane(JTabbedPane.BOTTOM); menuBar = new JMenuBar(); filemenu = new JMenu("文件(F)"); editMenu = new JMenu("编辑(E)"); formatMenu = new JMenu("格式(o)"); checkMenu = new JMenu("查看(V)"); helpmenu = new JMenu("帮助(H)"); itemNew = new JMenuItem("新建文件"); itemOpen = new JMenuItem("打开"); itemSave = new JMenuItem("保存"); itemSaveAs = new JMenuItem("另存为"); itemSet = new JMenuItem("页面设置"); itemPrint = new JMenuItem("打印"); itemExit = new JMenuItem("退出"); itemAutomatic = new JMenuItem("自动转行"); itemFont = new JMenuItem("字体"); itemaStatusBar = new JMenuItem("状态栏"); itemaCheckHelp = new JMenuItem("查看帮助"); itemAbout = new JMenuItem("关于记事本"); } /** * 添加组件方法 * */ public void addFunction(){ setJMenuBar(menuBar); menuBar.add(filemenu); menuBar.add(editMenu); menuBar.add(formatMenu); menuBar.add(checkMenu); menuBar.add(helpmenu); filemenu.add(itemNew); filemenu.add(itemOpen); filemenu.add(itemSave); filemenu.add(itemSaveAs); filemenu.addSeparator(); filemenu.add(itemSet); filemenu.add(itemPrint); filemenu.add(itemExit); formatMenu.add(itemAutomatic); formatMenu.add(itemFont); checkMenu.add(itemaStatusBar); helpmenu.add(itemaCheckHelp); helpmenu.add(itemAbout); tabbedPane.add("联系人", jp1); tabbedPane.add("发现", jp2); tabbedPane.add("我", jp3); add(tabbedPane); } /** * 添加关键字的方法 * */ public void keyWord(){ itemNew.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,InputEvent.CTRL_MASK)); itemOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK)); itemSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK)); itemSaveAs.setAccelerator(KeyStroke.getKeyStroke('A')); itemSet.setAccelerator(KeyStroke.getKeyStroke('U')); itemPrint.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,InputEvent.CTRL_MASK)); itemExit.setAccelerator(KeyStroke.getKeyStroke('X')); itemAutomatic.setAccelerator(KeyStroke.getKeyStroke('W')); itemFont.setAccelerator(KeyStroke.getKeyStroke('F')); itemaStatusBar.setAccelerator(KeyStroke.getKeyStroke('S')); itemaCheckHelp.setAccelerator(KeyStroke.getKeyStroke('H')); itemAbout.setAccelerator(KeyStroke.getKeyStroke('A')); } }
效果图: