日期:2014-05-20 浏览次数:20784 次
package com; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import javax.swing.BorderFactory; import javax.swing.BoxLayout; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JPopupMenu; import javax.swing.SwingUtilities; public class TestAnswer extends JFrame{ TestAnswer(){ this.getContentPane().add(this.createMenuBar(), BorderLayout.LINE_START); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public JMenuBar createMenuBar() { JMenuBar menuBar = new JMenuBar(); menuBar.setLayout(new BoxLayout(menuBar, BoxLayout.PAGE_AXIS));//设置JMenuBar的布局管理器为纵向的 menuBar.add(createMenu("Menu 1")); menuBar.add(createMenu("Menu 2")); menuBar.add(createMenu("Menu 3")); menuBar.setBorder(BorderFactory.createMatteBorder(0,0,0,1, Color.BLACK));//设置border return menuBar; } class HorizontalMenu extends JMenu { HorizontalMenu(String label) { super(label); JPopupMenu pm = getPopupMenu(); pm.setLayout(new BoxLayout(pm, BoxLayout.LINE_AXIS));//设置纵向菜单的布局 } @Override public Dimension getMinimumSize() { return getPreferredSize(); } @Override public Dimension getMaximumSize() { return getPreferredSize(); } } public JMenu createMenu(String title) { JMenu m = new HorizontalMenu(title); return m; } public static void main(String[] args){ //异步调用,使用awt的调度线程 SwingUtilities.invokeLater(new Runnable() { @Override public void run() { TestAnswer t = new TestAnswer(); t.pack(); t.setVisible(true); } }); } }