嵌套if(e.getActionCommand()。equals(“”)

时间:2013-10-21 12:11:13

标签: java swing actionlistener

有人可以告诉我是否可以嵌套

if(e.getActionCommand().equals("some String"){//do it}

例如。 。 。

public void actionPerformed(ActionEvent e)
{
  theFrame.hide();

  if(e.getActionCommand().equals("Button in theFrame"))
  {
      newFramerz.show();

      if (e.getActionCommand().equals("Button in newFramerz"))
      {
       //do usual stuff
      }
  }
}

出于某种原因,当我尝试编译它时,我的代码无效。然后我怀疑我在最顶层写的代码行。

有人可以告诉我是否有可能吗?解释也很棒。

修改

以下是我的问题的示例代码。

public void ATM_MainMenu()
{

    //-----------------------------//
    MainMenu = new JFrame("Main Menu");

    JPanel TextPanel     = new JPanel();
    JPanel BTPanel       = new JPanel();
    JPanel FormPanel     = new JPanel();

    JLabel TextLabel     = new JLabel("Choose Transaction");

    JButton InquireBalBT = new JButton("Inquire Balance");
    InquireBalBT.addActionListener(this);
    JButton DepositBT    = new JButton("Deposit");
    DepositBT.addActionListener(this);
    JButton WithdrawBT   = new JButton("Withdraw");
    WithdrawBT.addActionListener(this);

    TextPanel.setBackground(Color.white);
    TextPanel.add(TextLabel);
    BTPanel.add(TextPanel);
    BTPanel.add(InquireBalBT);
    BTPanel.add(DepositBT);
    BTPanel.add(WithdrawBT);
    FormPanel.setLayout(new GridLayout(2,1));
    FormPanel.add(TextPanel);
    FormPanel.add(BTPanel);

    MainMenu.setContentPane(FormPanel);
    MainMenu.pack();
    MainMenu.show();
    MainMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    MainMenu.setResizable(false);

}

public void actionPerformed(ActionEvent e)
{     
    MainMenu.hide();

    if(e.getActionCommand().equals("Inquire Balance") || e.getActionCommand().equals("Withdraw") || e.getActionCommand().equals("Deposit"))
    {
        //----------------------------------//

        PINEnter = new JFrame("PIN");

        JPanel PINTextPanel   = new JPanel();
        JPanel PINButtonPanel = new JPanel();
        JPanel PINUniterPanel = new JPanel();

        JLabel PINTextLabel   = new JLabel("Please Enter PIN");

        JTextField PINField   = new JTextField(4); 

        JButton SubmitBT   = new JButton("Submit PIN");
        SubmitBT.addActionListener(this);

        PINTextPanel.setBackground(Color.white);
        PINTextPanel.add(PINTextLabel);
        PINTextPanel.add(PINField);
        PINButtonPanel.add(SubmitBT);
        PINUniterPanel.setLayout(new GridLayout(2,1));
        PINUniterPanel.add(PINTextPanel);
        PINUniterPanel.add(PINButtonPanel);

        PINEnter.setContentPane(PINUniterPanel);
        PINEnter.setSize(360,140);
        PINEnter.pack();
        PINEnter.show();
        PINEnter.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PINEnter.setResizable(false);

        PINNow =  PINField.getText();

        if(e.getActionCommand().equals("Inquire Balance")){OPTIONS = 1;}
        else if(e.getActionCommand().equals("Withdraw")){OPTIONS = 3;}
        else if(e.getActionCommand().equals("Deposit")){OPTIONS = 2;}

        if(e.getActionCommand().equals("Submit PIN") && PINNow.equals(RealPin))
        { // switch and then some functions which are too long}
}
}
}

这是我正在处理的代码,ATM模拟器。唯一的问题是当我到达PINEnter然后我按下SubmitBT时,它不会转到其他帧。 PINEnter有一个JTextField PINField,我应该将内容转换为String PIN。为了转到其他帧,PIN应该等于String RealPin,即“1234”。因此,如果提交PIN按钮和PIN等于RealPin,那么它应该已经与其他功能一起进行。我预计,当按下SubmitBT时,PIN与RealPIN相同,我应该转到if语句,但之后,它没有。

1 个答案:

答案 0 :(得分:2)

虽然在语法上你可以做到 - 而且它会编译,我认为它不会做你想要的。我们来看看你的代码:

if(e.getActionCommand().equals("Button in theFrame"))
{
    newFramerz.show();

    // If you got in here, then the value of e.getActionCommand() is "Button in theFrame"

    if (e.getActionCommand().equals("Button in newFramerz"))
    {
        //The execution will never get here, because
        //the value of e.getActionCommand() is "Button in theFrame"
        //and hence will never be equal to "Button in newFramerz"
    }
}

更合适的处理方法是:

String action = e.getActionCommand();

if(action.equals("Button in theFrame"))
{
    newFramerz.show();
    //whatever else
}
else if(action.equals("Button is newFramerz"))
{
    //do something else
}