0

I'm trying to get some of the buttons to be bigger and be able to move them around to get them more to what my professor wants them to be but I'm not sure how to do it.

I decided to use a GridBagLayout but my professor never talked about it so I'm not sure if I'm missing anything or how exactly it works.

The image is what he wants us to get it too. Exactly like this.

LayoutQuestion

import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class GUI {
    public static JPanel buttonPanel;

    private static JFrame frame;

    public static void main(String[] args) {
        frame = new JFrame("Layout Question");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500, 500);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new GridBagLayout());
        frame.add(mainPanel);
        
        buttonPanel = new JPanel();
        mainPanel.add(buttonPanel);
        buttonPanel.add(new JButton("hi"));
        buttonPanel.add(new JButton("long name"));
        buttonPanel.add(new JButton("bye"));
        buttonPanel.add(new JButton("1"));
        buttonPanel.add(new JButton("2"));
        buttonPanel.add(new JButton("3"));
        buttonPanel.add(new JButton("4"));
        buttonPanel.add(new JButton("5"));
        buttonPanel.add(new JButton("6"));
        buttonPanel.add(new JButton("7"));
        buttonPanel.add(new JButton("Cancel"));
    }

}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    The image is what he wants us to get it too. Exactly like that. – ScreamQueenDG44 Apr 30 '21 at 18:47
  • *"to get them more to what my professor wants them to be"* Where does your professor expects the buttons to be? Please provide an image of that – Frakcool Apr 30 '21 at 18:47
  • @RyanLilitu Which layout managers do you know or has been taught by the professor? – Progman Apr 30 '21 at 18:48
  • @Frakcool Does the image not show up? I did add one for what he wants it to look like – ScreamQueenDG44 Apr 30 '21 at 18:49
  • I'm looking at the image and compiling your code – Frakcool Apr 30 '21 at 18:51
  • @Progman he's showed us FlowLayout, GridLayout, and BorderLayout, he taught this to us about 2ish months ago? – ScreamQueenDG44 Apr 30 '21 at 18:51
  • @RyanLilitu You can use multiple layout managers for parts of the GUI and combine them together (using `JPanel`s). – Progman Apr 30 '21 at 18:53
  • I would just nest some JPanels with different layouts and look what's coming out of it. Maybe it works, maybe it doesn't. – JCWasmx86 Apr 30 '21 at 18:53
  • 1
    [A Visual Guide to Layout Managers](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) is a good place to start to understand layout managers. You can also google the documentation and tutorials for each specific one. As others have said, you often need to use multiple JPanels, each with its own layout manager, nested together, to get the look you want. – Code-Apprentice Apr 30 '21 at 18:55
  • @Progman I'm sorry what? you can do that? how? do you have a link or anything that can help show that? – ScreamQueenDG44 Apr 30 '21 at 18:57

1 Answers1

3

There are some improvements you can do to your code:

  1. Don't use static variables, and place your program on the EDT, an easy way to do this is:

     public static void main(String[] args) {
         SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
     }
    
  2. Don't call setSize(...) on your JFrame, it's going to make your window smaller than you think, it's taking the frame decorations into the calculation for the size, instead call frame.pack(), or override the getPreferredSize() of your JPanel, see Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? for a more in-depth explanation.

  3. Don't call frame.setVisible(true) before you've added all your components to your JFrame, otherwise you'll get strange bugs related to invisible components, that line should be the last one on your code.

  4. Divide and conquer, you can use multiple JPanels with different Layout Managers, and you can combine them and join them together later on.

One possible approach (which isn't exactly as your teacher wants it to be but is close enough) is this one:

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class LayoutManagersExample {
    private JFrame frame;
    
    private JPanel pane;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new LayoutManagersExample()::createAndShowGUI);
    }
    
    private void createAndShowGUI() {
        frame = new JFrame("Layout Question");
        
        pane = new JPanel();
        
        JPanel topPanel = getTopPanel();
        JPanel boxesPanel = getBoxesPanel();
        JPanel buttonsPanel = getButtonsPanel();
        
        pane.add(boxesPanel);
        pane.add(buttonsPanel);
        
        frame.add(pane);
        frame.add(topPanel, BorderLayout.NORTH);
        frame.add(new JButton("Cancel"), BorderLayout.SOUTH);
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.setSize(500, 500);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
    
    private JPanel getButtonsPanel() {
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridLayout(2, 2));
        
        buttonsPanel.add(new JButton("1"));
        buttonsPanel.add(new JButton("2"));
        buttonsPanel.add(getInnerButtonsPanel());
        buttonsPanel.add(new JButton("7"));
        
        return buttonsPanel;
    }
    
    private JPanel getInnerButtonsPanel() {
        JPanel innerButtonsPanel = new JPanel();
        innerButtonsPanel.setLayout(new GridLayout(2, 2));
        
        innerButtonsPanel.add(new JButton("3"));
        innerButtonsPanel.add(new JButton("4"));
        innerButtonsPanel.add(new JButton("5"));
        innerButtonsPanel.add(new JButton("6"));
        
        return innerButtonsPanel;
    }
    
    private JPanel getBoxesPanel() {
        JPanel boxesPanel = new JPanel();
        boxesPanel.setLayout(new BoxLayout(boxesPanel, BoxLayout.PAGE_AXIS));
        
        boxesPanel.add(new JCheckBox("Bold"));
        boxesPanel.add(new JCheckBox("Italic"));
        boxesPanel.add(new JCheckBox("Underline"));
        boxesPanel.add(new JCheckBox("Strikeout"));
        
        return boxesPanel;
    }
    
    private JPanel getTopPanel() {
        JPanel topPanel = new JPanel();
        JPanel topButtonsPanel = new JPanel();
        
        topButtonsPanel.setLayout(new GridLayout());
        
        topButtonsPanel.add(new JButton("hi"));
        topButtonsPanel.add(new JButton("long name"));
        topButtonsPanel.add(new JButton("bye"));
        
        topPanel.add(new JLabel("Buttons: "));
        topPanel.add(topButtonsPanel);
        return topPanel;
    }
}

enter image description here

Play around with the code, and try to find a different approach by combining the layouts, divide each piece of the window in your head and see how to apply a different layout manager to each of them, divide them in methods as I did to make things easier to follow.

Find a way to left align the elements in the JCheckBoxes for example, and other things

Frakcool
  • 10,915
  • 9
  • 50
  • 89