1
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 正确创建。

4

2 回答 2

2

我已将您的 LayoutManager 更改为 GridBagLayout 并且它工作正常。它适合你吗?:

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();
}

}

编辑:将水平更改为垂直对齐。结果:

在此处输入图像描述

于 2013-10-31T08:07:58.630 回答
2

尝试这个:

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);
    }
}

当您在 中打包组件时JFrame,您的布局管理器可能不尊重组件的首选大小,请尝试设置最小和最大尺寸。

在垂直布局(y 轴)中,BoxLayout 尝试将所有组件设为最宽组件。由于所有按钮中都没有文本或图标,因此按钮将缩小到默认大小,并且所有按钮都将具有相同的宽度。因此,使用最大和最小尺寸指示特定尺寸的盒子布局。

于 2013-10-31T08:24:03.353 回答