1

I have to do a project in Java and thought a GUI Text Adventure would be cool. My Problem is that when I create a JPanel and move it further down on the screen, the panel first changes its size and then disappears completely at one point.

On the GameScreen there should be a panel for choice Options to be put on but it refuses to go further down than about half the size of the Screen.

Here's the code:

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Yeet {

    JFrame epicOfYeet;
    Container con;
    JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
    JLabel titleNameLabel;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
    Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
    JButton startButton;
    JButton choice1;
    JButton choice2;
    JButton choice3;
    JButton choice4;
    JTextArea mainTextArea;
    TitleScreenHandler tsHandler = new TitleScreenHandler();

    public static void main(String[] args) {

        new Yeet();
    }

    public Yeet() {

        epicOfYeet = new JFrame();
        epicOfYeet.setSize(1200, 1000);
        epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        epicOfYeet.getContentPane().setBackground(Color.black);
        epicOfYeet.setLayout(null);
        con = epicOfYeet.getContentPane();

        titleNamePanel = new JPanel();
        titleNamePanel.setBounds(190, 100, 800, 230);
        titleNamePanel.setBackground(Color.black);
        titleNameLabel = new JLabel("EPIC OF YEET");
        titleNameLabel.setForeground(Color.red);
        titleNameLabel.setFont(titleFont);

        startButtonPanel = new JPanel();
        startButtonPanel.setBounds(400, 500, 400, 100);
        startButtonPanel.setBackground(Color.black);

        startButton = new JButton("START");
        startButton.setBackground(Color.black);
        startButton.setForeground(Color.white);
        startButton.setFont(normalFont);
        startButton.addActionListener(tsHandler);
        startButton.setFocusPainted(false);

        titleNamePanel.add(titleNameLabel);
        startButtonPanel.add(startButton);
        con.add(titleNamePanel);
        con.add(startButtonPanel);

        epicOfYeet.setVisible(true);
    }

    public void createGameScreen(){

        titleNamePanel.setVisible(false);
        startButtonPanel.setVisible(false);
        mainTextPanel = new JPanel();
        mainTextPanel.setBounds(100, 100, 1000, 400);
        mainTextPanel.setBackground(Color.green);
        con.add(mainTextPanel);

        mainTextArea = new JTextArea("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
        mainTextArea.setBounds(100, 100, 1000, 250);
        mainTextArea.setBackground(Color.blue);
        mainTextArea.setForeground(Color.white);
        mainTextArea.setFont(normalFont);
        mainTextArea.setLineWrap(true);
        mainTextPanel.add(mainTextArea);

        choiceButtonPanel = new JPanel();
        choiceButtonPanel.setBounds(300, 500, 600, 550);
        choiceButtonPanel.setBackground(Color.red);
        con.add(choiceButtonPanel);
    }

    public class TitleScreenHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {

            createGameScreen();
        }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
ZeroxEvox
  • 11
  • 1
  • I think your screen resolution causes this problem. You are setting size as 1200x1000, if your screen is like 1440x900, bound violation might be occurs. This is why you see the panel when you resize as I thought. – Y.Kakdas Feb 25 '19 at 13:21
  • 3
    Welcome to SO. Do not set layout manager to null. Instead learn [how to use layout managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) – c0der Feb 25 '19 at 13:24
  • ... and learn about EDT and how to build and handle an UI in the correct thread: https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – spi Feb 25 '19 at 13:28

1 Answers1

1

Here is a basic implementation using layout mangers, avoiding the bad practice of null layout manager.
It is not an optimal one, but should get you started:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Yeet {

    JFrame epicOfYeet;
    Container con;
    JPanel titleNamePanel, startButtonPanel, mainTextPanel, choiceButtonPanel;
    JLabel titleNameLabel;
    Font titleFont = new Font("Times New Roman", Font.PLAIN, 90);
    Font normalFont = new Font ("Times New Roman", Font.PLAIN, 55);
    JButton startButton, coice1, choice2, choice3, choice4;
    JTextArea mainTextArea;
    TitleScreenHandler tsHandler = new TitleScreenHandler();

    public static void main(String[] args) {

        SwingUtilities.invokeLater(()->new Yeet());
    }

    public Yeet() {

        epicOfYeet = new JFrame();
        //epicOfYeet.setSize(1200, 1000); // do not set size. let layout manager calcualte it
        epicOfYeet.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        epicOfYeet.getContentPane().setBackground(Color.black);
        //epicOfYeet.setLayout(null); //aviod null layouts Jframe uses BorderLayout by default
        con = epicOfYeet.getContentPane();

        titleNamePanel = new JPanel();
        //titleNamePanel.setBounds(190, 100, 800, 230);
        titleNamePanel.setPreferredSize(new Dimension(800, 230));//set preferred size to be used by layout manager
        titleNamePanel.setBackground(Color.black);
        titleNameLabel = new JLabel("EPIC OF YEET");
        titleNameLabel.setForeground(Color.red);
        titleNameLabel.setFont(titleFont);

        startButtonPanel = new JPanel();
        //startButtonPanel.setBounds(400, 500, 400, 100);
        startButtonPanel.setPreferredSize(new Dimension(400, 100));
        startButtonPanel.setBackground(Color.black);

        startButton = new JButton("START");
        startButton.setBackground(Color.black);
        startButton.setForeground(Color.white);
        startButton.setFont(normalFont);
        startButton.addActionListener(tsHandler);
        startButton.setFocusPainted(false);

        titleNamePanel.add(titleNameLabel);
        startButtonPanel.add(startButton);
        con.add(titleNamePanel, BorderLayout.PAGE_START);  //set to top
        con.add(startButtonPanel, BorderLayout.PAGE_END); //set to bottom
        epicOfYeet.pack();
        epicOfYeet.setVisible(true);
    }

    public void createGameScreen(){

        //titleNamePanel.setVisible(false);
        //startButtonPanel.setVisible(false);
        con.remove(titleNamePanel);
        con.remove(startButtonPanel);
        mainTextPanel = new JPanel();
        //mainTextPanel.setBounds(100, 100, 1000, 400);
        mainTextPanel.setBackground(Color.green);
        con.add(mainTextPanel); // added to center position of the BorderLayout (the default)

        mainTextArea = new JTextArea(10, 20); //size in rows, cols
        mainTextArea.setText("You come to your senses again.\nThe dewy grass you're laying on is gleaming with moonlight.\nYour body aches as you get up and catch a \nglimpse of your surroundings.\n");
        //mainTextArea.setBounds(100, 100, 1000, 250);
        mainTextArea.setBackground(Color.blue);
        mainTextArea.setForeground(Color.white);
        mainTextArea.setFont(normalFont);
        mainTextArea.setLineWrap(true);
        mainTextPanel.add(mainTextArea);

        choiceButtonPanel = new JPanel();
        //choiceButtonPanel.setBounds(300, 500, 600, 550);
        choiceButtonPanel.setPreferredSize(new Dimension(600, 550));
        choiceButtonPanel.setBackground(Color.red);
        con.add(choiceButtonPanel, BorderLayout.PAGE_END);//add to bottom
        epicOfYeet.pack();
    }

    public class TitleScreenHandler implements ActionListener{

        @Override
        public void actionPerformed(ActionEvent event) {
            createGameScreen();
        }
    }
}
c0der
  • 18,467
  • 6
  • 33
  • 65