很多初学者在用Java布局器自动布局画界面时,经常遇见不知道如何定义区域大小或按钮之间的距离等问题。我写过一篇《实现JAVA手动布局中各个组件能随窗口变化的方法》的文章,有读者反映算坐标不好算,问能不能用布局器实现文章中的界面。其实自动布局也可以解决定义区域大小或按钮之间的距离等问题,只是没有手动布局那么灵活。下面我就举一个例子。
首先,建一个frame文件(Application应用程序),在Design中将this中的layout设置为BorderLayout。
第二,在组件盘内点选Swing Container页签,选取Jpanel图标,在this中上方拖拽一块区域,布局器会自动调整位置与大小;同样的`方法在中下方也拖拽一块区域;在Swing Container页签,选取jScrollPane图标,将jScrollPane在中间拖拽一块区域。拖拽的顺序一定要先上后下再中间。为了方便区分,在Properties的background中,将上方的Jpanel1区域设置为红色,下方的Jpanel2区域设置为橙色,中间的jScrollPane1为粉红色。将Jpanel1和Jpanel2的layout设置为flowLayout(必须要手动设置,不要采用默认值)。
第三,在Jpanel中放入一个Jlable标题栏,JTextField1文本框和Jbutton按钮,在组件盘内点选Swing 页签,选取JLable图标在Jpanel1的中画一个标题栏,将text改为“请输入查询条件”,再选取JtextField在Jpanel1中画一个文本框,将text改为空,最后选取Jbutton在Jpanel1中再画一个按钮将text改为“查询”。画完后他们都是在中间,而且大小固定,这时点选Jpanel的flowLayout1将右边Properties中的alignment设置为LEFT,这时Jpanel1中的组键就会向左排列。选中其中一个组键,在Properties中的preferredSize可以设置组键的宽和高。同样的方法在Jpanel2中画三个Jbutton按钮,将text分别设为“增加”、“删除”、“修改”。点选Jpane2的flowLayout2将右边Properties中的hgap设置为30(按钮的间距,可根据自己的需要调整数值大小), 这样就调整了三个按钮之间的距离,设置vgap还可以改变Jpane2区域的高度。
第四,在jScrollPane1中建一个表格用来显示数据库数据的内容,在组件盘内点选Swing 页签,选取JTable图标,将Jtable加入到jScrollPane1中。
最后,将this中的defaultCloseOperation改为EXIT_ON_CLOSE,这样在关闭窗口时程序会自动退出。
程序源代码如下(除中文注释部分的两句是自己加上去,其余是自动生成):
import g.*;
import .*;
import t.*;
import or;
import ultTableModel;
public class Frame1
extends JFrame {
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextField1 = new JTextField();
JButton jButton1 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
FlowLayout flowLayout2 = new FlowLayout();
JButton jButton2 = new JButton();
JButton jButton3 = new JButton();
JButton jButton4 = new JButton();
GridLayout gridLayout1 = new GridLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTable jTable1 = new JTable();
public Frame1() {
try {
jbInit();
}
catch (Exception e) {
tStackTrace();
}
}
public static void main(String[] args) {
Frame1 frame1 = new Frame1();
ize(new Dimension(400, 350));
();
}
private void jbInit() throws Exception {
ontentPane()ayout(borderLayout1);
ackground();
ayout(flowLayout1);
ackground();
ayout(flowLayout2);
ackground();
ayout(gridLayout1);
referredSize(new Dimension(100, 16));
ext("请输入查询条件");
referredSize(new Dimension(140, 22));
ext("");
ext("查询");
ctionListener(new Frame1_jButton1_actionAdapter(this));
lignment();
gap(5);
gap(10);
ext("增加");
ext("删除");
ext("修改");
gap(30);
gap(5);
efaultCloseOperation(EXIT_ON_CLOSE);
ontentPane()(jPanel1, H);
(jLabel1, null);
(jTextField1, null);
(jButton1, null);
ontentPane()(jPanel2, H);
(jButton2, null);
(jButton3, null);
(jButton4, null);
ontentPane()(jPanel3, ER);
(jScrollPane1, null);
iewport()(jTable1, null);
}
//模拟查询数据库
void jButton1_actionPerformed(ActionEvent e) {
try { //制作表
Vector vcol = new Vector(); //列名
Vector vrow = new Vector(); //内容
for (int col = 1; col < 31; col++) {
lement("列" + col);
}
for (int row = 1; row < 101; row++) {
Vector vr1 = new Vector();
for (int col = 1; col < 31; col++) {
lement(row + "/" + col);
}
lement(vr1);
}
DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
jTable1 = new JTable(vrow, vcol);
utoResizeMode(_RESIZE_OFF); //滚动条设置左右滚
iewport()(jTable1, null); //在滚动条中放入表
}
catch (Exception ex) {
MessageDialog(null, ex);
}
}
}
class Frame1_jButton1_actionAdapter
implements onListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
tee = adaptee;
}
public void actionPerformed(ActionEvent e) {
ton1_actionPerformed(e);
}
}