Moving from Command prompt to a gui. advice?

So here’s what I want to do for my first go.
I want to Have a frame with two panels one that holds the text to update the player whats happening (basically the command prompt)
The second holding button for the player to press, to make things happen.

I know that two classes right now have to have access to the JPanel with the buttons, however many classes need access to the text area.

would be making the gui in its own class be a good idea?
I’m also going to need to make a class that makes buttons that give access to buttons but how do I handle the event?

I understand how to make things happen when the event is press but I’m unsure how to do it in a case like this.

Thank-you once again for the advice and help everyone you all have made this journey that much better.

I think the most popular way of doing things is to do the whole thing in a single class (provided that it isn’t too large). You declare all the elements of the gui (like buttons and text areas) as fields of the class. You initialize / setup the elements either in the constructor or an init() method called from the constructor. That way all the elements are “visible” from each other and you can refer to them in your action events directly.

The alternative is to make a more intuitive split and, for example, make each panel its own class. Then it is best to expose functionality in the panel through public methods.

A GUI creator like Netbeans’ Matisse is IMO simply the best way forward for these things. If you don’t like swing then even JavaFX has its own Scene Builder now. I only code GUIs myself for the very simplest of tasks.

Definitely seconded, the Netbeans GUI builder is very useful. If you like coding GUIs by hand try TableLayout.

so here is my main class. My main problem that I can’t wrap my head around is if I do the gui in a seperate class how do I make it do things. I know I would use actionlistener but if all that is another class even if I pass the data in on creation of the button lets say once the button is pressed it would just give an error because the object is out of scoop. Looking at the above does that mean I would create the action listeners and buttons in the class that would use them and just have a method in my gui class that would add them and remove them from the panel. Sorry if large code dump I’m not sure whats consided too large and needed for pastebin. Thank-you for your help everyone


public class Escape {
	
	//object
	Player player = new Player("Sam", 50, 8, 4, 6, 4);
	Scanner input = new Scanner(System.in);
	locationObjects location = new locationObjects();
	//gui stuff later
	
	//global varibles
	private boolean done = false;
	String command= null;
	int currentXCord = 0;
	int currentYCord = 0;
	
	Room currentRoom;
	
	public static void main(String[] args) {
		Escape game = new Escape();
		game.go();

	}
	//constructor for future gui creation.
	private Escape(){
		
	}
	
	public void go(){
		System.out.println("You awaken in a dark empty cell you can't remember how you got here nor can you remember who you are.\nAll you know is that you need you need to get out of here.");
		System.out.println("To go forward type go, and to search the room type search");
		location.addRooms(1);
		currentRoom = location.getRoom(0, 0);
                while(done == false){
			if(player.getHealth() <= 0){
                            gameOver();
                            done = true;
                            break;
                        }
                        command = input.nextLine();
			testWhatToDo(command);
			
			
		}
		
		
	}
	
	private void testWhatToDo(String action) {
		if(action.equalsIgnoreCase("search")){
			search();
		}else if(action.equalsIgnoreCase("go")){
			if(currentRoom.hasExit() == true){
				this.Win();
			}else{
				boolean gettingMove = true;
				while(gettingMove == true){
					System.out.println("Which Direction do you want to go? (try things like left, forward, down, or south)");
					String move = input.nextLine();
                                        if(move.equalsIgnoreCase("forward") || move.equalsIgnoreCase("up") || move.equalsIgnoreCase("north")){
						
                                                
						if(currentRoom.getUp() == true && currentRoom.DoorNotLocked("up") == true){
						currentYCord++;
                                                gettingMove = false;
                                                newRoom();
						}else{
                                                    gettingMove = false;
						}
					}else if(move.equalsIgnoreCase("back") || move.equalsIgnoreCase("south")){
						gettingMove = false;
                                                if(currentRoom.getDown() == true && currentRoom.DoorNotLocked("down") == true){
							currentYCord--;
                                                        newRoom();
						}else{
                                                    gettingMove = false;
                                                    System.out.println("Theres no way to travel though the wall");
						}
					}else if(move.equalsIgnoreCase("left") || move.equalsIgnoreCase("west") || move.equalsIgnoreCase("left side") ){
							if(currentRoom.isLeft() == true && currentRoom.DoorNotLocked("left") == true){
								gettingMove = false;
                                                                currentXCord--;
                                                                newRoom();
							}else{
								gettingMove = false;
                                                                System.out.println("there is no way.");
							}
						}else if(move.equalsIgnoreCase("right") || move.equalsIgnoreCase("east") || move.equalsIgnoreCase("right side") ){
							if(currentRoom.isRight() == true && currentRoom.DoorNotLocked("right") == true){
								gettingMove = false;
                                                                currentXCord++;
                                                                newRoom();
							}else{
								gettingMove = false;
                                                                System.out.println("this once contained a meme referenace but now its gone");
							}
						}else{
                                                    gettingMove = false;
                                                }
					}
				}
				
                              //end of go block
                    }else if(action.equalsIgnoreCase("bag") || action.equalsIgnoreCase("inventory") || action.equalsIgnoreCase("items")){
                        if(player.getItemList() == true){
				System.out.println("You have no items");
				
			}else{
				System.out.println("What item do you want to use? enter a number associated with an item to use it.");
				player.getContentsOfBag();
                                boolean itemUsed = false;
				itemuse:
				while(itemUsed == false){
                                        int i;
                                                try{
						i = Integer.parseInt(input.nextLine());                                               
                                                }catch(Exception ex){
                                                 System.out.println("umm was that a numeric number? lets try that again");
						break itemuse;
                                                }
                                                
                                                itemUsed = true;
                                                Item item = player.getItemFromBag(i);
						item.doWhatYouDo(player, currentRoom, i);
                                                
                                                
					
						
					}
					
					
				}
			}else{
                        System.out.println("Sorry thats not a valid action");
                    }
		}
	
	private void Win() {
		System.out.println("You have beaten the game Congraulations");
		done = true;
		
	}
	
	public void newRoom(){
		
		Room nRoom;
                nRoom = location.getRoom(currentXCord, currentYCord);
		currentRoom = nRoom;
		System.out.println("\nYou walk into " + currentRoom.getName() +". Upon entrance you look around you and see "
				+ currentRoom.getDescription());
		Random rand = new Random();
		if(currentRoom.onEntranceEventTest() == true){
                    currentRoom.getOnEntranceEvent();
                }else{
                int i = rand.nextInt(100);
		if(currentRoom.isEvent() == true){
			//location.getRoom(currentRoom).getEvent(s);
		}
		else if(currentRoom.hasBaddies() == true){
		if(i < 35){
			int e = rand.nextInt(5);
			Baddie badguy = currentRoom.getBaddie(e);
			Battle battle = new Battle(player, badguy, input);
			
		}
		}
		System.out.println("\nWhat will you do now?");
               }
	}
	
	public void search() {
		Random rand = new Random();
		int i;
		i = rand.nextInt(50);
		
		if(i <= 25){
		 if(currentRoom.isBaddiesEmpty() == false){
                        int e = rand.nextInt(5);
			Baddie badguy = currentRoom.getBaddie(e);
			Battle battle = new Battle(player, badguy, input);
                        
                 }else{
                    System.out.println("You didn't find anything.");
                 }
		}else if(i > 25 && i < 50 ){
                    if(currentRoom.NoItems() == true){
                        System.out.println("There are no items of use in this room.");
                    }else{
                    currentRoom.getAnItem(i, player);
                    }
		}else if(i == 50){
			
		}
		
	}

    private void gameOver() {
        System.out.println("You have fallen and your journey ends here! You never escape and now you never will.");
    }
	
	
}//end class

heres my gui class as stands


public class Gui {
    JFrame frame;
    JPanel mainArea;
    JPanel choiceArea;
    JScrollPane scrollBar;
    JPanel charArea;//for later use
    JPanel mapArea;
    JTextArea textArea;
    
    
    public Gui(){
        
        frame = new JFrame("Escape");
        frame.setSize(700, 800);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainArea = new JPanel();
        choiceArea = new JPanel();
        choiceArea.setSize(700, 150);
        
        textArea = new JTextArea();
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        
        scrollBar = new JScrollPane();
        scrollBar.setVerticalScrollBarPolicy(scrollBar.VERTICAL_SCROLLBAR_AS_NEEDED);
        scrollBar.setHorizontalScrollBarPolicy(scrollBar.HORIZONTAL_SCROLLBAR_NEVER);
        scrollBar.add(textArea);
        
        
        mainArea.add(scrollBar);
        frame.add(BorderLayout.CENTER, mainArea);
        frame.add(BorderLayout.SOUTH, choiceArea);
        frame.validate();
        frame.setVisible(true);
    }
    
    public void addTextToTextArea(String s){
        textArea.append("\n" + s);
    }
    
    
    
}


I think you are going the wrong way. I think what you want is something like a HUD inside a game. Swing components are not suitable for that. I also don’t know why you would remove and add actionListeners continuously. You just would check game states in your actionPerformed method if you would need to change the functionallity of a button.

Also your code suggests that you are quite a beginner in java programming. I would recommend you to get a little bit more familiar with java before even considering making games.

Well I’ve already gone though a few books so this is my way of continuing to try and learn. What would you suggest to better understand the art of coding in java, and not be such a beginner?

Make a JTextArea on one side of the JFrame and your JButtons on the other side of the JFrame. Instead of printing to the console just append whatever text you’re outputting to the JTextArea instead.

This was my plan. The part I have trouble with is how I go about handling the button when it changes and how to arrange them to work with the method. I have some idea which I’m working on now.

Thank-you for your input.