简体   繁体   中英

Adding a background Image to JPanel

I get error's with any threads I could find about this issue. Pretty much I just was to add a background to my JPanel.

My code:

    package org.client;

import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * 
 * @author Ryan Taubert || Dev.Ryanj
 * @version 0.1
 * 
 */

public class Main extends JFrame {
    private static final long serialVersionUID = -7729008412395425144L;
    private BufferedImage img;

    private static double APP_VERSION = 0.1;
    private static String APP_NAME = "Launcher ~ Version: "+APP_VERSION;

    private JPanel jp;
    private JTextField jUsername;
    private JTextField jPassword;

    /**
     * Width/Height
     */
    private int x = 1280, y = 720;


    /**
     * Launch the Application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                System.out.println("System: [Starting Application: "+APP_NAME+"]");
                try {
                    Main frame = new Main();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Launch Panel
     */
    public Main() {
        setTitle("Ryan's JLauncher"+APP_NAME);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, x, y);
        jp = new JPanel();
         try {
              img = ImageIO.read(new File("C:\\Users\\Ryan T\\Desktop\\wNSE6p7.jpg"));
            } catch(IOException e) {
              e.printStackTrace();
            }
        //jp.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(jp);
        jp.setLayout(null);

        /**
         * ``TextFields``
         */
        //username
        jUsername = new JTextField(10);
        jUsername.setBounds(6, 30, 50, 20);
        jUsername.setOpaque(false);
        jUsername.setBorder(null);
        jp.add(jUsername);

        //password
        jPassword = new JTextField(10);
        jPassword.setBounds(6, 30, 50, 20);
        jPassword.setOpaque(false);
        jPassword.setBorder(null);
        jp.add(jPassword);

        /**
         * ``Labels``
         */
        //username
        JLabel username = new JLabel("Username");
        username.setBounds(15, 11, 50, 14);
        jp.add(username);

        //password
        JLabel password = new JLabel("Password");
        password.setBounds(15, 11, 50, 14);
        jp.add(password);


        /**
         * Log Button
         */
        JButton login = new JButton("Log In");
        login.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                handleStartClient();
            }
        });
        login.setBounds(1, 60, 400, 30);
        jp.add(login);

        /**
         * Create New Account Button
         */
        JButton create = new JButton("Create New Account");
        create.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                handleStartClient();
            }
        });
        create.setBounds(1, 120, 400, 30);
        jp.add(create);

    }

     protected void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
      }

    private void handleStartClient() {

    }

}

The paint method is from another thread here on stackoverflow & It doesn't not work. Am I doing it wrong or?

JFrame has no paintComponent method. You need to wrap it in a JPanel then add that panel to the JFrame

public class Main exends JFrame{
    MyPanel panel;
    private BufferedImage img;

    public Main(){
        try {
            img = ImageIO.read(new File("C:\\Users\\Ryan T\\Desktop\\wNSE6p7.jpg"));
        } catch(IOException e) {
            e.printStackTrace();
        }
        panel= new MyPanel();
        add(panel);
    }    

    private class MyPanel extends JPanel{
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }
    }
} 

paintComponent is a method from JComponent . JFrame is not a subtype of JComponent , it is a Container

Also, see JPanel javadoc and JFrame javadoc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM