2016-09-17 112 views
-1

如何通过动作命令处理mousepressed事件并获取动作命令来移动到各种JFrame。 Java swing代码:如何按照动作命令处理mousepressed事件

//this code belongs to home jframe class 

mntmPublicSector= new JMenuItem("Public Sector"); 
    mntmPublicSector.setActionCommand("public"); 
    mntmPublicSector.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent arg0) { int select=JOptionPane.showConfirmDialog(null,"if already user login","not user Register now",JOptionPane.YES_NO_OPTION); 
      if(select==JOptionPane.YES_OPTION) 
      { 
       new Login_Page().setVisible(true); 
       dispose(); 
      } 
      else 
      { 
       new Student_Register().setVisible(true); 
       dispose(); 
      } }}); 

mntmStateSector = new JMenuItem("State Sector"); 
    mntmStateSector.setActionCommand("state"); 
    mntmStateSector.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent arg0) { int select=JOptionPane.showConfirmDialog(null,"if already user login","not user Register now",JOptionPane.YES_NO_OPTION); 
      if(select==JOptionPane.YES_OPTION) 
      { 
       new Login_Page().setVisible(true); 
       dispose(); 
      } 
      else 
      { 
       new Student_Register().setVisible(true); 
       dispose(); 
      } }}); 



mntmApptitude = new JMenuItem("Apptitude"); 
    mntmApptitude.setActionCommand("app"); 
    mntmApptitude.addMouseListener(new MouseAdapter() { 
     @Override 
     public void mousePressed(MouseEvent arg0) { int select=JOptionPane.showConfirmDialog(null,"if already user login","not user Register now",JOptionPane.YES_NO_OPTION); 
      if(select==JOptionPane.YES_OPTION) 
      { 
       new Login_Page().setVisible(true); 
       dispose(); 
      } 
      else 
      { 
       new Student_Register().setVisible(true); 
       dispose(); 
      } }}); 



/*if i click with if i login,it needs to take to respective jframe [--App_Exam_Register_Panel()--or --State_exam_reg()-- or -- Public_exam_Reg()-- ] */ 

//this code belongs to login jframe class  
JButton btnLogin = new JButton(iim); 
Home hm=new Home(); 
    btnLogin.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) {    
      String st=hm.mntmPublicSector.getActionCommand(); 
      String st1 =hm.mntmStateSector.getActionCommand(); 
      String aps=hm.mntmApptitude.getActionCommand(); 
      //String apss=hm.mntmApptitude 
      if(st.equals("public")) 
      { 
       new Public_exam_Reg().setVisible(true);//jframe class 
       dispose(); 
      }  //switch case 
      else if(st1.equals("state")) 
      { 
       new State_exam_reg().setVisible(true);//jframe class 
       dispose(); 
      } 
      else if (aps.equals("Apptitude")) 
      { 
      new App_Exam_Register_Panel().setVisible(true);//jframe class 
       dispose(); 
      }  }  }); 
+0

新增至计算器, – venkatasubramanianr

+1

请编辑您的问题以包含展示您所描述的问题和您期望的结果的[mcve]。 – trashgod

+0

1)参见[使用多个JFrames,好/坏实践?](http://stackoverflow.com/q/9554636/418556)2)使用合乎逻辑的一致形式缩进代码行和块。缩进旨在使代码的流程更易于遵循! –

回答

1

请勿在JMenuItem上使用MouseListener。

要处理菜单项的点击,您应该使用Action。

阅读关于How to Use Menus的Swing教程的部分以获取更多信息和工作示例。

保留指向其他Swing基础示例的教程句柄的链接。