Question

import java.awt.Color;
import java.awt.Dimension;

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

public class Ships {

    public static JPanel init(JPanel radioPanel){
        radioPanel.add(addShips(2));
        radioPanel.add(addShips(3));
        radioPanel.add(addShips(4));
        radioPanel.add(addShips(5));

        return radioPanel;
    }

    public static JButton addShips(int size){
        JButton but = new JButton();
        but.setPreferredSize(new Dimension((40*size),40));
        but.setBackground(Color.BLACK);
        return but;
    }

    public static void main(String[] args){
        JFrame frame = new JFrame();
        frame.setVisible(true);
        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); \\line 4
        init(radioPanel);
        frame.getContentPane().add(radioPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
    }

}

I see that the buttons are placed in one single line. Need to place the buttons one after the other according to BoxLayout.Y_AXIS. When I remove //line 4, it creates correctly according to FlowLayout.

Was it helpful?

Solution

Try this:

import java.awt.Color;
import java.awt.Dimension;

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

public class Ships {

    public static JPanel init(JPanel radioPanel){
        radioPanel.add(addShips(2));
        radioPanel.add(addShips(3));
        radioPanel.add(addShips(4));
        radioPanel.add(addShips(5));

        return radioPanel;
    }

    public static JButton addShips(int size){
        JButton but = new JButton();
        Dimension d = new Dimension((40*size),40);
        but.setPreferredSize(d);
        but.setMinimumSize(d);
        but.setMaximumSize(d);
        but.setBackground(Color.BLACK);
        return but;
    }

    public static void main(String[] args){
        JFrame frame = new JFrame();
        JPanel radioPanel = new JPanel();
        radioPanel.setLayout(new BoxLayout(radioPanel, BoxLayout.Y_AXIS)); //line 4
        init(radioPanel);
        frame.getContentPane().add(radioPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}

When you pack components in JFrame, your layout manager may not honour preferred size of components, try setting minimum and maximum sizes too.

In vertical layout (y-axis), BoxLayout tries to make all components wide as widest component. As there is no text or icon in all buttons, button will shrink to default size, and all will have same width. So instruct box layout for particular sizes using maximum and minimum sizes.

OTHER TIPS

I have changed your LayoutManager to GridBagLayout and it works fine. Is it suitable for you? :

public class Ships {

public static JPanel init(JPanel radioPanel){
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    radioPanel.add(addShips(2),c);
    c.gridy = 1;
    radioPanel.add(addShips(6),c);
    c.gridy = 2;
    radioPanel.add(addShips(4),c);
    c.gridy = 3;
    radioPanel.add(addShips(5),c);

    return radioPanel;
}

public static JButton addShips(int size){
    JButton but = new JButton();
    but.setPreferredSize(new Dimension((40*size),40));
    but.setBackground(Color.BLACK);
    return but;
}

public static void main(String[] args){
    JFrame frame = new JFrame();
    frame.setVisible(true);
    JPanel radioPanel = new JPanel();
    radioPanel.setLayout(new GridBagLayout()); 
    init(radioPanel);
    frame.getContentPane().add(radioPanel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
}

}

EDIT: change horizontal to vertical align. Result:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top