增加的菜單欄效果圖如下: eclipse 中調整到 swt的design視圖下 控制項區域選擇Menu Controls 將Menu Bar拖動到視窗標題欄 將Cascaded Menu拖動到Menu Bar 依次將多個Menu Item加入到New SubMenu Separator是分隔符 也可以 ...
增加的菜單欄效果圖如下:
eclipse 中調整到 swt的design視圖下
控制項區域選擇Menu Controls
將Menu Bar拖動到視窗標題欄
將Cascaded Menu拖動到Menu Bar
依次將多個Menu Item加入到New SubMenu
Separator是分隔符
也可以再放入Cascaded Menu級聯菜單
好友列表視窗的代碼如下:
package com.swift.frame; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.FlowLayout; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JDialog; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.JPopupMenu; import javax.swing.JTabbedPane; import javax.swing.SwingConstants; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class FriendsDialog extends JDialog { public static void main(String args[]) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); } catch (ClassNotFoundException e1) { e1.printStackTrace(); } catch (InstantiationException e1) { e1.printStackTrace(); } catch (IllegalAccessException e1) { e1.printStackTrace(); } catch (UnsupportedLookAndFeelException e1) { e1.printStackTrace(); } EventQueue.invokeLater(new Runnable() { public void run() { try { FriendsDialog dialog = new FriendsDialog(); dialog.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); dialog.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public FriendsDialog() { super(); setTitle("飛燕聊天"); setBounds(100, 100, 247, 581); final JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); getContentPane().add(panel, BorderLayout.NORTH); final JLabel label = new JLabel(new ImageIcon("Images/logo.jpg")); label.setText("New JLabel"); panel.add(label, BorderLayout.WEST); label.setPreferredSize(new Dimension(74,74)); final JPanel panel_1 = new JPanel(); panel_1.setLayout(new BorderLayout()); panel.add(panel_1, BorderLayout.CENTER); final JLabel advancingSwiftLabel = new JLabel(); advancingSwiftLabel.setText("Advancing Swift"); panel_1.add(advancingSwiftLabel, BorderLayout.CENTER); final JLabel neverWasterLabel = new JLabel(); neverWasterLabel.setText("Never waste time any more"); panel_1.add(neverWasterLabel, BorderLayout.SOUTH); final JPanel panel_2 = new JPanel(); panel_2.setLayout(new BorderLayout()); getContentPane().add(panel_2, BorderLayout.SOUTH); final JPanel panel_3 = new JPanel(); final FlowLayout flowLayout = new FlowLayout(); flowLayout.setAlignment(FlowLayout.LEFT); panel_3.setLayout(flowLayout); panel_2.add(panel_3); final JButton button = new JButton(); panel_3.add(button); button.setHorizontalTextPosition(SwingConstants.LEFT); button.setHorizontalAlignment(SwingConstants.LEFT); button.setText("設置"); final JButton button_1 = new JButton(); panel_3.add(button_1); button_1.setText("查找"); final JPanel panel_4 = new JPanel(); panel_2.add(panel_4, BorderLayout.EAST); final JButton button_2 = new JButton(); panel_4.add(button_2); button_2.setText("退出"); final JTabbedPane tabbedPane = new JTabbedPane(); getContentPane().add(tabbedPane, BorderLayout.CENTER); final JPanel panel_5 = new JPanel(); tabbedPane.addTab("好友列表", null, panel_5, null); final JPanel panel_6 = new JPanel(); tabbedPane.addTab("群聊", null, panel_6, null); final JPanel panel_7 = new JPanel(); tabbedPane.addTab("聊天記錄", null, panel_7, null); final JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar); final JMenu menu = new JMenu(); menu.setText("操作"); menuBar.add(menu); final JMenuItem newItemMenuItem = new JMenuItem(); newItemMenuItem.setText("設置"); menu.add(newItemMenuItem); final JMenuItem newItemMenuItem_1 = new JMenuItem(); newItemMenuItem_1.setText("空間"); menu.add(newItemMenuItem_1); final JMenuItem newItemMenuItem_2 = new JMenuItem(); newItemMenuItem_2.setText("郵箱"); menu.add(newItemMenuItem_2); final JMenu menu_1 = new JMenu(); menu_1.setText("會員"); menu.add(menu_1); final JMenuItem newItemMenuItem_3 = new JMenuItem(); newItemMenuItem_3.setText("會員官網"); menu_1.add(newItemMenuItem_3); final JMenuItem newItemMenuItem_4 = new JMenuItem(); newItemMenuItem_4.setText("會員專區"); menu_1.add(newItemMenuItem_4); menu.addSeparator(); final JMenu menu_2 = new JMenu(); menu_2.setText("安全"); menu.add(menu_2); final JMenuItem newItemMenuItem_5 = new JMenuItem(); newItemMenuItem_5.setText("緊急凍結"); menu_2.add(newItemMenuItem_5); final JMenuItem newItemMenuItem_6 = new JMenuItem(); newItemMenuItem_6.setText("密碼保護"); menu_2.add(newItemMenuItem_6); final JMenuItem newItemMenuItem_7 = new JMenuItem(); newItemMenuItem_7.setText("退出"); menu.add(newItemMenuItem_7); final FlowLayout flowLayout_1 = new FlowLayout(); flowLayout_1.setAlignment(FlowLayout.RIGHT); } /** * WindowBuilder generated method.<br> * Please don't remove this method or its invocations.<br> * It used by WindowBuilder to associate the {@link javax.swing.JPopupMenu} with parent. */ private static void addPopup(Component component, final JPopupMenu popup) { component.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { if (e.isPopupTrigger()) showMenu(e); } public void mouseReleased(MouseEvent e) { if (e.isPopupTrigger()) showMenu(e); } private void showMenu(MouseEvent e) { popup.show(e.getComponent(), e.getX(), e.getY()); } }); } }