本章包括三部分:
一、標籤(Label)組件構造方法
二、標籤(Label)組件常用樣式
三、標籤(Label)組件示例
本章示例運行效果:
一、標籤(Label)組件構造方法
標籤(Label類)組件是SWT中最簡單的組件。Label類的構造方法和Button類相似,參數的含義與相同,格式如下:
Label(Composite parent,int style)
二、標籤(Label)組件常用樣式
SWT.CENTER:文字居中。
SWT.RIGHT:文字靠右。
SWT.LEFT:文字靠左。
SWT.NONE:默認式樣。
SWT.WRAP:自動換行。
SWT.BORDER:深陷型。
SWT.SEPARATOR:分欄符,默認爲豎線分欄。
SWT.HORIZONTAL:橫線分欄符。
三、標籤(Label)組件示例
package com.bianyf.frame.swt.demo;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.*;
public class LabelTest {
public static void main(String[] args) {
Display display=new Display();//創建一個display對象。
Shell shell=new Shell(display);//shell是程序的主窗體
//shell.setLayout(null); //設置shell的佈局方式
shell.setText("標籤示例"); //設置主窗體的標題
Label lb1=new Label(shell,SWT.BORDER|SWT.RIGHT);//深陷型、文字右對齊
lb1.setBounds(10,10,70,30);
lb1.setText("標籤1");
lb1.setFont(new Font(display,"黑體",14,SWT.BOLD));//設置文字的字體字號
lb1.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_BLUE));
Label lb2=new Label(shell,SWT.CENTER);//文字居中的標籤
lb2.setBounds(90,10,70,30);
lb2.setText("標籤2");
lb2.setFont(new Font(display,"宋體",14,SWT.NORMAL));//設置文字的字體字號
Label lb3=new Label(shell,SWT.SEPARATOR|SWT.BORDER);//豎直分欄符
lb3.setBounds(10,50,70,30);
Label lb4=new Label(shell,SWT.SEPARATOR|SWT.HORIZONTAL|SWT.BORDER);//水平分欄符
lb4.setBounds(90,50,70,30);
shell.pack(); //自動調整主窗體的大小
shell.open(); //打開主窗體
while(!shell.isDisposed()){ //如果主窗體沒有關閉則一直循環
if(!display.readAndDispatch()){ //如果display不忙
display.sleep(); //休眠
}
}
display.dispose(); //銷燬display
}
}
原文鏈接:http://blog.sina.com.cn/s/blog_aa757f440101apt2.html
SWT組件之Table淺析
研究了幾天Table、TableViewer和數據庫的東西,現整理table的一些基礎知識如下。
首先明確基本概念:Table爲一張表;TableColumn爲列;TableItem爲行中的實體內容。
1)Table的幾個方法:1.table.remove(int index)→刪除相應的tableItem(一個行)。
//table.remove(int[] indices)→刪除相應的tableItem(幾個行)。
2.table.getSelectionIndices()→返回值爲int,即你選中的行的index值。
3.table.getItem(int index)→返回值爲TableItem,獲取其中所選行的內容
4.table.getItems→返回值爲TableItem[]
///////如果需要通過按鈕“保存”後跟新數據庫,則代碼如下。
1)TableItem[] items = table.getItems();
2)保存表格中的數據到數據庫中。
5.table.getItemCount()→得到表格行數
2)有關表格的修改編輯等操作在TableEditor下進行。。///TableEditor是在ControlEditor下的類
TableEditor的幾個important的方法:getEditor,setEditor,getItem,setItem。
3)有關TableItem。tableItem.getText(int index)→獲取這行中相應位置的數據,這裏不用管位置裏面數據的數據類型。
4)///////有關在表格中添加新的一行,並且可以手動輸入。是否能更新至數據庫,這裏不討論。////////////
個人的一些想法,還待研究。
辦法1: (1)通過“添加”按鈕觸發後,在事件中,先添加一個新的TableItem。即:
TableItem tableItem = new TableItem(table,SWT.CENTER);
tableItem.setText(new String[]{"新信息","","",""});//個人覺得很傻逼的做法
(2)然後將這一行的屬性設置成“可編輯”。
(3)修改這一行的信息,填入正確的信息。