日期:2014-05-16 浏览次数:20543 次
一、实用源程序,可以用于自己的类库中调用相应的setConstraints方法即可迅速地实现界面良好布局
:
package ioutil;
import javax.swing.*;
import java.awt.*;
public class EasyGridLayout extends GridBagLayout{
?public void setConstraints(JLabel c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet(c, row, col, width, height,
???????????????? 0, 0,
???????????????? GridBagConstraints.NONE,
???????????????? GridBagConstraints.NORTHWEST);
?? }
?????
public void setConstraints (JButton c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet (c, row, col, width, height,
???????????????? 0,0,
???????????????? GridBagConstraints.NONE,
???????????????? GridBagConstraints.CENTER);
?? }
?? public void setConstraints (JTextField c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet (c, row, col, width, height,
???????????????? 100, 0,
???????????????? GridBagConstraints.HORIZONTAL,
???????????????? GridBagConstraints.NORTHWEST);
?? }
? ? public void setConstraints (JScrollPane c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet (c, row, col, width, height,
???????????????? 100, 100,
???????????????? GridBagConstraints.BOTH,
???????????????? GridBagConstraints.NORTHWEST);
?? }
public void setConstraints (JTextArea c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet (c, row, col, width, height,
???????????????? 100, 100,
???????????????? GridBagConstraints.BOTH,
???????????????? GridBagConstraints.NORTHWEST);
?? }
?public void setConstraints (JList c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet (c, row, col, width, height,
???????????????? 100, 100,
???????????????? GridBagConstraints.BOTH,
???????????????? GridBagConstraints.NORTHWEST);
?? }
public void setConstraints (JCheckBox c, int row, int col,
?????????????????????????????? int width, int height){
???? finishSet (c, row, col, width, height,
??????????????? 0, 0,
??????????????? GridBagConstraints.HORIZONTAL,
??????????????? GridBagConstraints.NORTHWEST);
?? }
public void setConstraints (JRadioButton c, int row, int col,
?????????????????????????????? int width, int height){
???? finishSet (c, row, col, width, height,
??????????????? 0, 0,
??????????????? GridBagConstraints.HORIZONTAL,
??????????????? GridBagConstraints.NORTHWEST);
?? }
?public void setConstraints (JPanel c, int row, int col,
?????????????????????????????? int width, int height){
????? finishSet (c, row, col, width, height,
???????????????? 100, 100,
???????????????? GridBagConstraints.BOTH,
???????????????? GridBagConstraints.NORTHWEST);
?? }
private void finishSet (Component c, int y, int x, int w, int h,//
gridx,gridwidth4个单位都是网格
?????????????????????????? int weightx, int weighty, //
分配额外空间比例给行列
,以上三种都是如何将网格分配给组件。
?????????????????????????? int fill, int anchor){//
组件大于或小于网格时伸缩或放置位置。
在特定网格区域内设置伸缩,放置,内边距。
??????????????????????????
????? GridBagConstraints gbc = new GridBagConstraints();
?????
????? gbc.insets.bottom = 5; /
/组件与放置它的grix、gridy之间的偏移,单位为px.
????? gbc.insets.left = 5;
????? gbc.insets.right = 5;
????? gbc.in