字元串常用操作
length()//擷取字元串的長度
startsWith(Strings)、endsWith(String s) //判斷字元串的字首或字尾與已知字元串是否相同字首
equals(String s) //比較兩個字元串
Integer.parseInt(字元串)
Long.parseLong(字元串)
Folat.valueOf(字元串).floatValue()
Double.valueOf(字元串).doubleValue()//把字元串強制轉換
valueOf(數值) //将數值轉化為字元串
字元串檢索:
indexOf(Srting s)//從頭開始檢索
indexOf(String s ,int startpoint) //從startpoint處開始檢索,如果沒有檢索到,将傳回-1
字元串截取:
substring(int startpoint) //從startpoint處開始擷取
substring(int start,int end) //從start到end中間的字元
替換字元串中的字元:
replace(char old,char new) //用new替換old
分析字元串
StringTokenizer(String s)//構造一個分析器,使用預設分隔字元(空格,換行,回車,Tab,進紙符)
StringTokenizer(String s,String delim) //delim是自己定義的分隔符
nextToken() //逐個擷取字元串中的語言符号
boolean hasMoreTokens() //隻要字元串還有語言符号将傳回true,否則傳回false
countTokens() //得到一共有多少個語言符号
文本框和文本區
文本框
TextField()//構造文本框,一個字元長、TextField(int x) 構造文本框,x個字元長
TextField(String s) //構造文本框,顯示s、setText(String s) 設定文本為s
getText() //擷取文本
setEchoChar(char c) //設定顯示字元為c
setEditable(boolean) //設定文本框是否可以被修改
addActionListener() //添加螢幕
removeActionListener() //移去螢幕
文本區
TextArea()//構造文本區
TextArea(String s) //構造文本區,顯示s
TextArea(String s,int x,int y) //構造文本區,x行,y列,顯示s
TextArea(int x,int y) //構造文本區,x行,y列
TextArea(String s,int x,ing y,intscrollbar)
scrollbar的值是:
TextArea.SCROLLBARS_BOTH;TextArea.SCROLLBARS_VERTICAL_ONLY
TextArea.SCROLLBARS_HORIZONTAL_ONLY;TextArea.SCROLLBARS_NONE
setText(String s)//設定文本為s;getText() 擷取文本
addTextListener() //添加螢幕;removeTextListener() 移去螢幕
insert(String s,int x) //在x處插入文本s
replaceRange(String s,int x,int y) //用s替換從x到y處的文本
append(String s) //在文本的最後追加文本s
Int getCaretPosition(int n) //擷取文本區中光标的位置
按鈕
Button()//構造按鈕
Button(String s) //構造按鈕,标簽是s
setLabel(String s) //設定按鈕标簽是s
getLabel() //擷取按鈕标簽
addActionListener() //添加螢幕
removeActionListener() //移去螢幕
标簽
Label()//構造标簽
Label(String s) //構造标簽,顯示s
Label(String s,int x) //x是對齊方式,取值:Label.LEFT;Label.RIGHT;Label.CENTER
setText(String s) //設定文本s
getText() //擷取文本
setBackground(Color c) //設定标簽背景顔色
setForeground(Color c) //設定字型顔色
選擇框
Checkbox()//構造選擇框
Checkbox(String s) //構造選擇框,給定标題s
Checkbox(String s,boolean b) //b設定初始狀态
Checkbox(String s,boolean b,CheckboxGroup g) //g設定了所屬的組(有了組就成為單選框)
addItemListener() //添加螢幕
removeItemListener() //移去螢幕
getState() //傳回選擇框的是否選中狀态
setState(boolean b) //設定選擇框的狀态
getLabel() //擷取選擇框的标題
setLabel(String s) //設定選擇框的标題為s
選擇控件
Choice()//構造選擇控件
add(String s) //向選擇控件增加一個選項
addItemListener() //添加螢幕
removeItemListener() //移去螢幕
getSelectedIndex() //傳回目前選項的索引
getSelectedItem() //傳回目前選項的字元串代表
insert(String s,int n) //在n處插入選項s
remove(intn)
removeAll()
滾動清單
List()//構造滾動清單
List(int n) //參數n是可見行數
List(int n,boolean b) //參數b是設定是否可以多項選擇
add(String s) //向清單的結尾增加一個選項
add(String s,int n) //在n處增加一個選項
AddActionListener() //滾動清單添加螢幕
addItemListener() //滾動清單上的選項添加螢幕
remove(int n) //删除n初的選項
remnoveAll() //删除全部選項
getSelectedIndex() //傳回目前選項的索引
getSelectedItem() //傳回目前選項的字元串代表
元件類的一些常用方法void setBackground(Color c) //設定元件背景顔色
void setForeground(Color c) //設定元件前景顔色
void setFonts(Font f) //設定元件字型
void setBounds(int x,int y,int w,int h) //設定坐标,x,y表示在容器中坐标,w,h表示寬和高
void setLocation(int x,int y) //移動到x,y 處;void setSize(int w,int h) 設定寬和高
void setVisible(boolean b) //設定組建是否可見
int getBounds().wigth //擷取寬
int getBounds().height //擷取高
int getBounds().x //擷取x 坐标;
注意1). 要固定JComboBox大小有兩個地方需要關注的。1、你的JComboBox放的面闆的布局是什麼類型的布局,如果不是null布局就調整布局來給JComboBox配置設定合适的大小。2、如果你用的是空布局的話,那麼用setSize方法 或者 setBounds方法,來改變JComboBox大小setBounds可以改變控件的大小和位置。2). Java中setBounds和setSize有什麼差別:bounds就是size和location的結合體