利用了一点点自由布局可以很方便的自由添加多个菜单项,代码如下:
import java.awt.Color; import java.awt.Dimension; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import java.awt.GridLayout; import java.awt.Toolkit; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.GroupLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.LineBorder; public class SimpleMenulist extends JFrame implements MouseListener { private static final long serialVersionUID = -6050868817749702480L; /** * @param args */ private JButton menulistButton1; private JLabel menulistButton1_1; private JLabel menulistButton1_2; private JLabel menulistButton1_3; private JButton menulistButton2; private JLabel menulistButton2_1; private JLabel menulistButton2_2; private JLabel menulistButton2_3; private JButton menulistButton3; private JLabel menulistButton3_1; private JLabel menulistButton3_2; private JLabel menulistButton3_3; private JButton menulistButton4; private JLabel menulistButton4_1; private JLabel menulistButton4_2; private JLabel menulistButton4_3; private JButton menulistButton5; private JLabel menulistButton5_1; private JLabel menulistButton5_2; private JLabel menulistButton5_3; private JPanel mainPanel; private JPanel buttonPanel1; private JPanel buttonPanel2; private JPanel buttonPanel3; private JPanel buttonPanel4; private JPanel buttonPanel5; // 基本思想:4(按钮+标签+GridLayout) * 4(面板+GridBagLayout) * 1(面板+自由布局) public SimpleMenulist(String title) { super(title); setDefaultCloseOperation(EXIT_ON_CLOSE); mainPanel = new JPanel(); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING).addComponent(mainPanel)); layout.setVerticalGroup(layout.createParallelGroup( GroupLayout.Alignment.LEADING).addGroup( layout.createSequentialGroup().addComponent(mainPanel) .addContainerGap(500, Short.MAX_VALUE))); add(mainPanel); mainPanel.setLayout(new GridBagLayout()); buttonPanel1 = new JPanel(); buttonPanel2 = new JPanel(); buttonPanel3 = new JPanel(); buttonPanel4 = new JPanel(); buttonPanel5 = new JPanel(); GridBagConstraints GBC = new GridBagConstraints(); GBC.gridx = 0; GBC.gridy = 0; GBC.fill = GridBagConstraints.BOTH; mainPanel.add(buttonPanel1, GBC); GBC.gridx = 0; GBC.gridy = 1; mainPanel.add(buttonPanel2, GBC); GBC.gridx = 0; GBC.gridy = 2; mainPanel.add(buttonPanel3, GBC); GBC.gridx = 0; GBC.gridy = 3; mainPanel.add(buttonPanel4, GBC); GBC.gridx = 0; GBC.gridy = 4; mainPanel.add(buttonPanel5, GBC); buttonPanel1.setLayout(new GridLayout(1, 1)); buttonPanel2.setLayout(new GridLayout(1, 1)); buttonPanel3.setLayout(new GridLayout(1, 1)); buttonPanel4.setLayout(new GridLayout(1, 1)); buttonPanel5.setLayout(new GridLayout(1, 1)); menulistButton1 = new JButton("Ben Zeph Menulist1_0"); menulistButton1_1 = new JLabel("menulist1_1"); menulistButton1_2 = new JLabel("menulist1_2"); menulistButton1_3 = new JLabel("menulist1_3"); menulistButton2 = new JButton("Ben Zeph Menulist2_0"); menulistButton2_1 = new JLabel("menulist2_1"); menulistButton2_2 = new JLabel("menulist2_2"); menulistButton2_3 = new JLabel("menulist2_3"); menulistButton3 = new JButton("Ben Zeph Menulist3_0"); menulistButton3_1 = new JLabel("menulist3_1"); menulistButton3_2 = new JLabel("menulist3_2"); menulistButton3_3 = new JLabel("menulist3_3"); menulistButton4 = new JButton("Ben Zeph Menulist4_0"); menulistButton4_1 = new JLabel("menulist4_1"); menulistButton4_2 = new JLabel("menulist4_2"); menulistButton4_3 = new JLabel("menulist4_3"); menulistButton5 = new JButton("Ben Zeph Menulist5_0"); menulistButton5_1 = new JLabel("menulist5_1"); menulistButton5_2 = new JLabel("menulist5_2"); menulistButton5_3 = new JLabel("menulist5_3"); buttonPanel1.add(menulistButton1); buttonPanel2.add(menulistButton2); buttonPanel3.add(menulistButton3); buttonPanel4.add(menulistButton4); buttonPanel5.add(menulistButton5); menulistButton1_1.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton1_2.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton1_3.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton2_1.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton2_2.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton2_3.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton3_1.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton3_2.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton3_3.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton4_1.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton4_2.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton4_3.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton5_1.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton5_2.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton5_3.setHorizontalAlignment((int) CENTER_ALIGNMENT); menulistButton1.addMouseListener(this); menulistButton2.addMouseListener(this); menulistButton3.addMouseListener(this); menulistButton4.addMouseListener(this); menulistButton5.addMouseListener(this); menulistButton1_1.addMouseListener(this); menulistButton1_2.addMouseListener(this); menulistButton1_3.addMouseListener(this); menulistButton2_1.addMouseListener(this); menulistButton2_2.addMouseListener(this); menulistButton2_3.addMouseListener(this); menulistButton3_1.addMouseListener(this); menulistButton3_2.addMouseListener(this); menulistButton3_3.addMouseListener(this); menulistButton4_1.addMouseListener(this); menulistButton4_2.addMouseListener(this); menulistButton4_3.addMouseListener(this); menulistButton5_1.addMouseListener(this); menulistButton5_2.addMouseListener(this); menulistButton5_3.addMouseListener(this); // 设置整体框架的位置 Dimension frameSize = getPreferredSize(); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setLocation(screenSize.width / 2 - frameSize.width / 2, screenSize.height / 2 - frameSize.height / 2); pack(); } public void repaintUpdateUI() { buttonPanel1.repaint(); buttonPanel1.updateUI(); buttonPanel2.repaint(); buttonPanel2.updateUI(); buttonPanel3.repaint(); buttonPanel3.updateUI(); buttonPanel4.repaint(); buttonPanel4.updateUI(); buttonPanel5.repaint(); buttonPanel5.updateUI(); } public static void main(String[] args) { // TODO Auto-generated method stub java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new SimpleMenulist("menulistList - Ben Zeph").setVisible(true); } }); } @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub if (menulistButton1.equals(e.getSource())) { if (!menulistButton1_1.isValid()) { buttonPanel1.removeAll(); buttonPanel1.setLayout(new GridLayout(4, 1)); buttonPanel1.add(menulistButton1); buttonPanel1.add(menulistButton1_1); buttonPanel1.add(menulistButton1_2); buttonPanel1.add(menulistButton1_3); } else if (menulistButton1_1.isValid()) { buttonPanel1.removeAll(); buttonPanel1.setLayout(new GridLayout(1, 1)); buttonPanel1.add(menulistButton1); } repaintUpdateUI(); } else if (menulistButton2.equals(e.getSource())) { if (!menulistButton2_1.isValid()) { buttonPanel2.removeAll(); buttonPanel2.setLayout(new GridLayout(4, 1)); buttonPanel2.add(menulistButton2); buttonPanel2.add(menulistButton2_1); buttonPanel2.add(menulistButton2_2); buttonPanel2.add(menulistButton2_3); } else if (menulistButton2_1.isValid()) { buttonPanel2.removeAll(); buttonPanel2.setLayout(new GridLayout(1, 1)); buttonPanel2.add(menulistButton2); } repaintUpdateUI(); } else if (menulistButton3.equals(e.getSource())) { if (!menulistButton3_1.isValid()) { buttonPanel3.removeAll(); buttonPanel3.setLayout(new GridLayout(4, 1)); buttonPanel3.add(menulistButton3); buttonPanel3.add(menulistButton3_1); buttonPanel3.add(menulistButton3_2); buttonPanel3.add(menulistButton3_3); } else if (menulistButton3_1.isValid()) { buttonPanel3.removeAll(); buttonPanel3.setLayout(new GridLayout(1, 1)); buttonPanel3.add(menulistButton3); } repaintUpdateUI(); } else if (menulistButton4.equals(e.getSource())) { if (!menulistButton4_1.isValid()) { buttonPanel4.removeAll(); buttonPanel4.setLayout(new GridLayout(4, 1)); buttonPanel4.add(menulistButton4); buttonPanel4.add(menulistButton4_1); buttonPanel4.add(menulistButton4_2); buttonPanel4.add(menulistButton4_3); } else if (menulistButton4_1.isValid()) { buttonPanel4.removeAll(); buttonPanel4.setLayout(new GridLayout(1, 1)); buttonPanel4.add(menulistButton4); } repaintUpdateUI(); } else if (menulistButton5.equals(e.getSource())) { if (!menulistButton5_1.isValid()) { buttonPanel5.removeAll(); buttonPanel5.setLayout(new GridLayout(4, 1)); buttonPanel5.add(menulistButton5); buttonPanel5.add(menulistButton5_1); buttonPanel5.add(menulistButton5_2); buttonPanel5.add(menulistButton5_3); } else if (menulistButton5_1.isValid()) { buttonPanel5.removeAll(); buttonPanel5.setLayout(new GridLayout(1, 1)); buttonPanel5.add(menulistButton5); } repaintUpdateUI(); } } @Override public void mouseEntered(MouseEvent e) { // TODO Auto-generated method stub if (menulistButton1_1.equals(e.getSource())) { menulistButton1_1.setBorder(new LineBorder(Color.BLACK)); menulistButton1_1.setBackground(Color.green); } else if (menulistButton1_2.equals(e.getSource())) { menulistButton1_2.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton1_3.equals(e.getSource())) { menulistButton1_3.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton2_1.equals(e.getSource())) { menulistButton2_1.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton2_2.equals(e.getSource())) { menulistButton2_2.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton2_3.equals(e.getSource())) { menulistButton2_3.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton3_1.equals(e.getSource())) { menulistButton3_1.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton3_2.equals(e.getSource())) { menulistButton3_2.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton3_3.equals(e.getSource())) { menulistButton3_3.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton4_1.equals(e.getSource())) { menulistButton4_1.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton4_2.equals(e.getSource())) { menulistButton4_2.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton4_3.equals(e.getSource())) { menulistButton4_3.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton5_1.equals(e.getSource())) { menulistButton5_1.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton5_2.equals(e.getSource())) { menulistButton5_2.setBorder(new LineBorder(Color.BLACK)); } else if (menulistButton5_3.equals(e.getSource())) { menulistButton5_3.setBorder(new LineBorder(Color.BLACK)); } } @Override public void mouseExited(MouseEvent e) { // TODO Auto-generated method stub if (menulistButton1_1.equals(e.getSource())) { menulistButton1_1.setBorder(null); } else if (menulistButton1_2.equals(e.getSource())) { menulistButton1_2.setBorder(null); } else if (menulistButton1_3.equals(e.getSource())) { menulistButton1_3.setBorder(null); } else if (menulistButton2_1.equals(e.getSource())) { menulistButton2_1.setBorder(null); } else if (menulistButton2_2.equals(e.getSource())) { menulistButton2_2.setBorder(null); } else if (menulistButton2_3.equals(e.getSource())) { menulistButton2_3.setBorder(null); } else if (menulistButton3_1.equals(e.getSource())) { menulistButton3_1.setBorder(null); } else if (menulistButton3_2.equals(e.getSource())) { menulistButton3_2.setBorder(null); } else if (menulistButton3_3.equals(e.getSource())) { menulistButton3_3.setBorder(null); } else if (menulistButton4_1.equals(e.getSource())) { menulistButton4_1.setBorder(null); } else if (menulistButton4_2.equals(e.getSource())) { menulistButton4_2.setBorder(null); } else if (menulistButton4_3.equals(e.getSource())) { menulistButton4_3.setBorder(null); } else if (menulistButton5_1.equals(e.getSource())) { menulistButton5_1.setBorder(null); } else if (menulistButton5_2.equals(e.getSource())) { menulistButton5_2.setBorder(null); } else if (menulistButton5_3.equals(e.getSource())) { menulistButton5_3.setBorder(null); } } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub if (menulistButton1_1.equals(e.getSource())) { menulistButton1_1.setForeground(Color.RED); } else if (menulistButton1_2.equals(e.getSource())) { menulistButton1_2.setForeground(Color.RED); } else if (menulistButton1_3.equals(e.getSource())) { menulistButton1_3.setForeground(Color.RED); } else if (menulistButton2_1.equals(e.getSource())) { menulistButton2_1.setForeground(Color.RED); } else if (menulistButton2_2.equals(e.getSource())) { menulistButton2_2.setForeground(Color.RED); } else if (menulistButton2_3.equals(e.getSource())) { menulistButton2_3.setForeground(Color.RED); } else if (menulistButton3_1.equals(e.getSource())) { menulistButton3_1.setForeground(Color.RED); } else if (menulistButton3_2.equals(e.getSource())) { menulistButton3_2.setForeground(Color.RED); } else if (menulistButton3_3.equals(e.getSource())) { menulistButton3_3.setForeground(Color.RED); } else if (menulistButton4_1.equals(e.getSource())) { menulistButton4_1.setForeground(Color.RED); } else if (menulistButton4_2.equals(e.getSource())) { menulistButton4_2.setForeground(Color.RED); } else if (menulistButton4_3.equals(e.getSource())) { menulistButton4_3.setForeground(Color.RED); } else if (menulistButton5_1.equals(e.getSource())) { menulistButton5_1.setForeground(Color.RED); } else if (menulistButton5_2.equals(e.getSource())) { menulistButton5_2.setForeground(Color.RED); } else if (menulistButton5_3.equals(e.getSource())) { menulistButton5_3.setForeground(Color.RED); } } @Override public void mouseReleased(MouseEvent e) { // TODO Auto-generated method stub if (menulistButton1_1.equals(e.getSource())) { menulistButton1_1.setForeground(Color.DARK_GRAY); } else if (menulistButton1_2.equals(e.getSource())) { menulistButton1_2.setForeground(Color.DARK_GRAY); } else if (menulistButton1_3.equals(e.getSource())) { menulistButton1_3.setForeground(Color.DARK_GRAY); } else if (menulistButton2_1.equals(e.getSource())) { menulistButton2_1.setForeground(Color.DARK_GRAY); } else if (menulistButton2_2.equals(e.getSource())) { menulistButton2_2.setForeground(Color.DARK_GRAY); } else if (menulistButton2_3.equals(e.getSource())) { menulistButton2_3.setForeground(Color.DARK_GRAY); } else if (menulistButton3_1.equals(e.getSource())) { menulistButton3_1.setForeground(Color.DARK_GRAY); } else if (menulistButton3_2.equals(e.getSource())) { menulistButton3_2.setForeground(Color.DARK_GRAY); } else if (menulistButton3_3.equals(e.getSource())) { menulistButton3_3.setForeground(Color.DARK_GRAY); } else if (menulistButton4_1.equals(e.getSource())) { menulistButton4_1.setForeground(Color.DARK_GRAY); } else if (menulistButton4_2.equals(e.getSource())) { menulistButton4_2.setForeground(Color.DARK_GRAY); } else if (menulistButton4_3.equals(e.getSource())) { menulistButton4_3.setForeground(Color.DARK_GRAY); } else if (menulistButton5_1.equals(e.getSource())) { menulistButton5_1.setForeground(Color.DARK_GRAY); } else if (menulistButton5_2.equals(e.getSource())) { menulistButton5_2.setForeground(Color.DARK_GRAY); } else if (menulistButton5_3.equals(e.getSource())) { menulistButton5_3.setForeground(Color.DARK_GRAY); } } }