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);
}
}
我的程序正常显示,直到我调用setLayout(newBoxLayout(this,BoxLayout.PAGE_AXIS));在DealerPanel的构造函数中。在我调用该函数后,我的整个程序被扩展到右侧,显示空白区域。之前,它被设置为显示600乘600的图像,底部添加了按钮(OptionPanel)。谁能告诉我我做错了什么?
答案 0 :(得分:1)
我认为那是因为你在构造函数中指定了this
(这个JPanel可能没有完全初始化)。尝试更换:
setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
使用:
setLayout(new BorderLayout());
注意:根据我们在此处的代码中看到的情况,将此BoxLayout
替换为BorderLayout
不应对此面板内的布局产生任何影响,因为您未向此面板添加任何组件在你的代码中。如果不是这种情况,请阅读BorderLayout
Javadoc。