2017-03-03 119 views
-1

我已經作出了遊戲,隨機化面板(A)的 (使用setBounds)的位置,如何在另一個JPanel中添加隨機座標的JPanel?

但每當I(A)的另一面板上添加圖(b)它停留在面板的頂部(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(); 
} 
+1

*「我非常樂於接受建議。」*使用自定義繪畫。爲了儘快提供更好的幫助,請發佈[MCVE]或[簡短,獨立,正確的示例](http://www.sscce.org/)。善待你的寵物。 –

+0

'private JPanel panel,scoreBoard,countdown,gamePanel,gameUi,gameText;'這6個面板中的哪一個應該被隨機定位?請注意,有關將一個面板隨機定位到另一個面板的問題僅需要2個面板。 –

回答

-1

你可以給null作爲佈局gameUi,因爲默認情況下它得到的FlowLayout,那包您的按鈕到左上角。

+0

我不行。它需要位於'GridBagLayout'上,這樣定位纔不會變得瘋狂 –

+0

GridBagLayout管理的是gamePanel,而不是gameUi。按照您當前的代碼,FlowLayout正在管理gameUi。 –

+0

哦,謝謝!有點新來這些東西,所以原諒我的知識缺乏 –

相關問題