简体   繁体   English

无法在ActionListener中更改JButton ActionCommand

[英]Cannot change JButton ActionCommand inside ActionListener

I cannot seem to change JButton ActionCommand inside an ActionListener. 我似乎无法在ActionListener中更改JButton ActionCommand。 I am trying to use the same two buttons multiple time by changing the ActionCommand, but that doesn't seem to work. 我试图通过更改ActionCommand多次使用相同的两个按钮,但这似乎不起作用。 Here's my code, hopefully you can help: 这是我的代码,希望你可以提供帮助:

void ChangeAction() {
    storyButn1.setActionCommand("but3");
    storyButn2.setActionCommand("but4");
}

public Main() {

    story.setText("<html>You wake up with a sore head in a dark forest. You can't remember anything, except from the word H.E.L.P. You see a light in the distance, What to do? </html>");

    storyButn1.addActionListener(this);
    storyButn1.setActionCommand("but1");

    storyButn2.addActionListener(this);
    storyButn2.setActionCommand("but2");

    Box Layout = new Box(BoxLayout.Y_AXIS);
    Layout.add(story);
    Layout.setSize(story.getWidth() + 10, story.getHeight());

    frame.setVisible(true);
    frame.setSize(400, 200);
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(Layout, BorderLayout.CENTER);
    frame.add(storyButn1);
    frame.add(storyButn2);
    frame.setTitle("Text Adventure");

    frame.setLayout(new GridLayout(3, 2, 5, 5));
    frame.getContentPane().setBackground(Color.lightGray);

}

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("but1")) {
        random = (int) (Math.round(Math.random()) * 10);
        if (random == 10) {
            story.setText("<html>You run towards the light source. It's dark and you don't see that root in the ground. You break your ankle!</html>");
            storyButn1
                    .setText("<html>Lay against a tree and hope for the best?</html>");
            storyButn2.setText("<html>Call out for help?</html>");
        } else {
            story.setText("<html>You walk carefully towards the light source, When you reach it, you find that it is a torch.</html>");
            storyButn1.setText("Pick it up?");
            storyButn2.setText("Switch it off?");
        }
    }
    if (e.getActionCommand().equals("but2")) {
        random = (int) (Math.round(Math.random()) * 20);
        if (random == 20) {
            story.setText("<html>You are near the top of the tree when your foot slips. You go plummeting back into the darkness, never to be seen again.");
            storyButn1.setText("Retry?");
            storyButn2.setText("Retry?");
            if (e.getActionCommand().equals("but1") || e.getActionCommand().equals("but2")) {
                this.dispose();
                frame.setVisible(false);
                new Main();             
            }
        } else {
            story.setText("<html>You successfully climb to the top of the tree where you find a safe spot to sit. You stay there for a while, *BANG*, You hear something in the distance!</html>");
            ChangeAction();
            storyButn1.setText("Climb down and investigate?");
            storyButn2.setText("Follow the light source?");

                if(e.getActionCommand().equals("but3")){
                    story.setText("<html>You slowly climb down the tree. When you reach the bottom, you try to follow where you think the sound had come from. As your walking you look down. There is a loaded revolver with one bullet missing and a trail of blood.</html>");
                } else if(e.getActionCommand().equals("but4")){
                    story.setText("<html>You walk carefully towards the light source, When you reach it, you find that it is a torch.</html>");
                }
        }
    }

}

There is only one place where you do something if the action command is "but3" or "but4" , and this place is inside the following if block: 如果动作命令是"but3""but4" ,则只有一个地方可以执行某些操作,并且此位置位于以下if块中:

if (e.getActionCommand().equals("but2")) {

Obviously, it's impossible for the action command to be both "but2" and "but3" or "but4" . 显然,动作命令不可能是"but2""but3""but4" It's one or the other. 这是一个或另一个。

So the conditions of the following if blocks will always evaluate to false : 因此,以下if块的条件将始终评估为false

if(e.getActionCommand().equals("but3")){
    story.setText("<html>You slowly climb down the tree. When you reach the bottom, you try to follow where you think the sound had come from. As your walking you look down. There is a loaded revolver with one bullet missing and a trail of blood.</html>");
} else if(e.getActionCommand().equals("but4")){
    story.setText("<html>You walk carefully towards the light source, When you reach it, you find that it is a torch.</html>");
}

EDIT: 编辑:

to recap, the actionPerformed() method looks like this: 回顾一下, actionPerformed()方法如下所示:

public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("but1")) {
        // do something
    }
    if (e.getActionCommand().equals("but2")) {
        // do something
    }
}

It doesn't handle the case where the action command is "but3" or "but4" . 它不处理动作命令为"but3""but4"

you can't reach this part of code 你无法达到这部分代码

else {
        story.setText("<html>You successfully climb to the top of the tree where you find a safe spot to sit. You stay there for a while, *BANG*, You hear something in the distance!</html>");
        ChangeAction();
        storyButn1.setText("Climb down and investigate?");
        storyButn2.setText("Follow the light source?");
    }

and here where you call ChangeAction(). 在这里你调用ChangeAction()。 test using system.out.println("inside else"); 使用system.out.println("inside else"); you will see that this line will never be printed. 你会看到这条线永远不会被打印出来。

you need to use ChangeAction() somewhere inside the 1st two if statments 你需要在前两个内部的某个地方使用ChangeAction()if if statments

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM