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();
}
}
我看到按钮放在一行中。需要按顺序依次放置按钮BoxLayout.Y_AXIS
。当我删除 //line 4 时,它会根据 FlowLayout 正确创建。