Swing组件对齐方式
2014-02-13 17:13
225 查看
这次记录一下Swing组件的对齐问题。
-----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);
关于setAlignmentY的使用
这个方法的确是用来设置对齐的,但对JPanl使用setAlignmentY(水平对齐)是不会另panel上组件改变对齐方式的,Api文档描述很简单:Sets the the horizontal alignment.
其实该方法是用来设置组件自身的对齐方式,并且要求必须在布局方式为BoxLayout.X_AXIS
(同理,setAlignmentX对应于BoxLayout.Y_AXIS)
下面代码展示了这个问题:
JPanel panel = new JPanel();
panel .setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("button");
JButton button2 = new JButton("button2");
JButton button3 = new JButton("button3");
add(button);
add(button2);
add(button3);
button.setAlignmentX(Component.LEFT_ALIGNMENT);
button2.setAlignmentX(Component.RIGHT_ALIGNMENT);
button3.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel panel = new JPanel();
panel .setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("button");
JButton button2 = new JButton("button2");
JButton button3 = new JButton("button3");
add(button);
add(button2);
add(button3);
button.setAlignmentX(Component.LEFT_ALIGNMENT);
button2.setAlignmentX(Component.RIGHT_ALIGNMENT);
button3.setAlignmentX(Component.LEFT_ALIGNMENT);
通过上面代码,可以看到BoxLayout布局下,调用组件的setAlignmentX后的对齐效果
查看API文档可以发现,可作为容器的组件,均由布局管理来设置对齐方式。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/walilk/archive/2010/05/23/5618321.aspx
-----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);
关于setAlignmentY的使用
这个方法的确是用来设置对齐的,但对JPanl使用setAlignmentY(水平对齐)是不会另panel上组件改变对齐方式的,Api文档描述很简单:Sets the the horizontal alignment.
其实该方法是用来设置组件自身的对齐方式,并且要求必须在布局方式为BoxLayout.X_AXIS
(同理,setAlignmentX对应于BoxLayout.Y_AXIS)
下面代码展示了这个问题:
JPanel panel = new JPanel();
panel .setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("button");
JButton button2 = new JButton("button2");
JButton button3 = new JButton("button3");
add(button);
add(button2);
add(button3);
button.setAlignmentX(Component.LEFT_ALIGNMENT);
button2.setAlignmentX(Component.RIGHT_ALIGNMENT);
button3.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel panel = new JPanel();
panel .setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JButton button = new JButton("button");
JButton button2 = new JButton("button2");
JButton button3 = new JButton("button3");
add(button);
add(button2);
add(button3);
button.setAlignmentX(Component.LEFT_ALIGNMENT);
button2.setAlignmentX(Component.RIGHT_ALIGNMENT);
button3.setAlignmentX(Component.LEFT_ALIGNMENT);
通过上面代码,可以看到BoxLayout布局下,调用组件的setAlignmentX后的对齐效果
查看API文档可以发现,可作为容器的组件,均由布局管理来设置对齐方式。
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/walilk/archive/2010/05/23/5618321.aspx
相关文章推荐
- Swing系列-组件对齐方式的学习
- [Java] Swing系列-组件对齐方式的学习
- Java_Swing系列组件对齐方式
- Swing系列-组件对齐方式
- Swing组件对齐方式与字体对齐方式
- swing 组件——在JLabel中添加图片的两种方式
- 常用的WYSIWYG方式编辑HTML/XHTML的开源JAVA Swing组件
- java Swing组件的对齐问题
- Swing之JTable篇设置表格单元格的内容对齐方式
- java Swing组件的对齐问题2
- 内存对齐方式
- 使用ActivityOptionsCompat动画共享组件的方式实现跳转Activity动画
- [Android学习笔记四] 自定义Android组件之组合方式创建密码框组件
- java POI实现Excel单元格数据的对齐方式
- Swing 刷新组件java swing中两大原则: 1. 不要阻塞UI线程 2. 不要在UI线程外的线程去操作UI控件
- Creator2中关于组件验证的几种方式
- vuejs 子组件传递父组件的第一种方式
- (7) 让Spring自动扫描和管理Bean ---- 通过在classpath自动扫描方式把组件纳入spring容器中管理
- 布局垂直对齐的方式
- ios之UIButton文本对齐方式