gpt4 book ai didi

java - 嵌套 if(e.getActionCommand().equals ("")

转载 作者:行者123 更新时间:2023-11-30 04:11:31 24 4
gpt4 key购买 nike

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

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 语句,但后来却没有。

最佳答案

虽然从语法上讲你可以做到这一点 - 并且它会编译,但我认为它不会做你正在寻找的事情。让我们看看您的代码:

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
}

关于java - 嵌套 if(e.getActionCommand().equals (""),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19494136/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com