package UserFrame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
public class MainFrame extends JFrame
implements ActionListener{
public MainFrame(){
super("学生成绩管理系统");
setSize(500, 400);
desktop = new JDesktopPane();
setContentPane(desktop);
setJMenuBar(createBar());
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
JButton button = new JButton("test");
button.setActionCommand("basicInput");
button.addActionListener(this);
add(button,BorderLayout.CENTER);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
protected JMenuBar createBar(){
JMenuBar menuBar = new JMenuBar();
JMenu basicInput = new JMenu("基本数据录入");
basicInput.setActionCommand("basicInput");
basicInput.addActionListener(this);
JMenu scoreInput = new JMenu("成绩录入");
scoreInput.setActionCommand("scoreInput");
JMenu scoreSearch = new JMenu("成绩查询");
scoreSearch.setActionCommand("scoreSearch");
JMenu scoreAnalysis = new JMenu("成绩分析");
scoreAnalysis.setActionCommand("scoreAnalysis");
JMenu help = new JMenu("帮助");
help.setActionCommand("help");
JMenu quit = new JMenu("退出");
quit.setActionCommand("quit");
menuBar.add(basicInput);
menuBar.add(scoreInput);
menuBar.add(scoreSearch);
menuBar.add(scoreAnalysis);
menuBar.add(help);
menuBar.add(quit);
return menuBar;
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.out.println(e.getActionCommand());
if("basicInput".equals(e.getActionCommand())){
System.out.println(1);
BasicInputFrame bif = new BasicInputFrame();
addFrame(bif);
System.out.println(2);
}else if("scoreInput".equals(e.getActionCommand())){
}else if("scoreSearch".equals(e.getActionCommand())){
}else if("scoreAnalysis".equals(e.getActionCommand())){
}else if("help".equals(e.getActionCommand())){
}else{//quit
this.quit();
}
}
public void addFrame(JInternalFrame frame){
frame.setVisible(true);
desktop.add(frame);
try{
frame.setSelected(true);
}catch(java.beans.PropertyVetoException e){}
}
public void quit(){
System.exit(0);
}
private JDesktopPane desktop;
public static void main(String[] args){
MainFrame frame = new MainFrame();
frame.setVisible(true);
}
}
****************************************************
目前只对“基本数据录入”JMenu有实际动作,就是产生一个子窗口
但是一直单击后买反应,因为监听器里面的第一句print语句都没执行,看看有啥问题?好久没用过java了
代码可以忽略掉了……
我把JMenuBar上的JMenu全改成JMenuItem就可以触发了,为什么JMenu就不能触发事件呢?
JMenu 能触发事件吧?我的怎么触发不了单击事件?!
这个其实是可以的。你给JMenu注册MenuListener(MenuEvent e)监视器,其中有三个方法 void menuCanceled(MenuEvent e)取消菜单时调用。void menuDeselected(MenuEvent e)取消选择某个菜单时调用。void menuSelected(MenuEvent e)选择某个菜单时调用。具体实现方法即可。其实SWT中有现成的选项卡(TabFolder)...