Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot change JButton ActionCommand inside ActionListener
    primarykey
    data
    text
    <p>I cannot seem to change JButton ActionCommand inside an ActionListener. I am trying to use the same two buttons multiple time by changing the ActionCommand, but that doesn't seem to work. Here's my code, hopefully you can help:</p> <pre><code>void ChangeAction() { storyButn1.setActionCommand("but3"); storyButn2.setActionCommand("but4"); } public Main() { story.setText("&lt;html&gt;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? &lt;/html&gt;"); 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("&lt;html&gt;You run towards the light source. It's dark and you don't see that root in the ground. You break your ankle!&lt;/html&gt;"); storyButn1 .setText("&lt;html&gt;Lay against a tree and hope for the best?&lt;/html&gt;"); storyButn2.setText("&lt;html&gt;Call out for help?&lt;/html&gt;"); } else { story.setText("&lt;html&gt;You walk carefully towards the light source, When you reach it, you find that it is a torch.&lt;/html&gt;"); 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("&lt;html&gt;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("&lt;html&gt;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!&lt;/html&gt;"); ChangeAction(); storyButn1.setText("Climb down and investigate?"); storyButn2.setText("Follow the light source?"); if(e.getActionCommand().equals("but3")){ story.setText("&lt;html&gt;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.&lt;/html&gt;"); } else if(e.getActionCommand().equals("but4")){ story.setText("&lt;html&gt;You walk carefully towards the light source, When you reach it, you find that it is a torch.&lt;/html&gt;"); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload