我正在尝试将图像背景设置在 3 个 JButton
后面。截至目前,它正在绘制背景,然后将按钮放置在没有背景的下面(拉伸(stretch)窗口以查看)。我如何使用JLayeredPane
将背景图像放在后面?
我的印象是 JLayeredPane 是最简单的方法。不过我可能是错的。
package main;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.*;
import javax.swing.*;
public class Galaga extends JFrame implements ActionListener, WindowListener{
private JButton startButton, highScoreButton, optionsButton;
public JPanel buttonPanel, optionsPanel, highScorePanel;
public Galaga() {
startButton = new JButton("Start Game");
highScoreButton = new JButton("High Scores");
optionsButton = new JButton("Options");
buttonPanel = new JPanel();
buttonPanel.add(new JLabel(new ImageIcon("images/Spacebackground.png")));
buttonPanel.setLayout(new FlowLayout());
buttonPanel.add(startButton);
buttonPanel.add(highScoreButton);
buttonPanel.add(optionsButton);
startButton.addActionListener(this);
highScoreButton.addActionListener(this);
optionsButton.addActionListener(this);
addWindowListener(this);
add(buttonPanel);
}
最佳答案
您只需在标签上设置布局管理器并向其中添加按钮即可...
JLabel label = new JLabel(new ImageIcon("images/Spacebackground.png"));
label.setLayout(new FlowLayout());
label.add(startButton);
label.add(highScoreButton);
label.add(optionsButton);
add(label);
关于java - 我将如何使用 Jlayered Pane 将按钮置于背景之上而不是背景之下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22822588/