Java布局管理器使用方法解析
2024年03月01日
很多初学者在用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 javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
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){
e.printStackTrace();
}
}
public static void main(String[]args){
Frame1 frame1=new Frame1();
frame1.setSize(new Dimension(400,350));
frame1.show();
}
private void jbInit()throws Exception{
this.getContentPane().setLayout(borderLayout1);
jPanel1.setBackground(Color.red);
jPanel1.setLayout(flowLayout1);
jPanel2.setBackground(Color.red);
jPanel2.setLayout(flowLayout2);
jPanel3.setBackground(Color.pink);
jPanel3.setLayout(gridLayout1);
jLabel1.setPreferredSize(new Dimension(100,16));
jLabel1.setText("请输入查询条件");
jTextField1.setPreferredSize(new Dimension(140,22));
jTextField1.setText("");
jButton1.setText("查询");
jButton1.addActionListener(new Frame1_jButton1_actionAdapter(this));
flowLayout1.setAlignment(FlowLayout.LEFT);
flowLayout1.setHgap(5);
flowLayout1.setVgap(10);
jButton2.setText("增加");
jButton3.setText("删除");
jButton4.setText("修改");
flowLayout2.setHgap(30);
flowLayout2.setVgap(5);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.getContentPane().add(jPanel1,BorderLayout.NORTH);
jPanel1.add(jLabel1,null);
jPanel1.add(jTextField1,null);
jPanel1.add(jButton1,null);
this.getContentPane().add(jPanel2,BorderLayout.SOUTH);
jPanel2.add(jButton2,null);
jPanel2.add(jButton3,null);
jPanel2.add(jButton4,null);
this.getContentPane().add(jPanel3,BorderLayout.CENTER);
jPanel3.add(jScrollPane1,null);
jScrollPane1.getViewport().add(jTable1,null);
}
//模拟查询数据库
void jButton1_actionPerformed(ActionEvent e){
try{//制作表
Vector vcol=new Vector();//列名
Vector vrow=new Vector();//内容
for(int col=1;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);
}
}