Question

I know this is horrible coding but I desperately need to fix this problem. I've tried multiple ways of trying to position the button but the button still stays in the top center with all the other buttons lined up after it.

import javax.imageio.ImageIO;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.io.IOException;
import java.net.URL;

public class Template extends JFrame {
/**
* @param args
*/

public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
    JFrame frame = new JFrame("The Impossible Quiz");//Construct JFrame
    frame.setLayout(null);//manual setting for button placement
    frame.setContentPane(new JPanel() {//sets panel as background
        BufferedImage image = ImageIO.read(new URL("https://pbs.twimg.com/media/BoyFVfXIUAA0Tik.png"));//uses url image 
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, 1360, 690, this);//sets image as jframe background
        }
    });


    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//closes jframe when press exit
    frame.setSize(1365, 730);//sets size of jframe

    JPanel buttonpanel = new JPanel();//sets panel for all buttons
    buttonpanel.setBounds(0, 0, 1460, 690);//sets placing and sizing of panel
    buttonpanel.setOpaque(false);//makes panel transparent

    JButton next = new JButton ("Start");//contructs correct start button
    next.setBounds(10, 5, 40, 50);
    buttonpanel.add(next);//adds button to panel

    next.addActionListener(new ActionListener()//adds actionlistener to button
    {
        public void actionPerformed(ActionEvent e)
        {
            new Introduction();//continues to next question
        }
    });
    JButton wrongstart = new JButton("Start!!");//constructs wrong start button
    wrongstart.setSize(100, 400);//setes size of button
    buttonpanel.add(wrongstart);//adds button to panel
    wrongstart.addActionListener(new ActionListener()//adds actionlistener to button
    {
        public void actionPerformed(ActionEvent e)
        {
            new Wrong();//direct user to wrong panel

        }
    });

    frame.add(buttonpanel);//adds panel to jframe
    frame.setVisible(true);//sets jframe as visible
}

}
Was it helpful?

Solution

Your problem is that you're trying to use absolute positioning to position a component (your JButton) in a container (the containing JPanel) that uses FlowLayout as default, and FlowLayout completely ignores the components bounds. A quick solution is to set the JPanel's layout to null allowing for absolute positioning. A correct solution is to always avoid null layouts and absolute positioning and instead to nest JPanels, each using its own layouts, in order to create complex yet flexible and pleasing GUI's.


You're setting the JFrame contentPane's layout to null as well -- don't do that either.
And then adding a JPanel as the contentPane which uses a default FlowLayout -- don't do that. Let the contentPane's layout be BorderLayout.


Edit
For example, if we leave the contentPane alone with its BorderLayout, and add another image panel on top of it, one that uses GridBagLayout, we can easily position our JButton to the top left corner of the GUI if desired. ....

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.image.BufferedImage;

import javax.swing.*;

@SuppressWarnings("serial")
public class Template2 extends JPanel {
   private static final int PREF_W = 1460;
   private static final int PREF_H = 690;
   private BufferedImage img;
   private JButton startButton = new JButton("Start");

   public Template2() {
      setLayout(new GridBagLayout());

      // TODO: .... read in your image here

      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 1;
      gbc.gridy = 1;
      gbc.gridwidth = 1;
      gbc.gridheight = 1;
      gbc.insets = new Insets(5, 10, 0, 0);
      gbc.anchor = GridBagConstraints.NORTHWEST;
      gbc.fill = GridBagConstraints.NONE;
      gbc.weightx = 1.0;
      gbc.weighty = 1.0;
      add(startButton, gbc);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   @Override
   public void paintComponents(Graphics g) {
      super.paintComponents(g);
      if (img != null) {
         g.drawImage(img, 0, 0, this);
      }
   }

   private static void createAndShowGui() {
      Template2 mainPanel = new Template2();

      JFrame frame = new JFrame("Some Horrendous Program");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top