Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is your code in a form that works. To make it work, you need to to the following things:</p> <ol> <li>all buttons need the same amount of weightx, to get the space evenly distributed between them</li> <li>the heading needs some weightx to get all the of its row</li> <li>use fill=BOTH to make all components fill the space they get</li> <li>use gridwidth to make the heading use three columns while the buttons only use one</li> <li>use the horizontal alignment of JLabel to make the heading centered within its space</li> </ol> <hr> <pre><code>public void initialize() { JButton[] moveChoices = new JButton[3]; JPanel buttonsPanel = new JPanel(); buttonsPanel.setBackground(Color.green); buttonsPanel.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); JLabel welcome = new JLabel(); c.fill=GridBagConstraints.BOTH; c.anchor=GridBagConstraints.CENTER; welcome.setText("Welcome to rock paper scissors! Please enter your move."); welcome.setHorizontalAlignment(SwingConstants.CENTER); c.weightx=1; c.gridy = 0; c.gridx = 0; c.insets = new Insets(10, 0, 0, 0); c.gridwidth=moveChoices.length; c.gridheight=1; buttonsPanel.add(welcome,c); c.insets=new Insets(0,0,0,0); c.gridwidth=1; for(int i = 0; i &lt; moveChoices.length; i++) { moveChoices[i] = new JButton(); c.gridx = i; c.gridy = 1; c.weightx=0.5; c.weighty=1; moveChoices[i].addActionListener(this); buttonsPanel.add(moveChoices[i], c); } moveChoices[0].setText("Rock"); moveChoices[1].setText("Paper"); moveChoices[2].setText("Scissors"); this.add(buttonsPanel); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload