Java_Swing系列组件对齐方式

声明:本文材料转载自_http://blog.sina.com.cn/s/blog_7750745b010197ta.html(郭盖)

-----JPanel-----
  首先从Jpanel说起,很多时候,需要在JPanel上使组件遵循某种对齐方式:
 (注,JDK1.5以后版本,对frame调用setLayout会默认在frame的content面板上执行)
  方法:
   使用布局管理器:FlowLayout
  代码:(右对齐)
   panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
 

----JLabel-------
  偶尔,设计Label的时候也会需要让Label上的文字实现某种对齐方式:
  方法:
    setHorizontalAlignment()
  代码:(右对齐)
    label.setHorizontalAlignment(JLabel.RIGHT);
 
----JTextField-----
  JTextField的右对齐很常用了,比如写一个计算器程序的输入框。
  方法:
   setHorizontalAlignment()
  代码:
   field.setHorizontalAlignment(JTextField.RIGHT);

 

----JFormattedTextField -----
  格式化文本框也常常使用。
  方法:(与JTextField相同)
    setHorizontalAlignment() 
  代码:
    field.setHorizontalAlignment(JTextField.RIGHT);


----JPasswordField -----
  密码框……似乎从右边输入是没有必要的。
  方法:(与JTextField相同)
    setHorizontalAlignment()
  代码:
    field.setHorizontalAlignment(JTextField.RIGHT);

 

----JTexArea-----
  这是为了实现从Area的右边开始输入:
  方法:
    setComponentOrientation()
  代码:(从右向左输入)
    area.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
  关于setComponentOrientation: 从Component继承而来,API这样描述:

  Sets the language-sensitive orientation that is to be used to order the elements or text within this component. Language-sensitive LayoutManager and Component subclasses will use this property to determine how to lay out and draw components.
 
---JEditorPane---
  这个我没尝试出右边输入的方法
 
---JTextPane----
  方法:
    setComponentOrientation()
  代码:(从右向左输入)
    textPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值