Hello, I have two problems. First, I am trying to have a set background image that displays with two jbuttons on the bottom. My error is that when the back ground image is displayed it hides the buttons (until you mouse over them). Second, I want the buttons on the bottom of the frame. Thanks for any help.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;

public class Greeting extends JFrame {

    private JButton   startButton;
    private JButton   exitButton;
    private Image     background;

    public JButton getStartButton() { return startButton; }
    public JButton getExitButton() { return exitButton; }


    public Greeting(String title) {
         super(title);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setSize(800,600);
         show();



         background = Toolkit.getDefaultToolkit().getImage("island.jpg");


        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints layoutConstraints = new GridBagConstraints();
        setLayout(layout);



        startButton = new JButton("Start");
        layoutConstraints.gridx = 3;
        layoutConstraints.gridy = 0;
        layoutConstraints.gridwidth = 1;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(10, 10, 10, 10);
        layoutConstraints.anchor = GridBagConstraints.PAGE_END;
        layoutConstraints.weightx = 0.0;
        layoutConstraints.weighty = 0.0;
        layout.setConstraints(startButton, layoutConstraints);
        add(startButton);


        exitButton = new JButton("Exit");
        layoutConstraints.gridx = 5;
        layoutConstraints.gridy = 2;
        layoutConstraints.gridwidth = 1;
        layoutConstraints.gridheight = 1;
        layoutConstraints.fill = GridBagConstraints.BOTH;
        layoutConstraints.insets = new Insets(10, 10, 10, 10);
        layoutConstraints.anchor = GridBagConstraints.LAST_LINE_END;
        layoutConstraints.weightx = 0.0;
        layoutConstraints.weighty = 0.0;
        layout.setConstraints(exitButton, layoutConstraints);
        add(exitButton);


    }
    public void paint(Graphics g) {
        g.drawImage(background, 0, 0, null);}

    public static void main(String args[]) {
        Greeting frame =  new Greeting("Greeting");
        frame.setVisible(true);
    }
}

Recommended Answers

All 2 Replies

Line 63: overrided paintComponent, not paint
JFrame's paint method also paints the buttons etc, so by overriding it you mess that up. paintComponent is the JFrame method that paints the content area of the frame before the buttons etc are painted over it, so that's the one you need to override.

For the GridBagConstraints...
you have fill = BOTH - this makes the buttons as big as the available space. NONE would be a better choice as each button will just be its own natural size

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.