如何通过action command和get action命令处理mousepressed事件来移动到各种JFrame
。 Java swing代码:
//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 :(得分:1)
不要在JMenuItem上使用MouseListener。
要处理菜单项的点击,您应该使用操作。
阅读How to Use Menus上Swing教程中的部分,了解更多信息和工作示例。
保留指向教程句柄的链接,以获取其他Swing基础知识的示例。