繁体   English   中英

设置背景图像

[英]Setting background image

我怎么能摆脱那个灰盒子?

这就是我所说的:

如果你能帮帮我,我真的很感激

完整代码: 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 background = new BackgroundPanel(...);
background.add(panel);
add(background);
setResizable(false);
pack();
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM