簡體   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