1

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

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,我应该将内容转换为字符串 PIN。为了转到其他帧,PIN 应该等于 String RealPin,即“1234”。因此,如果提交 PIN 按钮和 PIN 等于 RealPin,那么它应该已经与其他功能一起进行。我希望当 SubmitBT 被按下时,PIN 与 RealPIN 相同,我应该去 if 语句,但是,它没有。

4

1 回答 1

2

While syntactically you can do it - and it will compile, I don't think it will do what you're looking for. Let's look at your code:

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"
    }
}

A more appropriate way of dealing with it would be:

String action = e.getActionCommand();

if(action.equals("Button in theFrame"))
{
    newFramerz.show();
    //whatever else
}
else if(action.equals("Button is newFramerz"))
{
    //do something else
}
于 2013-10-21T12:50:28.030 回答