Java 如何在另一个JPanel中添加带有随机坐标的JPanel?

Java 如何在另一个JPanel中添加带有随机坐标的JPanel?,java,swing,jpanel,Java,Swing,Jpanel,我做了一个随机位置的游戏 (使用面板(a)的setBounds) 但每当我在另一个面板(b)上添加面板(a)时,它都会停留在面板(b)的顶部 我需要将其放在另一个面板(b)上,因为该面板(b)位于Gridlayout上,以便正确使用Cardlayout 如果不能随机使用setBounds。我非常愿意听取建议 public class DisATrial extends JFrame{ private JLabel score,time,title; private JButton ga

我做了一个随机位置的游戏 (使用面板(a)的
setBounds

但每当我在另一个面板(b)上添加面板(a)时,它都会停留在面板(b)的顶部

我需要将其放在另一个面板(b)上,因为该面板(b)位于
Gridlayout
上,以便正确使用
Cardlayout

如果不能随机使用
setBounds
。我非常愿意听取建议

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();
}

您可以将
null
作为布局提供给gameUi,因为默认情况下,它会将您的按钮打包到左上角。

“我非常愿意接受建议。”使用自定义绘制。为了更快地获得更好的帮助,请发布一个or。善待你的宠物。
私人JPanel面板、记分板、倒计时、游戏面板、游戏界面、游戏文本这6个面板中的哪一个应该随机放置?请注意,关于随机将一个面板放置在另一个面板中的问题只需要两个面板,我不能。它需要位于
GridBagLayout
上,以便定位不会变得疯狂GridBagLayout管理的是游戏面板,而不是游戏UI。根据您当前的代码,FlowLayout正在管理gameUi。噢,谢谢!!对这些东西有点陌生,所以请原谅我缺乏知识,Java GUI必须在不同的操作系统、屏幕大小、屏幕分辨率等上工作,在不同的地区使用不同的PLAF。因此,它们不利于像素完美布局。而是使用布局管理器,或与布局填充和边框一起使用。