简体   繁体   中英

jpanel will not place over jframe

Good day, first time poster here. I've been working on homework involving a fish tank gui that generates and displays fish swimming in a 800 by 500 tank. The issue at hand is that now I must add to the frame something on the bottom that displays information. My problem is that our class had been given almost no training in working with a gui or instructions and all my attempts leave me with a mess of code and no visible jpanel or container. At best I just end up with an area where the panel would go but with nothing displaying and a blue background. What would it take for me to at least add to the bottom of the tank a visible jpanel that I can add components to?

Here is the code I was provided for the gui:

package fishtank.view;

import fishtank.model.Fish;
import fishtank.model.Tank;

/**
 * GUI frame for the fish tank that displays the frame and places the fish
 * within it.
 * 
 */
public class FishTankGUI extends JFrame {

    private Tank tank; //Class that generates the fish onto the frame.

    // Required as JFrame implements Serializable interface
    private static final long serialVersionUID = 1L;
    private static final int REFRESH_TIME_MS = 500;

    /**
     * Default constructor that sets up the GUI frame.
     * @param width
     *            The width of the frame
     * @param height
     *            The height of the frame
     * @param titleBarText
     *            The text to display on the title bar
     */
    public FishTankGUI(int width, int height, String titleBarText) {
        super(titleBarText);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setSize(width, height); //another class defines this as 800 by 500.
        setResizable(false);
        setVisible(true);


    }

    /**
     * Fills the tank and puts it in motion.
     * <p>
     * Precondition: none
     */
    public void start() {
        this.setupTank();
        this.animateTank();

    }

    /**
     * Fills the tank and puts it in motion.
     * @param totalFish
     *            The number of fish to place in the tank.
     */
    public void start(int totalFish) {
        this.setupTank(totalFish);
        this.animateTank();
    }

    private void setupTank() {
        TankPane tankPane = new TankPane();
        getContentPane().add(tankPane);
        getContentPane().validate();

        this.tank = new Tank(tankPane);
    }

    private void setupTank(int totalFish) {
        TankPane tankPane = new TankPane();
        getContentPane().add(tankPane);
        getContentPane().validate();

        this.tank = new Tank(tankPane, totalFish);
    }

    private void animateTank() {
        if (this.tank == null) {
            this.setupTank();
        }

        while (true) {
            this.pause(REFRESH_TIME_MS);

this.tank.moveFish();
}
}
private void pause(long milliseconds) {
if (milliseconds <= 0) {
throw new IllegalArgumentException("milliseconds is out of range.");
}

try {
Thread.sleep(milliseconds);
} catch (InterruptedException ie) {
System.err.println(ie);
}
}

}

Any help that can be provided, even the slightest amount, would be greatly appreciated!

I created a second class that extends JPanel. Then, within setupTank(int totalFish) I changed tankPane to use BorderLayout CENTER and added my second JPanel class with BorderLayout SOUTH. This added a second panel at the bottom.

Let me know if I need to be more specific or if you figured out a better way. I'm in your class. I found this thread while Googling solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM