java - 使用 Swing 将背景图像添加到 JFrame

标签 java image swing background jframe

您好,我一直在遵循多个有关如何向 JFrame 添加背景图像的指南。然而,当运行我的程序时,背景结果只有一点背景图像显示在左下角和下侧。

这是我当前的代码:

private int width = 1280;
private int height = 720;

private String title = "Creation - " + Component.versionID + " Launcher";

private JPanel window = new JPanel();
private JFrame frame = new JFrame();
private JLabel name, version;


private JButton play, options, changelog, quit;
private Rectangle rPlay, rOptions, rChangelog, rQuit, rName, rVersion;
private int buttonWidth = 200;
private int buttonHeight = 80;

public Menu (Component component) {
    frame.setTitle(title);
    frame.setSize(new Dimension(width, height));
    frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
    frame.add(component);
    frame.getContentPane().add(window);
    frame.setResizable(false);
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
    frame.setIconImage(new ImageIcon("res/icon.png").getImage());

    setLayout(new BorderLayout());
    JLabel background = new JLabel(new ImageIcon("res/background.png"));
    frame.add(background);
    background.setLayout(new FlowLayout());

    window.setLayout(null);
    drawButtons();
    frame.repaint();
    frame.pack();
}

private void drawButtons() {

    name = new JLabel("Creation");
    rName = new Rectangle((width/2) - (buttonWidth/2) + 60, 20, buttonWidth, buttonHeight);
    name.setBounds(rName);
    window.add(name);

    play = new JButton("Play");
    rPlay = new Rectangle((width/2) - (buttonWidth/2), 250, buttonWidth, buttonHeight);
    play.setBounds(rPlay);
    window.add(play);

    options = new JButton("Options");
    rOptions = new Rectangle((width/2) - (buttonWidth/2), 350, buttonWidth, buttonHeight);
    options.setBounds(rOptions);
    window.add(options);

    changelog = new JButton("View change-log");
    rChangelog = new Rectangle((width/2) - (buttonWidth/2), 450, buttonWidth, buttonHeight);
    changelog.setBounds(rChangelog);
    window.add(changelog);

    quit = new JButton("Quit");
    rQuit = new Rectangle((width/2) - (buttonWidth/2), 550, buttonWidth, buttonHeight);
    quit.setBounds(rQuit);
    window.add(quit);

    play.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Component component = new Component();
            frame.dispose();
            component.start();
        }
    });
    options.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    changelog.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {

        }
    });
    quit.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {        
            System.exit(0);
        }
    });
}

(如您所见)我的 JFrame 大小是 1280x720。这与背景图像的大小相同,所以我没有看到任何问题。如果有人可以帮助我编辑代码或给我指点,我只是在寻找显示在按钮后面的背景图像。

最佳答案

  1. 如果您的 GUI 将是图像的大小,则将 BufferedImage 添加到 JLabel,并将 JLabel 添加到 JFrame 的 contentPane,而不是某些不显示图像的 JPanel。
  2. 不要使用 null 布局和 setBounds(...),而是使用合适的布局管理器。虽然空布局对于新手来说似乎更容易,但这通常只是因为您可能不熟悉如何使用布局管理器,相信我,创建和维护 GUI 更容易使用行为良好的布局管理器。
  3. 为 JPanel/contentPane 提供一个合适的布局,因为您将向其中添加组件。

关于java - 使用 Swing 将背景图像添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16329706/

相关文章:

Java Regex - 非法重复字符

java - 继承,从子类调用方法

html - 悬停图像时使图像从 overflow hidden 到可见

html - 如何使图像更改以显示它正在被按下?

java - 如何使用带有 CachedRowSet 的 fireTableRowsInserted 在 JTable 中插入新行?

java - 如何在关闭应用程序之前停止纹理 View 监听器并显示图像

java - 如何在任何在线服务器上部署 Spring 框架后端和 Angular 2 前端应用程序?

wpf - WPF-如何使用模板创建图像按钮

java - 如何在 JRadioButtons 的 ButtonGroup 中随机选择一个按钮?

java重绘多个区域