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-Simulator です。唯一の問題は、PINEnter に到達してから SubmitBT を押すと、他のフレームに移動しないことです。PINEnter には JTextField PINField があり、コンテンツを文字列 PIN に変換する必要があります。他のフレームに移動するために、PIN は "1234" である String RealPin と等しくなければなりません。そのため、[PIN の送信] ボタンと PIN が RealPin と等しい場合は、他の機能で既に実行されているはずです。PIN が RealPIN と同じで、SubmitBT が押されたときに、if ステートメントに進むべきだと思っていましたが、そうではありませんでした。

4

1 に答える 1

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
}
于 2013-10-21T12:50:28.030 に答える