【问题标题】:Cannot change JButton ActionCommand inside ActionListener无法更改 ActionListener 中的 JButton ActionCommand
【发布时间】:2013-07-27 13:45:05
【问题描述】:

我似乎无法在 ActionListener 中更改 JButton ActionCommand。我试图通过更改 ActionCommand 多次使用相同的两个按钮,但这似乎不起作用。 这是我的代码,希望对您有所帮助:

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

}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖 SSCCE。顺便说一句 - “走路时你往下看。” 应该是 “当你走路时你往下看。”
  • 它仍处于开发阶段,这不是问题
  • “它仍处于开发阶段,” 这是否意味着“不,我不希望尽快获得更好的帮助?” “这不是问题” 因此我的“不是答案” - 这就是 cmets 的目的。
  • actionlistener 方法中设置JButtonactioncommand 无处可寻..
  • 您在发布的代码中哪里更改了操作命令?

标签: java swing action jbutton actionlistener


【解决方案1】:

如果action命令是"but3""but4",只有一个地方可以做某事,并且这个地方在下面的if块里面:

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

显然,操作命令不可能同时是"but2""but3""but4"。这是一个或另一个。

因此,以下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>");
}

编辑:

回顾一下,actionPerformed() 方法如下所示:

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

它不处理action命令为"but3""but4"的情况。

【讨论】:

  • 那么我该如何设置我的 JButton 以拥有不同的 ActionCommand?
  • 你已经这样做了。问题不在于更改操作命令。当事件有这个新的动作命令时,它在 actionPerformed 中做一些事情。目前,您的代码仅处理两个操作命令:but1 和 but2。对于所有其他操作命令,它什么都不做。
  • 哦,对了,谢谢你的解释我现在明白我的问题了!谢谢!
  • 试过了,现在已经修好了!
【解决方案2】:

你无法访问这部分代码

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?");
    }

在这里调用 ChangeAction()。 使用system.out.println("inside else"); 进行测试,你会发现这行永远不会被打印出来。

你需要在第一个两个 if 语句中的某处使用 ChangeAction()

【讨论】:

    猜你喜欢
    • 2020-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-17
    • 2021-10-19
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多