Search code examples
javaswingbackgroundjframepaintcomponent

Backgroundproblem in Swing, without Netbeans


I am a total Beginner and while I had most of the Application right away. I can t make a background picture for my swing gui. I ve read that you should do it with the overriding the paint methode, which I did. But somewhere I am making a mistake. Since nothing chances, except that the Button is invisible until I go over it with the Mouse. I tried several things, maybe one of you can see the Problem and help me out? Thanks a lot :)

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

/**
 *
 * @author Shore
 */
public class GUI extends JFrame implements ActionListener  {
    Container c;
    JButton überprüfungsButton = new JButton();
    JTextField eingabeTextField = new JTextField();
    JTextArea ausgabeTextFeld = new JTextArea();
    Image hintergrundBild;



    public GUI(){
        this.hintergrundBild = Toolkit.getDefaultToolkit().getImage( "Bild2.jpg" );
        c = getContentPane();   
        c.setLayout(new BorderLayout());
        c.setBackground(Color.LIGHT_GRAY);
        überprüfungsButton = new JButton("Test");
        überprüfungsButton.addActionListener(this);
        c.add(überprüfungsButton, BorderLayout.CENTER);
        eingabeTextField = new JTextField(40);
        c.add(eingabeTextField, BorderLayout.PAGE_START);
        eingabeTextField.setOpaque(false);
        ausgabeTextFeld = new JTextArea(30,30);
        c.add(ausgabeTextFeld, BorderLayout.PAGE_END);
        ausgabeTextFeld.setOpaque(false);



    }
    public static void main(String[] args) {
          GUI fenster = new GUI();
          fenster.setTitle("Für");
          fenster.setSize(800, 800);
          fenster.setVisible(true);
          fenster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

      protected void paintComponent(Graphics g) {

                if (hintergrundBild != null) {
                    g.drawImage(hintergrundBild, 0, 0, getWidth(), getHeight(), null);
                }
      }


    @Override
    public void actionPerformed(ActionEvent e) {
       if(e.getSource() == überprüfungsButton){

Thanks whoever took the Time to take a look at it.

Update: I actually can resolve the Problem with Netbeans and the swing-GUI Creator. However, I am still very curious! If you could still help me, I would appreciate it.


Solution

  • JFrame does not override paintComponent so Swing will not invoke it.

    Adding the @Override annotation above the method will show that the method is not contained in the super class. The simplest way to implement a background image is to create a subclass of JPanel and override paintComponent there.

    Update:

    There are many examples on the web where the class JFrame is extended. From a design point is view this is not necessary as you typically do not any new functionality to the frame. For this reason just creating a direct instance & using is better (shown):

    public class BackGroundProblem {
        public static void main(String[] args) throws IOException {
    
            final Image image = ImageIO.read(new URL("http://news.beloblog.com/ProJo_Blogs/architecturehereandthere/hallstattsumm.jpg"));
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    JFrame frame = new JFrame();
                    JPanel backgroundPanel = new ImagePanel(image);
                    backgroundPanel.add(new JButton("Sample Button 1"));
                    backgroundPanel.add(new JButton("Sample Button 2"));
                    frame.add(backgroundPanel);
                    frame.setLocationByPlatform(true);
                    frame.pack();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }
            });
        }
    }
    
    class ImagePanel extends JPanel {
        private static final long serialVersionUID = 1L;
        private Image image;
    
        ImagePanel(Image image) {
            this.image = image;
        };
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
        }
    
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(800, 600);
        }
    }