gpt4 book ai didi

java - 设置背景图片

转载 作者:行者123 更新时间:2023-11-29 05:45:21 24 4
gpt4 key购买 nike

我怎样才能去掉那个灰色框?

这就是我要说的:

如果你能帮助我,我将不胜感激

完整代码在这里:http://pastebin.com/nrpCTjvV

public final void initUI() {

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

panel.setBorder(new EmptyBorder(new Insets(90, 155, 40, 60)));

JButton NewGame = new JButton ("New Game!");
JButton Highscore = new JButton("Highscore");
JButton Credits = new JButton ("Credits");
JButton Website = new JButton ("Website");
JButton Exit = new JButton ("Exit");

panel.add(NewGame);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Highscore);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Credits);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Website);
panel.add(Box.createRigidArea(new Dimension(0, 5)));
panel.add(Exit);

final ButtonGroup entreeGroup = new ButtonGroup();
JRadioButton radioButton;
panel.add(radioButton = new JRadioButton("Music1"));
radioButton.setActionCommand("Music1");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("Music2"));
radioButton.setActionCommand("Music2");
entreeGroup.add(radioButton);
panel.add(radioButton = new JRadioButton("No Music", true));
radioButton.setActionCommand("No Music");
entreeGroup.add(radioButton);

add(panel);
pack();

setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setResizable(false);
setSize(400, 400);

}

最佳答案

add(panel);
pack();
setTitle("Title");
JLabel background = new JLabel(new ImageIcon("background.png"));
add(background);

JFrame 的默认布局管理器是 BorderLayout。当您在没有指定约束的情况下添加组件时,该组件将添加到 CENTER。您不能将多个组件添加到一个位置。

相反,您需要使用不同的组件作为背景。然后将面板添加到此组件。查看Background Panel .然后代码会是这样的:

Background background = new BackgroundPanel(...);
background.add(panel);
add(background);
setResizable(false);
pack();
...

关于java - 设置背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16042328/

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