先看下API:

public void setAlignmentX(float alignmentX)

设置垂直对齐方式。

参数: alignmentX - 新的垂直对齐方式

网上关于这个函数的详细情况介绍的不多,JAVA布局管理器提到说, setAlignmentX(left,right)只有在布局是BoxLayout.Y_AXIS才效,而setAlignmentY(top,button)在布局为BoxLayout.X_AXIS才效果。我基本同意这个理解,也就是说,setAlignmentX()用于使用了BoxLayout.Y_AXIS时,将控件在X轴方向设置为左对齐、右对齐或居中对齐;setAlignmentY()用于使用了BoxLayout.X_AXIS时,将控件在Y轴方向设置为顶对齐、底对齐或居中对齐。以下是测试代码:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.LayoutManager;
import java.awt.TextArea;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.OverlayLayout;
import javax.swing.border.EtchedBorder; /*
* 2015-06-13
*/
public class setAlignmentDemo { public static void main(String[] args) {
// TODO Auto-generated method stub //第一个panel,垂直排列
JPanel jpanelY = new JPanel();
jpanelY.setLayout(new BoxLayout(jpanelY,BoxLayout.Y_AXIS));
jpanelY.setBorder(BorderFactory.createTitledBorder("BoxLayout.Y_AXIS"));
JButton buttonY1 = new JButton("LEFT_ALIGNMENT");
JButton buttonY2 = new JButton("RIGHT_ALIGNMENT");
JButton buttonY3 = new JButton("CENTER_ALIGNMENT"); buttonY1.setAlignmentX(Component.LEFT_ALIGNMENT);
buttonY2.setAlignmentX(Component.RIGHT_ALIGNMENT);
buttonY3.setAlignmentX(Component.CENTER_ALIGNMENT); jpanelY.add(buttonY1);
jpanelY.add(buttonY2);
jpanelY.add(buttonY3);
//第二个panel,水平排列
JPanel jpanelX=new JPanel();
jpanelX.setLayout(new BoxLayout(jpanelX,BoxLayout.X_AXIS));
jpanelX.setBorder(BorderFactory.createTitledBorder("BoxLayout.X_AXIS")); JButton buttonX1 = new JButton("TOP_ALIGNMENT");
JButton buttonX2 = new JButton("BOTTOM_ALIGNMENT");
JButton buttonX3 = new JButton("CENTER_ALIGNMENT"); buttonX1.setAlignmentY(Component.TOP_ALIGNMENT);
buttonX2.setAlignmentY(Component.BOTTOM_ALIGNMENT);
buttonX3.setAlignmentY(Component.CENTER_ALIGNMENT); jpanelX.add(buttonX1);
jpanelX.add(buttonX2);
jpanelX.add(buttonX3); //添加两个panel到窗体
JFrame frame = new JFrame("setAlignmentDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.add(jpanelX);
frame.add(jpanelY);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}

运行效果如下:

运行效果图

那么问题来了,运行结果的对齐方式是反的?

最新文章

  1. 测试Javacript里的checkbox是否被选中的status和checked的替换方法
  2. 高德地图API编译警告
  3. 知道创宇研发技能表v3.1
  4. Powerdesigner自定义DBMS(以derby数据库为例)
  5. Spring MVC静态资源处理——<mvc:resources /> ||<mvc:default-servlet-handler /> 转载
  6. MR中的combiner和partitioner
  7. 自动化测试实施的几个idea
  8. mapreduce任务失败、重试、猜測式运行机制小结
  9. vue项目的骨架及常用组件介绍
  10. Spring Cloud微服务笔记(五)Feign
  11. JavaEE 之 DBCP
  12. SpringBoot集成阿里巴巴Druid监控
  13. Beautiful Soup 学习手册
  14. Android中的padding和margin的区别
  15. linux命令 dirname
  16. thinkphp3.2 实现上一篇和下一篇
  17. PyQt4开关按钮ToggleButton
  18. sqlserver之on与where条件
  19. js代码小优化
  20. Magicodes.NET框架之路[转]

热门文章

  1. 细说 Java 的深拷贝和浅拷贝
  2. 易云捷讯MySQL云数据库上线,推进IaaS与PaaS融合战略布局
  3. table之thead兼容
  4. python之--------封装
  5. 关于CNoTrackObject
  6. APP软件半成品测试技巧
  7. cycript
  8. 家中路由添加静态IP映射(一)
  9. Linux中的定时任务at、crontab
  10. 深入剖析C++多态、VPTR指针、虚函数表