gpt4 book ai didi

java - 将 BoxLayout 设置为我的 JPanel 后,我的图像无法正常显示

转载 作者:太空宇宙 更新时间:2023-11-04 06:56:20 26 4
gpt4 key购买 nike

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) {
}
}

经销商面板:

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);
}
}

玩家面板:

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);
}
}

我的程序正确显示,直到我调用 setLayout(newBoxLayout(this,BoxLayout.PAGE_AXIS));在 DealerPanel 的构造函数中。调用该函数后,我的整个程序向右扩展,显示空白。之前,它被设置为显示 600 x 600 的图像,并在底部添加按钮(OptionPanel)。谁能告诉我我做错了什么?

最佳答案

我认为这是因为您在构造函数中指定了 this (此 JPanel 可能未完全初始化)。尝试替换:

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

与:

setLayout(new BorderLayout());

注意:从我们在此处的代码中看到的情况来看,用 BorderLayout 替换此 BoxLayout 不会对此面板内的布局产生任何影响,因为您没有在代码中向此面板添加任何组件。如果不是这种情况,请阅读BorderLayout Javadoc。

关于java - 将 BoxLayout 设置为我的 JPanel 后,我的图像无法正常显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22701149/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com