【发布时间】:2015-05-16 02:00:06
【问题描述】:
我无法将图像设置为我的项目的背景 到目前为止,我已经在下面包含了我的代码,它只是我的主页,按钮工作(我自定义了我的按钮),我只是想要一些帮助。谢谢
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);
}
});
}
}
【问题讨论】:
-
null布局会给您带来更多问题,然后他们会解决 -
当我尝试将图像设置为背景时,我删除了空布局,但它仍然不起作用?
-
创建一个可以绘制图像的自定义面板(在副本中链接或已经回答),将此面板设置为框架的
contentPane。设置框架布局管理器并像往常一样与之交互 -
感谢您的帮助,这很有帮助并且有效