谢谢斑竹对上一题的回答.还有一道题:窗体出不来了!
这个程序调试错误. 不知道上怎么回事.
错误提示:Exception in thread "main" java.lang.IllegalArgumentException: horizontalTextPosition
at javax.swing.AbstractButton.checkHorizontalKey(AbstractButton.java:908)
at javax.swing.AbstractButton.setHorizontalTextPosition(AbstractButton.java:833)
at MenuDemo.createToolbar(MenuDemo.java:59)
at MenuDemo.<init>(MenuDemo.java:32)
at MenuDemo.main(MenuDemo.java:192)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
public class MenuDemo extends JFrame implements ActionListener{
JMenuBar menuBar = new JMenuBar();
JMenu menu1,menu2,menu3,menu4,menu5;
JMenuItem menuItem11,menuItem12,menuItem13,menuItem14;
JCheckBoxMenuItem menuItem21,menuItem22;
JToolBar tb = new JToolBar();
JButton b1,b2,b3;
MenuDemo(){
getContentPane().setLayout(new BorderLayout());
createMenu();
createToolbar();
setTitle("工具栏演示");
setSize(400,400);
setVisible(true);
}
void createToolbar(){
b1 =new JButton("用户管理",new ImageIcon("Open.gif"));
b1.setHorizontalTextPosition(AbstractButton.CENTER);
b1.setVerticalTextPosition(AbstractButton.BOTTOM);
b1.setToolTipText("用户管理");
b1.setFocusPainted(false);
b1.setRequestFocusEnabled(false);
b2 = new JButton("密码设置",new ImageIcon("Open.gif"));
b2.setHorizontalTextPosition(AbstractButton.CENTER);
b2.setHorizontalTextPosition(AbstractButton.BOTTOM);
b2.setToolTipText("密码设置");
b2.setRequestFocusEnabled(false);
b3 = new JButton("数据维护",new ImageIcon("Open.gif"));
b3.setHorizontalTextPosition(AbstractButton.CENTER);
b3.setVerticalTextPosition(AbstractButton.BOTTOM);
b3.setToolTipText("数据维护");
b3.setRequestFocusEnabled(false);
tb.add(b1);tb.add(b2);tb.addSeparator();tb.add(b3);tb.setRollover(true);
getContentPane().add(tb,"North");
tb.setFloatable(true);
}
void createMenu(){
menu1 = new JMenu("系统管理(S)");
menu1.setMnemonic('S');
menu2 = new JMenu("视图(V)");
menu2.setMnemonic('V');
menuItem11 = new JMenuItem("用户管理(U)",new ImageIcon("Open.gif"));
menuItem11.setMnemonic('U');
menuItem11.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_U,java.awt.event.InputEvent.CTRL_MASK));
menuItem12 = new JMenuItem("密码设置(P)");
menuItem12.setIcon(new ImageIcon("Open.gif"));
menuItem12.setMnemonic('P');
menuItem12.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P,java.awt.event.InputEvent.CTRL_MASK));
menuItem13 = new JMenuItem("退出");
menu1.add(menuItem11);
menu1.add(menuItem12);
menu1.addSeparator();
menu1.add(menuItem13);
menuItem21 = new JCheckBoxMenuItem("显示工具栏",true);
menuItem22 = new JCheckBoxMenuItem("显示提示文字",true);
menu2.add(menuItem21);
menu2.add(menuItem22);
menuBar.add(menu1);
menuBar.add(menu2);
setJMenuBar(menuBar);
setIconImage(new ImageIcon("Open.gif").getImage());
menuItem11.addActionListener(this);
menuItem12.addActionListener(this);
menuItem13.addActionListener(this);
menuItem21.addActionListener(this);
menuItem22.addActionListener(this);
}
public void actionPerformed(ActionEvent e ){
if(e.getSource()==menuItem11)
;//此处调用用户管理程序
else if(e.getSource()==menuItem12)
;//此处调用密码设置程序
else if(e.getSource()==menuItem13){
dispose();
System.exit(0);
}
else if(e.getSource()==menuItem21){
if(menuItem21.getState())
tb.setVisible(true);
else
tb.setVisible(false);
this.invalidate();
}
}
public static void main(String args[]){
Font font = new Font("Frame",Font.PLAIN,14);
Enumeration keys = UIManager.getLookAndFeelDefaults().keys();
while(keys.hasMoreElements()){
Object key = keys.nextElement();
if(UIManager.get(key) instanceof Font) UIManager.put(key, font);
}
new MenuDemo();
}
}