很多初学者在用Java布局器自动布局画界面时经常遇见不知道如何定义区域大小或按钮之间的距离等问题我写过一篇《实现JAVA手动布局中各个组件能随窗口变化的方法》的文章有读者反映算坐标不好算问能不能用布局器实现文章中的界面其实自动布局也可以解决定义区域大小或按钮之间的距离等问题只是没有手动布局那么灵活下面我就举一个例子
首先建一个frame文件(Application应用程序)在Design中将this中的layout设置为BorderLayout
第二在组件盘内点选Swing Container页签选取Jpanel图标在this中上方拖拽一块区域布局器会自动调整位置与大小同样的方法在中下方也拖拽一块区域在Swing Container页签选取jScrollPane图标将jScrollPane在中间拖拽一块区域拖拽的顺序一定要先上后下再中间为了方便区分在Properties的background中将上方的Jpanel区域设置为红色下方的Jpanel区域设置为橙色中间的jScrollPane为粉红色将Jpanel和Jpanel的layout设置为flowLayout(必须要手动设置不要采用默认值)
第三在Jpanel中放入一个Jlable标题栏JTextField文本框和Jbutton按钮在组件盘内点选Swing 页签选取JLable图标在Jpanel的中画一个标题栏将text改为请输入查询条件再选取JtextField在Jpanel中画一个文本框将text改为空最后选取Jbutton在Jpanel中再画一个按钮将text改为查询画完后他们都是在中间而且大小固定这时点选Jpanel的flowLayout将右边Properties中的alignment设置为LEFT这时Jpanel中的组键就会向左排列选中其中一个组键在Properties中的preferredSize可以设置组键的宽和高同样的方法在Jpanel中画三个Jbutton按钮将text分别设为增加删除修改点选Jpane的flowLayout将右边Properties中的hgap设置为(按钮的间距可根据自己的需要调整数值大小) 这样就调整了三个按钮之间的距离设置vgap还可以改变Jpane区域的高度
第四在jScrollPane中建一个表格用来显示数据库数据的内容在组件盘内点选Swing 页签选取JTable图标将Jtable加入到jScrollPane中
最后将this中的defaultCloseOperation改为EXIT_ON_CLOSE这样在关闭窗口时程序会自动退出
程序源代码如下(除中文注释部分的两句是自己加上去其余是自动生成)
import javaxswing*;
import javaawt*;
import javaawtevent*;
import javautilVector;
import javaxswingtableDefaultTableModel;
public class Frame
extends JFrame {
BorderLayout borderLayout = new BorderLayout();
JPanel jPanel = new JPanel();
JPanel jPanel = new JPanel();
JPanel jPanel = new JPanel();
JLabel jLabel = new JLabel();
JTextField jTextField = new JTextField();
JButton jButton = new JButton();
FlowLayout flowLayout = new FlowLayout();
FlowLayout flowLayout = new FlowLayout();
JButton jButton = new JButton();
JButton jButton = new JButton();
JButton jButton = new JButton();
GridLayout gridLayout = new GridLayout();
JScrollPane jScrollPane = new JScrollPane();
JTable jTable = new JTable();
public Frame() {
try {
jbInit();
}
catch (Exception e) {
eprintStackTrace();
}
}
public static void main(String[] args) {
Frame frame = new Frame();
framesetSize(new Dimension( ));
frameshow();
}
private void jbInit() throws Exception {
thisgetContentPane()setLayout(borderLayout);
jPanelsetBackground(Colorred);
jPanelsetLayout(flowLayout);
jPanelsetBackground(Colorred);
jPanelsetLayout(flowLayout);
jPanelsetBackground(Colorpink);
jPanelsetLayout(gridLayout);
jLabelsetPreferredSize(new Dimension( ));
jLabelsetText(请输入查询条件);
jTextFieldsetPreferredSize(new Dimension( ));
jTextFieldsetText();
jButtonsetText(查询);
jButtonaddActionListener(new Frame_jButton_actionAdapter(this));
flowLayoutsetAlignment(FlowLayoutLEFT);
flowLayoutsetHgap();
flowLayoutsetVgap();
jButtonsetText(增加);
jButtonsetText(删除);
jButtonsetText(修改);
flowLayoutsetHgap();
flowLayoutsetVgap();
thissetDefaultCloseOperation(EXIT_ON_CLOSE);
thisgetContentPane()add(jPanel BorderLayoutNORTH);
jPaneladd(jLabel null);
jPaneladd(jTextField null);
jPaneladd(jButton null);
thisgetContentPane()add(jPanel BorderLayoutSOUTH);
jPaneladd(jButton null);
jPaneladd(jButton null);
jPaneladd(jButton null);
thisgetContentPane()add(jPanel BorderLayoutCENTER);
jPaneladd(jScrollPane null);
jScrollPanegetViewport()add(jTable null);
}
//模拟查询数据库
void jButton_actionPerformed(ActionEvent e) {
try { //制作表
Vector vcol = new Vector(); //列名
Vector vrow = new Vector(); //内容
for (int col = ; col < 31; col++) {
vcol.addElement("列" + col);
}
for (int row = 1; row < 101; row++) {
Vector vr1 = new Vector();
for (int col = 1; col < 31; col++) {
vr1.addElement(row + "/" + col);
}
vrow.addElement(vr1);
}
DefaultTableModel dtm = new DefaultTableModel(vrow, vcol);
jTable1 = new JTable(vrow, vcol);
jTable1.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); //滚动条设置左右滚
this.jScrollPane1.getViewport().add(jTable1, null); //在滚动条中放入表
}
catch (Exception ex) {
JOptionPane.showMessageDialog(null, ex);
}
}
}
class Frame1_jButton1_actionAdapter
implements java.awt.event.ActionListener {
Frame1 adaptee;
Frame1_jButton1_actionAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}