0

I want to create a window where there is a background image and components on top of that. I have managed to get the components and background 'stacked', but my problem is positioning the components.

I have tried using AbsoluteLayout but it doesn't seem to work.

This is my (beginner) code thus far:

public class RegionPrompt extends JPanel {

    public RegionPrompt () throws IOException {
        JFrame frame = new JFrame("Map");
        GridBagConstraints gbc = new GridBagConstraints();
        JPanel pane = new JPanel() { 
            URL image2 = getClass().getResource("map.jpg");
            BufferedImage image = ImageIO.read(image2);
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), this);    
            }
        };

        frame.setContentPane(pane);
        JPanel pane2 = new JPanel();
        pane2.setOpaque(false);
        pane2.setSize(DefaultGUI.defaultSize);
        pane2.setLayout(new GridBagLayout());

        JButton c = new JButton("Map Location");
        //gbc.gridx = 0;
        //gbc.gridy = 0;
        c.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {
               JOptionPane.showMessageDialog(null, "Location information", JOptionPane.INFORMATION_MESSAGE);
           }
        });
        pane2.add(c,gbc);

        frame.add(pane2);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(653, 448);
        frame.setVisible(true);
    }
}

Any help (as well as forgiveness for this code chunk) is greatly appreciated!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Dennis J.
  • 11
  • 4
  • You're going to have a hard time, because a lot of information goes into deciding how to layout a component and this information can change between platforms. In your case, I'd almost argue that it would be easier to generate a custom layout which you could use to support your needs – MadProgrammer Feb 14 '16 at 07:45
  • For [example](http://stackoverflow.com/questions/14290020/dynamically-growing-jpanel-with-boxlayout-on-a-null-layout/14290582#14290582), [example](http://stackoverflow.com/questions/11819669/absolute-positioning-graphic-jpanel-inside-jframe-blocked-by-blank-sections/11822601#11822601) and [example](http://stackoverflow.com/questions/15347293/making-a-button-java/15347407#15347407) – MadProgrammer Feb 14 '16 at 08:00
  • Thank you very much, I will go and look it up then. – Dennis J. Feb 14 '16 at 08:30
  • 1
    Provide ASCII art or a simple drawing of the *intended* layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Feb 14 '16 at 13:13

0 Answers0