首页 新闻 赞助 找找看

swing-setAlignmentX()函数的对齐效果是反的?

-1
悬赏园豆:20 [待解决问题]

我的理解是:setAlignmentX()用于在使用了BoxLayout.Y_AXIS时,设置panel在X轴方向的对齐方式,setAlignmentY()同理。因此作了如下测试:

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;


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

运行效果如下:

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

pzy4447的主页 pzy4447 | 初学一级 | 园豆:182
提问于:2015-06-13 20:45
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册