import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.EventListener;
public class TestMenuBar extends Frame /*implements ActionListener*/
{
MenuBar menubar=new MenuBar();
Menu fileM=new Menu("File");
Menu editM=new Menu("Edit");
Menu toolsM=new Menu("Tools");
Menu helpM=new Menu("Help");
MenuItem fileM1=new MenuItem("New");
MenuItem fileM2=new MenuItem("Open");
MenuItem fileM3=new MenuItem("Save");
Menu fileM4=new Menu("Print");
CheckboxMenuItem fileM5=new CheckboxMenuItem("Quit",true);
MenuItem printM=new MenuItem("Preview");
MenuItem setM=new MenuItem("Setting");
MenuItem editM1=new MenuItem("连接");
public TestMenuBar()
{
setM.setActionCommand("set");
editM1.setActionCommand("connect");
printM.addActionListener(new MenuBarListener());
setM.addActionListener(new MenuBarListener());
menubar.add(fileM);
menubar.add(editM);
menubar.add(toolsM);
menubar.add(helpM);
fileM.add(fileM1);
fileM.add(fileM2);
fileM.add(fileM3);
fileM.add(fileM4);
fileM.addSeparator();
fileM4.add(printM);
fileM4.add(setM);
fileM.add(fileM5);
editM.add(editM1);
//editM1.addActionListener(this);
this.setMenuBar(menubar);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
}
});
editM1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(editM1.getActionCommand().equals("connect"))
{
editM1.setActionCommand("disconnect");
editM1.setLabel("断开");
}
else if(editM1.getActionCommand().equals("disconnect"))
{
editM1.setActionCommand("connect");
editM1.setLabel("连接");
}
}
});
}
public static void main(String[] args)
{
System.out.println("Hello World!");
TestMenuBar mainFrame=new TestMenuBar();
mainFrame.setTitle("TestMenuBar");
mainFrame.setSize(400,400);
mainFrame.setVisible(true);
}
/*public void actionPerformed(ActionEvent e)
{
// TODO: Add your code here
if(e.getActionCommand().equals("connect"))
{
editM1.setActionCommand("disconnect");
editM1.setLabel("断开");
}
else if(e.getActionCommand().equals("disconnect"))
{
editM1.setActionCommand("connect");
editM1.setLabel("连接");
}
}*/
}
posted on 2007-01-20 14:40 大头剑客 阅读(355) 评论(0) 编辑 收藏 所属分类: 学习笔记