Java布局管理器使用方法
更新時(shí)間:2006年10月13日 00:00:00 作者:
很多初學(xué)者在用Java布局器自動(dòng)布局畫界面時(shí),經(jīng)常遇見不知道如何定義區(qū)域大小或按鈕之間的距離等問題。我寫過一篇《實(shí)現(xiàn)JAVA手動(dòng)布局中各個(gè)組件能隨窗口變化的方法》的文章,有讀者反映算坐標(biāo)不好算,問能不能用布局器實(shí)現(xiàn)文章中的界面。其實(shí)自動(dòng)布局也可以解決定義區(qū)域大小或按鈕之間的距離等問題,只是沒有手動(dòng)布局那么靈活。下面我就舉一個(gè)例子。
首先,建一個(gè)frame文件(Application應(yīng)用程序),在Design中將this中的layout設(shè)置為BorderLayout。
第二,在組件盤內(nèi)點(diǎn)選Swing Container頁簽,選取Jpanel圖標(biāo),在this中上方拖拽一塊區(qū)域,布局器會(huì)自動(dòng)調(diào)整位置與大?。煌瑯拥姆椒ㄔ谥邢路揭餐献б粔K區(qū)域;在Swing Container頁簽,選取jScrollPane圖標(biāo),將jScrollPane在中間拖拽一塊區(qū)域。拖拽的順序一定要先上后下再中間。為了方便區(qū)分,在Properties的background中,將上方的Jpanel1區(qū)域設(shè)置為紅色,下方的Jpanel2區(qū)域設(shè)置為橙色,中間的jScrollPane1為粉紅色。將Jpanel1和Jpanel2的layout設(shè)置為flowLayout(必須要手動(dòng)設(shè)置,不要采用默認(rèn)值)。
第三,在Jpanel中放入一個(gè)Jlable標(biāo)題欄,JTextField1文本框和Jbutton按鈕,在組件盤內(nèi)點(diǎn)選Swing 頁簽,選取JLable圖標(biāo)在Jpanel1的中畫一個(gè)標(biāo)題欄,將text改為“請(qǐng)輸入查詢條件”,再選取JtextField在Jpanel1中畫一個(gè)文本框,將text改為空,最后選取Jbutton在Jpanel1中再畫一個(gè)按鈕將text改為“查詢”。畫完后他們都是在中間,而且大小固定,這時(shí)點(diǎn)選Jpanel的flowLayout1將右邊Properties中的alignment設(shè)置為LEFT,這時(shí)Jpanel1中的組鍵就會(huì)向左排列。選中其中一個(gè)組鍵,在Properties中的preferredSize可以設(shè)置組鍵的寬和高。同樣的方法在Jpanel2中畫三個(gè)Jbutton按鈕,將text分別設(shè)為“增加”、“刪除”、“修改”。點(diǎn)選Jpane2的flowLayout2將右邊Properties中的hgap設(shè)置為30(按鈕的間距,可根據(jù)自己的需要調(diào)整數(shù)值大?。? 這樣就調(diào)整了三個(gè)按鈕之間的距離,設(shè)置vgap還可以改變Jpane2區(qū)域的高度。
第四,在jScrollPane1中建一個(gè)表格用來顯示數(shù)據(jù)庫數(shù)據(jù)的內(nèi)容,在組件盤內(nèi)點(diǎn)選Swing 頁簽,選取JTable圖標(biāo),將Jtable加入到j(luò)ScrollPane1中。
最后,將this中的defaultCloseOperation改為EXIT_ON_CLOSE,這樣在關(guān)閉窗口時(shí)程序會(huì)自動(dòng)退出。
程序源代碼如下(除中文注釋部分的兩句是自己加上去,其余是自動(dòng)生成):
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("請(qǐng)輸入查詢條件");
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);
}
//模擬查詢數(shù)據(jù)庫
void jButton1_actionPerformed(ActionEvent e) {
try { //制作表
Vector vcol = new Vector(); //列名
Vector vrow = new Vector(); //內(nèi)容
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); //滾動(dòng)條設(shè)置左右滾
this.jScrollPane1.getViewport().add(jTable1, null); //在滾動(dòng)條中放入表
}
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);
}
}
首先,建一個(gè)frame文件(Application應(yīng)用程序),在Design中將this中的layout設(shè)置為BorderLayout。
第二,在組件盤內(nèi)點(diǎn)選Swing Container頁簽,選取Jpanel圖標(biāo),在this中上方拖拽一塊區(qū)域,布局器會(huì)自動(dòng)調(diào)整位置與大?。煌瑯拥姆椒ㄔ谥邢路揭餐献б粔K區(qū)域;在Swing Container頁簽,選取jScrollPane圖標(biāo),將jScrollPane在中間拖拽一塊區(qū)域。拖拽的順序一定要先上后下再中間。為了方便區(qū)分,在Properties的background中,將上方的Jpanel1區(qū)域設(shè)置為紅色,下方的Jpanel2區(qū)域設(shè)置為橙色,中間的jScrollPane1為粉紅色。將Jpanel1和Jpanel2的layout設(shè)置為flowLayout(必須要手動(dòng)設(shè)置,不要采用默認(rèn)值)。
第三,在Jpanel中放入一個(gè)Jlable標(biāo)題欄,JTextField1文本框和Jbutton按鈕,在組件盤內(nèi)點(diǎn)選Swing 頁簽,選取JLable圖標(biāo)在Jpanel1的中畫一個(gè)標(biāo)題欄,將text改為“請(qǐng)輸入查詢條件”,再選取JtextField在Jpanel1中畫一個(gè)文本框,將text改為空,最后選取Jbutton在Jpanel1中再畫一個(gè)按鈕將text改為“查詢”。畫完后他們都是在中間,而且大小固定,這時(shí)點(diǎn)選Jpanel的flowLayout1將右邊Properties中的alignment設(shè)置為LEFT,這時(shí)Jpanel1中的組鍵就會(huì)向左排列。選中其中一個(gè)組鍵,在Properties中的preferredSize可以設(shè)置組鍵的寬和高。同樣的方法在Jpanel2中畫三個(gè)Jbutton按鈕,將text分別設(shè)為“增加”、“刪除”、“修改”。點(diǎn)選Jpane2的flowLayout2將右邊Properties中的hgap設(shè)置為30(按鈕的間距,可根據(jù)自己的需要調(diào)整數(shù)值大?。? 這樣就調(diào)整了三個(gè)按鈕之間的距離,設(shè)置vgap還可以改變Jpane2區(qū)域的高度。
第四,在jScrollPane1中建一個(gè)表格用來顯示數(shù)據(jù)庫數(shù)據(jù)的內(nèi)容,在組件盤內(nèi)點(diǎn)選Swing 頁簽,選取JTable圖標(biāo),將Jtable加入到j(luò)ScrollPane1中。
最后,將this中的defaultCloseOperation改為EXIT_ON_CLOSE,這樣在關(guān)閉窗口時(shí)程序會(huì)自動(dòng)退出。
程序源代碼如下(除中文注釋部分的兩句是自己加上去,其余是自動(dòng)生成):
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("請(qǐng)輸入查詢條件");
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);
}
//模擬查詢數(shù)據(jù)庫
void jButton1_actionPerformed(ActionEvent e) {
try { //制作表
Vector vcol = new Vector(); //列名
Vector vrow = new Vector(); //內(nèi)容
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); //滾動(dòng)條設(shè)置左右滾
this.jScrollPane1.getViewport().add(jTable1, null); //在滾動(dòng)條中放入表
}
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);
}
}
相關(guān)文章
點(diǎn)擊地圖div上的按鈕實(shí)現(xiàn)對(duì)地圖數(shù)據(jù)的入庫操作
在地圖div上添加一個(gè)按鈕并單擊在彈出層的輸入框內(nèi)輸入數(shù)據(jù)后點(diǎn)擊提交按鈕將數(shù)據(jù)提交至數(shù)據(jù)庫,具體的實(shí)現(xiàn)如下,感興趣的朋友可以參考下2013-08-08用Java實(shí)現(xiàn)HTTP文件隊(duì)列下載
用Java實(shí)現(xiàn)HTTP文件隊(duì)列下載...2006-10-10struts2 action跳轉(zhuǎn)調(diào)用另一個(gè)程序
主要為了在一個(gè)Action成功后跳轉(zhuǎn)調(diào)用另一個(gè)程序,需要的朋友可以參考下2012-11-11Java Web實(shí)現(xiàn)的基本MVC實(shí)例分析
這篇文章主要介紹了Java Web實(shí)現(xiàn)的基本MVC,以完整實(shí)例形式較為詳細(xì)的分析了JSP實(shí)現(xiàn)MVC架構(gòu)的具體步驟與相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09jsp獲取action傳來的session和session清空以及判斷
這篇文章主要介紹了jsp獲取action傳來的session和session清空以及判斷,需要的朋友可以參考下2014-03-03jsp的注釋可能會(huì)影響頁面加載速度讓代碼扔繼續(xù)執(zhí)行
注釋里面的java代碼還是會(huì)得到執(zhí)行,可以再查看頁面源代碼上看到執(zhí)行完成的內(nèi)容2014-10-10