Note that there are some explanatory texts on larger screens.

plurals
  1. POMaking GridBagLayout looks like in pattern
    primarykey
    data
    text
    <p>I want to do something like this:</p> <p><img src="https://i.imgur.com/4Vty3CI.jpg" alt="pattern"></p> <p>Here is my code:</p> <pre><code>public class MainFrame extends JFrame{ public MainFrame(){ super("Symulator Lotow"); JPanel returnPanel = new JPanel(new GridBagLayout()); add(returnPanel); Vector&lt;JButton&gt; v = new Vector&lt;JButton&gt;(); int licznik = 0; GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.BOTH; c.ipadx = 0; c.ipady = 0; c.insets = new Insets(0,0,0,0); c.weightx = 1.0; c.weighty = 1.0; c.anchor = GridBagConstraints.CENTER; v.add(new JButton()); c.gridwidth = 6; c.gridheight = 6; c.gridx = 0; c.gridy = 0; v.get(licznik).setBackground(Color.black); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 6; c.gridheight = 4; c.gridx = 6; c.gridy = 0; v.get(licznik).setBackground(Color.white); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 3; c.gridheight = 2; c.gridx = 6; c.gridy = 4; v.get(licznik).setBackground(Color.blue); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 3; c.gridheight = 2; c.gridx = 9; c.gridy = 4; v.get(licznik).setBackground(Color.yellow); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 6; c.gridheight = 2; c.gridx = 0; c.gridy = 6; v.get(licznik).setBackground(Color.yellow); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 6; c.gridheight = 2; c.gridx = 6; c.gridy = 6; v.get(licznik).setBackground(Color.yellow); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 4; c.gridheight = 2; c.gridx = 0; c.gridy = 8; v.get(licznik).setBackground(Color.red); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 4; c.gridheight = 2; c.gridx = 4; c.gridy = 8; v.get(licznik).setBackground(Color.red); returnPanel.add(v.get(licznik),c); licznik++; v.add(new JButton()); c.gridwidth = 4; c.gridheight = 2; c.gridx = 8; c.gridy = 8; v.get(licznik).setBackground(Color.red); returnPanel.add(v.get(licznik),c); licznik++; this.setExtendedState(JFrame.MAXIMIZED_BOTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(true); } public static void main(String[] args) { // TODO Auto-generated method stub new MainFrame(); } } </code></pre> <p>As can be seen when my code is run, it doesn't look like in the pattern. The colors are just samples, so I don't care about them. I just want the relative sizes of buttons to be correct. Where is my mistake?</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.
    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