gpt4 book ai didi

java - 使用 JFrame 的背景图像

转载 作者:行者123 更新时间:2023-11-30 03:28:31 25 4
gpt4 key购买 nike

这是我正在使用的 Yahtzee 游戏的一部分。我试图将背景设置为项目文件夹中的 yahtzee.png 文件。我评论了我这样做的尝试,因为它对我来说不起作用。有更好的方法来设置这个吗? This is how it looks now. There should be a custom image instead of the grey background.

     ExFrame(int numPlayers)
{
frame = new JFrame();
frame.setSize(450+150*numPlayers,700);

frame.setTitle("YAHTZEE!");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.numPlayers = numPlayers;
this.numGridRows = 20;
this.buttonWidth = 140;
this.numCreateButLabCalls = 0;
this.component = new DiceComponent(buttonWidth*2);
this.cButtons = new JButton[numGridRows];
this.cButtonsText = new String[numGridRows];
this.cLabels = new JLabel[numPlayers][numGridRows];
this.statusLabel = new JLabel("<html>New game has been started!<br>Please select the dice that you wish to hold or click on a scoring button</html>");
this.score = new YahtzeeScore[numPlayers];

//populate score array
for(int k = 0; k < numPlayers; k++)
{
score[k] = new YahtzeeScore(cButtons,cLabels, statusLabel, component.getDieArray(), cButtonsText, numGridRows, k);
}

statusLabel.setPreferredSize(new Dimension(buttonWidth*2, 100));
centerPanel = new JPanel(new GridLayout(numGridRows,numPlayers+1)); //columns based on numPlayers

component.rollDice(true);
popCenterPanel();
for(int k = 0; k < numPlayers; k++)
score[k].reset();
addListeners();

frame.setLayout(null);
frame.add(component);
frame.add(statusLabel);
frame.add(centerPanel);
// frame.add(new JLabel(new ImageIcon("/YahtzeeAgain/yahtzee.png")));
Insets insets = frame.getInsets();
Dimension size = statusLabel.getPreferredSize();
statusLabel.setBounds(100+ insets.left,insets.top,size.width,size.height);
size = component.getPreferredSize();
component.setBounds(insets.left, 150 + insets.top,
size.width, size.height);
size = centerPanel.getPreferredSize();
centerPanel.setBounds(290 + insets.left, 140 + insets.top,
size.width, size.height);
centerPanel.setBackground(Color.gray);
frame.add(this);
frame.setVisible(true);


}

最佳答案

最好的方法:

将您的背景 JLabel 添加到框架的内容 Pane (正如您所做的那样),然后对其执行 setLayout(null) ,并将所有其他组件添加到您的背景 JLabel

关于java - 使用 JFrame 的背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636765/

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