最近在做程序的时候,使用BoxLayout时遇到问题是:
将面板的布局管理器设成BoxLayout时,添加的组件宽度不一样时,不能左对齐。
这是程序中的语句:
friendShowArea.setLayout(new BoxLayout(getFriendShowArea(), BoxLayout.Y_AXIS));
我使用的是Eclipse3.2
将面板的布局管理器设成BoxLayout时,添加的组件宽度不一样时,不能左对齐。
这是程序中的语句:
friendShowArea.setLayout(new BoxLayout(getFriendShowArea(), BoxLayout.Y_AXIS));
我使用的是Eclipse3.2
现将每个组件添加到单独的FlowLayout的容器中,将FlowLayout的容器设为左对齐
设置每个FlowLayout的容器的PreferredSize的宽度为BoxLayout容器的宽度
然后添加这些FlowLayout的容器到BoxLayout容器中
为什么不用null布局??
import java.awt.*;
public class Panel1 {
public static void main(String[] args) {
Panel1 gui = new Panel1();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBackground(Color.DARK_GRAY);
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
JButton button = new JButton("yyc");
JButton button1 = new JButton("chunyou128");
panel.add(button);
panel.add(button1);
frame.getContentPane().add(BorderLayout.EAST,panel);
frame.setSize(250,200);
frame.setVisible(true);
}
}