我无法将图片设置为我的项目的背景 下面我已经包含了我的代码到目前为止,它只是我的主页,按钮工作(我自定义我的按钮),我只是想要一些帮助。感谢
public JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private ImageIcon tle = new ImageIcon("title1.gif");
private ImageIcon pbI = new ImageIcon("playbutton.gif");
private ImageIcon ibI = new ImageIcon("instructionbutton.gif");
private ImageIcon qbI = new ImageIcon("quitbutton.gif");
private JButton playButton;
private JButton instructionButton;
private JButton quitButton;
Timer t;
test2 newframe = new test2();
instrpage instrframe = new instrpage();
public static void main(String[] args) {
new test1();
}
public test1() {
mainGUI();
}
public void mainGUI() {
playButton = new JButton(pbI);
instructionButton = new JButton(ibI);
quitButton = new JButton(qbI);
mainFrame = new JFrame("My Major Project");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(800, 600);
mainFrame.setLocationRelativeTo(null);
mainFrame.setLayout(null);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new BorderLayout());
mainFrame.setContentPane(new JLabel(new ImageIcon("background.gif")));
mainFrame.setLayout(new FlowLayout());
controlPanel = new JPanel();
controlPanel.setLayout(null);
controlPanel.setSize(mainFrame.getWidth(), mainFrame.getHeight());
controlPanel.setLocation(0, 0);
headerLabel = new JLabel("");
headerLabel.setIcon(tle);
statusLabel = new JLabel("");
statusLabel.setSize(350, 100);
headerLabel.setSize(540, 100);
statusLabel.setLocation(100, 500);
headerLabel.setLocation(100, 500);
playButton.setSize(pbI.getIconWidth(), pbI.getIconHeight());
playButton.setLocation(mainFrame.getWidth() / 2 - pbI.getIconWidth()
/ 2, 150);
playButton.setVisible(true);
instructionButton.setSize(ibI.getIconWidth(), ibI.getIconHeight());
instructionButton.setLocation(
mainFrame.getWidth() / 2 - ibI.getIconWidth() / 2, 200);
instructionButton.setVisible(true);
quitButton.setSize(qbI.getIconWidth(), qbI.getIconHeight());
quitButton.setLocation(mainFrame.getWidth() / 2 - qbI.getIconWidth()
/ 2, 250);
quitButton.setVisible(true);
controlPanel.add(playButton);
controlPanel.add(instructionButton);
controlPanel.add(quitButton);
controlPanel.add(headerLabel);
controlPanel.add(statusLabel);
controlPanel.setVisible(true);
mainFrame.add(controlPanel);
mainFrame.setVisible(true);
showButtonDemo();
t = new Timer(5, this);
t.addActionListener(this);
t.start();
}
public void actionPerformed(ActionEvent e) {
controlPanel.setSize(mainFrame.getWidth(), mainFrame.getHeight());
controlPanel.setLocation(0, 0);
playButton.setSize(pbI.getIconWidth(), pbI.getIconHeight());
playButton.setLocation(controlPanel.getWidth() / 2 - pbI.getIconWidth()
/ 2, (int) (controlPanel.getHeight() / 2 - pbI.getIconHeight()
/ 2 - mainFrame.getHeight() * 0.1));
instructionButton.setSize(ibI.getIconWidth(), ibI.getIconHeight());
instructionButton.setLocation(
controlPanel.getWidth() / 2 - ibI.getIconWidth() / 2,
(int) (controlPanel.getHeight() / 2 - pbI.getIconHeight() / 2));
quitButton.setSize(qbI.getIconWidth(), qbI.getIconHeight());
quitButton.setLocation(controlPanel.getWidth() / 2 - qbI.getIconWidth()
/ 2, (int) (controlPanel.getHeight() / 2 - pbI.getIconHeight()
/ 2 + mainFrame.getHeight() * 0.1));
headerLabel.setLocation(mainFrame.getWidth() / 2 - tle.getIconWidth() /2,
mainFrame.getHeight() - (mainFrame.getHeight() - 75));
}
private void showButtonDemo() {
playButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.remove(controlPanel);
newframe.controlPanel.setSize(mainFrame.getWidth(),mainFrame.getHeight());
newframe.controlPanel.setLocation(0,0);
newframe.headerLabel.setLocation(mainFrame.getWidth() / 2 - 130,
mainFrame.getHeight() - (mainFrame.getHeight() - 75));
newframe.headerLabel.setSize(500,80);
mainFrame.add(newframe.controlPanel);
mainFrame.repaint();
}
});
instructionButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
mainFrame.remove(controlPanel);
instrframe.controlPanel.setSize(mainFrame.getWidth(),mainFrame.getHeight());
instrframe.controlPanel.setLocation(0,0);
instrframe.headerLabel.setLocation(mainFrame.getWidth() / 2 - 130,
mainFrame.getHeight() - (mainFrame.getHeight() - 75));
instrframe.headerLabel.setSize(500,80);
mainFrame.add(instrframe.controlPanel);
mainFrame.repaint();
}
});
quitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
}
}
答案 0 :(得分:0)
JComponent
的子类,覆盖paintComponent(Graphics g)
方法来绘制图像,然后将JFrame
的内容窗格设置为此子类。
class ImagePanel extends JComponent {
private Image bgImage;
public ImagePanel(Image image) {
this.bgImage= image;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(bgImage, 0, 0, this);
}
}
然后它可以用作:
BufferedImage image = ImageIO.load(...);
JFrame customFrame = new JFrame("Image panel");
customFrame.setContentPane(new ImagePanel(image ));
需要注意的一点是,此代码不会考虑调整图像大小以适应JFrame。