當前位置:學者齋 >

計算機 >java語言 >

Java使用方法集錦

Java使用方法集錦

很多初學者在用Java佈局器自動佈局畫介面時,經常遇見不知道如何定義區域大小或按鈕之間的距離等問題。我寫過一篇《實現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);

}

}

標籤: 集錦 JAVA
  • 文章版權屬於文章作者所有,轉載請註明 https://xuezhezhai.com/zh-tw/jsj/java/o9dg4z.html