Question

package BlackjackPanels;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;

public class MainPanel extends JFrame implements ActionListener {

    private JPanel background;
    BufferedImage backgroundImage=ImageIO.read(new File("src/BlackjackImages/blackjackBackground.jpg"));

    public MainPanel() throws IOException { 
        super("Alan's Blackjack");
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        background = new JPanel() 
        {
            @Override
            protected void paintComponent(Graphics g)
            {       
                super.paintComponent(g);
                g.drawImage(backgroundImage, 0, 0, this);
            }
        };
        add(background);
        loadGame();
        pack();
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
    }



    public void loadGame() throws IOException {

        background.setLayout(new BorderLayout(0,0));
        PlayerPanel player=new PlayerPanel();
        DealerPanel dealer=new DealerPanel();
        OptionPanel option= new OptionPanel();
            dealer.setBox();
        JPanel gameBoard=new JPanel();
        gameBoard.setOpaque(false);
        gameBoard.setLayout(new BoxLayout(gameBoard,BoxLayout.PAGE_AXIS));
        gameBoard.add(dealer);
        gameBoard.add(player);
        background.add(gameBoard, BorderLayout.CENTER);
        background.add(option, BorderLayout.PAGE_END);
    }

    public static void main(String [] args) throws IOException {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    MainPanel game = new MainPanel();
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }
}

DealerPanel:

public class DealerPanel extends JPanel {

private JLabel moneyAmt;
private JLabel betAmt;
private JPanel status;

public DealerPanel() {
    super();
    setPreferredSize(new Dimension(600,300));
    setOpaque(false);
    setFocusable(true);
    setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY), "DEALER"));
    moneyAmt = new JLabel("Your money:");
    betAmt = new JLabel("Your bet:");
    moneyAmt.setForeground(Color.red);
    betAmt.setForeground(Color.red);
}

public void setBox() {
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
    add(moneyAmt);
    add(betAmt);
}
}

PlayerPanel:

package BlackjackPanels;
public class PlayerPanel extends JPanel {

    Image cardImg=Card.loadCardImage();

    public PlayerPanel() throws IOException {
        super();
        setPreferredSize(new Dimension(600,300));
        setOpaque(false);
        setFocusable(true);
        setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.DARK_GRAY), "PLAYER"));

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(cardImg,0,0,this);
    }
}

My program displayed properly until I called setLayout(newBoxLayout(this,BoxLayout.PAGE_AXIS)); in the constructor of DealerPanel. After I called the function, my entire program was extended to the right, showing white space. Before, it was set showing a image of 600 by 600 with buttons (OptionPanel) added at the bottom. Can anyone tell me what I did wrong?

Was it helpful?

Solution

I think that's because you are specifying this in constructor (this JPanel may not be completely initialized). Try replacing:

setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));

with:

setLayout(new BorderLayout());

Note: From what we see in your code here, replacing this BoxLayout with a BorderLayout should not have any impact on your layout inside this panel because you don't add any component to this panel in your code. If this is not the case, please read BorderLayout Javadoc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top