【问题标题】:setting an image as background for jframe [duplicate]将图像设置为jframe的背景[重复]
【发布时间】: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。设置框架布局管理器并像往常一样与之交互
  • 感谢您的帮助,这很有帮助并且有效

标签: java swing


【解决方案1】:

IMO,我们没有直接的方法来做到这一点。但是我们可以创建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。

【讨论】:

  • 不要忘记覆盖getPreferredSize 方法并返回(至少)图像的大小;)
猜你喜欢
  • 2015-07-04
  • 2010-11-07
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多