如何在Y轴上创建随机大小的按钮

时间:2013-10-31 08:00:37

标签: java swing boxlayout

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一个接一个地放置按钮。当我删除//第4行时,它会根据FlowLayout正确创建。

2 个答案:

答案 0 :(得分: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();
}

}

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

enter image description here

答案 1 :(得分: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尝试将所有组件扩展为最宽的组件。由于所有按钮中都没有文本或图标,因此按钮将缩小为默认大小,并且所有按钮都将具有相同的宽度。因此,请使用最大和最小尺寸指示特定尺寸的框布局。