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)。有人能告诉我我做错了什么吗?
我认为这是因为您在构造函数中指定了this
(这个JPanel可能没有完全初始化)。试着更换:
setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
:
setLayout(new BorderLayout());
注意:从我们在这里看到的代码中,用BorderLayout
替换这个BoxLayout
应该不会对这个面板内的布局产生任何影响,因为你没有在代码中向这个面板添加任何组件。如果不是这样,请阅读BorderLayout
Javadoc。