2017-03-03 5 views
-1

私は、(a)はパネルの(setBoundsを使用して)位置 をランダムにゲームを作っ別のJPanelにランダム化された座標を持つJPanelを追加するには?

しかし、私は別のパネル(b)の上(a)のパネルを追加するたびに、それはパネルの上にとどまるています(b)。

Cardlayoutを正しく使用するには、パネル(b)がGridlayoutにあるので、別のパネル(b)に置く必要があります。

setBoundsでランダム化できない場合は、私は非常に提案に開放されています。デフォルトでは、左上にあなたのボタンをパックFlowLayoutのを取得するよう

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

'プライベートJPanelパネル、scoreBoard、countdown、gamePanel、gameUi、gameText;'ランダムに配置されるはずの6枚のパネルのどれですか? 1つのパネルを別のパネルにランダムに配置することについての質問は、2つのパネルしか必要としないことに留意されたい。 –

答えて

-1

あなたは、gameUiにレイアウトとしてnullを与えることができます。

+0

できません。 'GridBagLayout'上に配置する必要がありますので、位置が狂ってしまうことはありません –

+0

GridBagLayoutはgameUiではなくgamePanelを管理しています。現在のコードでは、FlowLayoutがgameUiを管理しています。 –

+0

ああありがとう!!私の知識の不足を許してください。 –

関連する問題