1

This is very hard to explain but when I am on the main menu I press button 1 to enter game, button 2 to enter ranks, button 3 to quit. Now if I press button 2 to enter the ranks then press button 1 to return to menu I go right into the game and not stay on menu.

Is there anyway I can code it to say on the menu instead of instantly entering the game?

Engine:

import javax.swing.*;

import com.wicke.main.game.Level1;

@SuppressWarnings("serial")
public class Engine extends JFrame {

    // Adding Classes
    Level1 lvl1 = new Level1(this);

    // System Variables
    public JButton button1;
    public JButton button2;
    public JButton button3;
    public JTextArea textArea1;

    public static void main(String[] args){

        new Engine();

    }

    public Engine(){
        // GUI Variables
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(900,600);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
        this.setTitle("Dungeon Drivers Alpha 0.2.3");

        JPanel thePanel = new JPanel();

        textArea1 = new JTextArea(33, 80);
        textArea1.setText("Window launch was Successful.\n");
        textArea1.setEditable(false);
        textArea1.setLineWrap(true);
        textArea1.setWrapStyleWord(true);
        thePanel.add(textArea1);

        JScrollPane scrollbar1 = new JScrollPane(textArea1, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        thePanel.add(scrollbar1);

        button1 = new JButton();
        button1.setText("1");
        thePanel.add(button1);

        button2 = new JButton();
        button2.setText("2");
        thePanel.add(button2);

        button3 = new JButton();
        button3.setText("3");
        thePanel.add(button3);

        this.add(thePanel);
        this.setVisible(true);

        lvl1.Game();
    }

}

Then there is Level1:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.wicke.main.Engine;
import com.wicke.main.data.Ranks;

public class Level1 {
    private Engine engine;

    public Level1(Engine engine) {
        this.engine = engine;
    }

    public void Game() {
        engine.textArea1.append("\tWelcome to Dungeon Drivers!\n");
        engine.textArea1.append("\tPress 1 to Start\n");
        engine.textArea1.append("\tPress 2 to Enter Local Leaderboard\n");
        engine.textArea1.append("\tPress 3 to Exit\n");
        engine.button1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   start();
               }
            });
        engine.button2.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   Leaderboard();
               }
            });
        engine.button3.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   System.exit(0);
               }
            });
    }

    private void Leaderboard() {
        engine.textArea1.setText("");
        engine.textArea1.append("\t1. " + Ranks.rank1 + "\n");
        engine.textArea1.append("\t2. " + Ranks.rank2 + "\n");
        engine.textArea1.append("\t3. " + Ranks.rank3 + "\n");
        engine.textArea1.append("\t4. " + Ranks.rank4 + "\n");
        engine.textArea1.append("\t5. " + Ranks.rank5 + "\n");
        engine.textArea1.append("\tPress 1 to return.");
        engine.button1.addActionListener(new ActionListener(){
               public void actionPerformed(ActionEvent e){
                   engine.textArea1.setText("");
                   Game();
               }
            });
    }

    private void start() {
        engine.textArea1.setText("");
        engine.textArea1.append("Test");
    }
}

1 Answers1

2

So, your code goes here, nice, you add an ActionListener to button1 and button2

engine.button1.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
         start();
     }
});
engine.button2.addActionListener(new ActionListener(){
     public void actionPerformed(ActionEvent e){
         Leaderboard();
     }
});

You press button2 and your code goes to here...

engine.button1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
       engine.textArea1.setText("");
       Game();
   }
});

Wait, what, you've added ANOTHER ActionListener to the button1, so when you press it, it will execute both the start and the Game methods ...

Instead, create separate components for each view, one for the menu, one for the leader board, one for the game, etc, each with their own internal management and functionality which is separate from the others.

Maybe utilise a CardLayout to facilitate the navigation between them

I'd encourage you to also learn about MVC and make use of it where you can.

While probably a little more advanced then you're use to, have a look at this example and this example for examples of using a CardLayout in a MVC style implementation

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366