-1

I have made a game which randomizes the position (using setBounds) of a panel (a),

But whenever I add the panel (a) on another panel (b) it stays on the top of the panel (b).

I need it to be put on another panel(b) because that panel(b) is on a Gridlayout in order for Cardlayout to be used properly.

If it's not possible to be randomized with setBounds. I am highly open to suggestions.

public class DisATrial extends JFrame{
private JLabel    score,time,title;
private JButton   gameButton;
private JPanel    panel ,gameUi //panel(a),panel(b)
,scoreBoard,countdown, gamePanel, gameText;

Container c;
CardLayout cl;
BufferedWriter writer,writer2;
Random r;
int xVal=0,yVal=0;
String[] comment = {"Over Here","Here bruh","Click me","Heyyyy","Sup Scrub","Too ez","Can't catch me"};
public DisATrial(){
    super("Click Me");
    c = getContentPane();
    cl = new CardLayout();
    c.setLayout(cl);
    GridBagConstraints gb= new GridBagConstraints();

    r=new Random();
    xVal=r.nextInt(750)+5; //random x coordinate
    yVal=r.nextInt(440)+30; //random y coordinate

    /*Master Panel-----------------------------------------------------------------------------------------------------------------------*/
    gamePanel=new JPanel(new GridBagLayout());
    /*coutdown-------------------------------------------------------------------------------------------------------------------------*/

    countdown=new JPanel();
    time=new JLabel("Time: ");
    time.setForeground(Color.WHITE);
    countdown.add(time,BorderLayout.CENTER);
    countdown.setBackground(Color.BLACK);
    gb.ipadx=132;
    gb.ipady=40;
    gb.gridx=0;
    gb.gridy=0;
    gb.anchor = GridBagConstraints.FIRST_LINE_START;
    gamePanel.add(countdown,gb);

    /*Game text-------------------------------------------------------------------------------------------------------------------------*/

    gameText=new JPanel();
    title=new JLabel("Mode : ");
    title.setForeground(Color.WHITE);
    gameText.add(title,BorderLayout.CENTER);
    gameText.setBackground(Color.LIGHT_GRAY);
    gb.ipadx=528;
    gb.ipady=40;
    gb.gridx=1;
    gb.gridy=0;
    gb.anchor = GridBagConstraints.PAGE_START;
    gamePanel.add(gameText,gb);

    /*Scoreboard-----------------------------------------------------------------------------------------------------------------------*/

    scoreBoard=new JPanel();
    score=new JLabel("Score: ");
    scoreBoard.add(score,BorderLayout.WEST);
    scoreBoard.setBackground(Color.decode("#1c1c1c"));
    gb.ipadx=132;
    gb.ipady=40;
    gb.gridx=2;
    gb.gridy=0;
    gb.anchor = GridBagConstraints.FIRST_LINE_END;
    gamePanel.add(scoreBoard,gb);


    /*Game Button -------------------------------------------------------------------------------------------------------------------------*/

    panel=new JPanel(); //panel(a)
    gameButton=new JButton(comment[r.nextInt(7)]);
    panel.setLocation(xVal,yVal);
    panel.setSize(120,40);
    panel.add(gameButton);
    panel.setBackground(Color.GRAY);

    /*Game UI -------------------------------------------------------------------------------------------------------------------------*/

    gameUi=new JPanel(); //panel(b)
    gameUi.add(panel);
    gameUi.setBackground(Color.DARK_GRAY);
    gb.ipadx=840;
    gb.ipady=520;
    gb.gridwidth=4;
    gb.gridx=0;
    gb.gridy=1;
    gamePanel.add(gameUi,gb);        

    /*Frame Properties---------------------------------------------------------------------------------------------------------------------*/

    c.add(gamePanel,"game");

    setVisible(true);
    setSize(960,640);
    cl.show(c,"game");
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}

 public static void main(String[] args){
    DisATrial app= new DisATrial();
}
  • 1
    *"I am highly open to suggestions."* Use custom painting. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). Be kind to your pets. – Andrew Thompson Mar 03 '17 at 12:48
  • `private JPanel panel, scoreBoard, countdown, gamePanel, gameUi, gameText;` Which of those 6 panels is supposed to be randomly positioned? Note that a question about randomly positioning one panel in another requires only 2 panels. – Andrew Thompson Mar 03 '17 at 13:54

1 Answers1

-1

You could give null as layout to gameUi, as by default it gets a FlowLayout that packs your button to the upper left.

Laurent G
  • 397
  • 8
  • 16
  • I can't. it needs to be on a `GridBagLayout` so that the positioning won't get crazy – Arnin Mendoza Mar 03 '17 at 14:35
  • The GridBagLayout is managing the gamePanel, not the gameUi. By your current code, a FlowLayout is managing the gameUi. – Laurent G Mar 03 '17 at 14:40
  • Ohhh Thanks!! Kinda new to these things so forgive my lack of knowledge – Arnin Mendoza Mar 03 '17 at 15:48
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Mar 03 '17 at 17:34