JDeffo
JDeffo

Reputation: 73

Java Frame problems

I am developing Tetris for fun/to learn more about Java. I am having problems with the JFrame aspect of it. I have the actual game portion, which is a on the left side of the screen with the score, level, and high score to the right of this. Then, under the score, level, and high score I am trying to put in a button. Here is my code:

public class Tetris implements World {
    boolean pause = false; // for pausing the game
    boolean end = false; // for ending the game
    static int score = 0; // score.  Increments of 100
    static int level = 1; // indicates level.  Increments of 1.
    static int highScore = 1000; // indicates the overall high score
    static final int ROWS = 20; // Rows of the board
    static final int COLUMNS = 10; // Columns of the board

    Tetromino tetr, ghost, od1, od2, od3; // Tetr is the tetromino currently following.  Ghost is the shadow blocks.
    SetOfBlocks blocks; //SetOfBlocks on the ground

    Tetris(Tetromino tetr, SetOfBlocks blocks) {
        this.tetr = tetr;
        this.blocks = blocks;
    }

    //Main Method
    public static void main(String[] args) {
        BigBang game = new BigBang(500, new Tetris(Tetromino.pickRandom(), new SetOfBlocks()));
        JFrame frame = new JFrame("Tetris");

        //JButton
        JButton toggleGhost = new JButton("Toggle Ghost");
        toggleGhost.setFont(new Font("default", Font.PLAIN, 10));
        Dimension size = new Dimension(100, 25);
        toggleGhost.setPreferredSize(size);
        toggleGhost.setLocation(217, 60);

        //frame
        //frame.getContentPane().add( toggleGhost );
        frame.getContentPane().add(game);
        //frame.getContentPane().add( toggleGhost );
        frame.addKeyListener(game);
        frame.setVisible(true);
        frame.setSize(Tetris.COLUMNS * Block.SIZE + 150, Tetris.ROWS * Block.SIZE + 120); // Makes the board slightly wider than the rows
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        game.start();

BigBang is a class that extends JComponent and deals with the Timer primarily. If I uncomment the portion where I add the toggleGhost button to the frame, then it takes up the entire frame. I have tried many different alternatives with panels and containers but I can't seem to find the right combination where both the game and button display.

Upvotes: 0

Views: 150

Answers (2)

goncalotomas
goncalotomas

Reputation: 1000

Like ACV said, you are adding objects to your JFrame without using a LayoutManager. You should use a LayoutManager when you want some control over how things will display (not when you're adding a single object to the frame). Consider BorderLayout or BoxLayout.

For instance, using BorderLayout you would have to add the following code:

frame.setLayout(new BorderLayout());
frame.add(game, BorderLayout.CENTER);
frame.add(toggleGhost, BorderLayout.SOUTH);

To understand the difference between setSize() and setPreferedSize(), see this question.
The best practice is to add a LayoutManager to whatever component you are using (JFrame, JPanel, ...) and use setPreferedSize(), setMinimumSize() or setMaximumSize().

Upvotes: 0

ACV
ACV

Reputation: 10560

Because you should use LayoutManager . And setPreferredSize doesn't guarantee the size.

Upvotes: 1

Related Questions