Note that there are some explanatory texts on larger screens.

plurals
  1. POGridBagLayout is not displaying as expected
    primarykey
    data
    text
    <p>This is my first foray into GridBagLayout, have reviewed the Java documentation online (as well as a lot of SO Q&amp;As) and I've creating a panel that I'm expecting to display as such:</p> <pre><code>+--------------------------+ | choose a timer | +--------+--------+--------+ | 5min | 25min | 30min | +--------+--------+--------+ | 00:00:00 | +--------+--------+--------+ | Start | Pause | Quit | +--------+--------+--------+ </code></pre> <p>and here's the GridBagLayout I'm using.</p> <pre><code>public class ButtonSel extends JFrame implements ActionListener { JLabel buttonSelLabel = new JLabel("Choose Which Timer To Run"); JButton pomoButton = new JButton("00:25:00"); JButton shrtButton = new JButton("00:05:00"); JButton longButton = new JButton("00:30:00"); JButton startButton = new JButton("Go"); JButton pauseButton = new JButton("Pause"); JButton quitButton = new JButton("Quit"); JLabel textDisplay = new JLabel("00:00:00"); // JPanel timerPanel = new JPanel(); // public ButtonSel() { super("ButtonTest"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); add(buttonSelLabel, c); // line 1 c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 1; c.gridy = 1; c.gridwidth = 3; add(pomoButton, c); // line 2 c.gridx = 1; c.gridy = 2; c.gridwidth = 1; add(shrtButton, c); c.gridx = 2; c.gridy = 2; c.gridwidth = 1; add(longButton, c); c.gridx = 3; c.gridy = 2; c.gridwidth = 1; add(textDisplay, c); // line 3 c.fill = GridBagConstraints.HORIZONTAL; c.gridx=1; c.gridy=3; c.gridwidth=3; add(startButton, c); // line 4 c.gridx = 1; c.gridy = 4; c.gridwidth = 1; add(pauseButton, c); c.gridx = 2; c.gridy = 4; c.gridwidth = 1; add(quitButton, c); c.gridx = 2; c.gridy = 4; c.gridwidth = 1; pomoButton.addActionListener(this); shrtButton.addActionListener(this); longButton.addActionListener(this); startButton.addActionListener(this); pauseButton.addActionListener(this); quitButton.addActionListener(this); } public void actionPerformed(ActionEvent radioSelect) { Object source = radioSelect.getSource(); if (source == pomoButton) textDisplay.setText("00:25:00"); else if (source == shrtButton) textDisplay.setText("00:05:00"); else if (source == longButton) textDisplay.setText("00:30:00"); else if (source == startButton) textDisplay.setText("Started"); else if (source == pauseButton) textDisplay.setText("Paused"); else if (source == quitButton) textDisplay.setText("Quit"); else textDisplay.setText("00:00:00"); } } </code></pre> <p>I was getting this output, </p> <p><img src="https://i.stack.imgur.com/AZMz7.jpg" alt="enter image description here"></p> <p>and taking a cue about horizontal alignment from <a href="https://stackoverflow.com/questions/10163501/laying-out-components-in-gridbaglayout-not-as-expected">this question</a> I added the <code>HORIZONTAL</code> constraints to the the fields. Now I'm getting:</p> <p><img src="https://i.stack.imgur.com/MSN1k.jpg" alt="enter image description here"></p> <p>I've used <code>(0,0)</code> and <code>(1,1)</code> as my starting <code>(x,y)</code> coordinates, interestingly enough, with the same results. </p> <p>Can anyone see something that I'm missing? </p>
    singulars
    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.
 

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