Java gui java awt and javax swing
Download
1 / 13

Java GUI - java.awt and javax.swing - - PowerPoint PPT Presentation


  • 191 Views
  • Uploaded on

Java GUI - java.awt and javax.swing -. Useful Components. JLabel, TextField, JPasswordField JScrollPane, JTextArea JCheckBox JPanel, JRadioButton JComboBox. JLabel, TextField, JPasswordField. import javax.swing.*; // 引用套件 import javax.swing.event.*; import java.awt.*;

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Java GUI - java.awt and javax.swing -' - camila


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Java gui java awt and javax swing

Java GUI- java.awt and javax.swing -


Useful components
Useful Components

  • JLabel, TextField, JPasswordField

  • JScrollPane, JTextArea

  • JCheckBox

  • JPanel, JRadioButton

  • JComboBox


Jlabel textfield jpasswordfield
JLabel, TextField, JPasswordField

import javax.swing.*; //引用套件

import javax.swing.event.*;

import java.awt.*;

import java.awt.event.*;

public class LabelFieldEX extends JFrame{

JLabel lbName = new JLabel("帳號(N) : ", JLabel.RIGHT);

JLabel lbPW = new JLabel("密碼(P) : ", JLabel.RIGHT);

JTextField tfName = new JTextField(20);

JPasswordField pfPW = new JPasswordField(20);

JLabel lbEnter = new JLabel("按下 Enter 取得的資料 : ");

//以匿名內部類別的方式定義並宣告監聽器物件

ActionListener al = new ActionListener() {

public void actionPerformed(ActionEvent e) {

//回應ActionEvent事件時, 更新lbEnter標籤的內容

lbEnter.setText("按下 Enter 取得的資料 : ["

+ tfName.getText() + "] ["

+ new String(pfPW.getPassword()) + "]");

}

};


LabelFieldEX(){

lbName.setDisplayedMnemonic('N'); //設定使用N搭配Alt鍵做為記憶鍵

lbName.setLabelFor(tfName); //設定lbName標籤為tfName文字欄位的名稱

lbPW.setDisplayedMnemonic('P'); //設定使用P搭配Alt鍵做為記憶鍵

lbPW.setLabelFor(pfPW); //設定lbPW標籤為tfPW文字欄位的名稱

pfPW.setEchoChar('@'); //設定密碼欄使用的遮罩字元

//註冊回應ActionEvent事件的監聽器

tfName.addActionListener(al);

pfPW.addActionListener(al);

JPanel jpCenter = new JPanel(new GridLayout(2, 2));

jpCenter.add(lbName); //將元件加入JPanel子容器

jpCenter.add(tfName);

jpCenter.add(lbPW);

jpCenter.add(pfPW);

Container cp = getContentPane(); //取得內容面版

cp.add(jpCenter); //將元件加入面版

cp.add(lbEnter, BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //設定關閉視窗將預設結束程式

setSize(300, 120); //設定視窗框架大小

setVisible(true); //顯示視窗框架

}

public static void main(String args[]) {

new LabelFieldEX(); //宣告視窗框架物件

}

}


Jscrollpane jtextarea
JScrollPane, JTextArea

import javax.swing.*;

import javax.swing.text.*; //引用包含Document介面的套件

import javax.swing.event.*; //引用包含CaretListener介面的套件

import java.awt.*;

import java.awt.event.*; //引用處理事件的event套件

public class TextAreaEX extends JFrame{

JTextArea taText = new JTextArea(5, 30); //宣告文字區物件, 並定義為5列30行

JScrollPane spText = new JScrollPane(taText); //以文字區物件建立捲軸面版

JLabel lbText = new JLabel("文字區 :");

JLabel lbTextPos = new JLabel("顯示游標位置");

JLabel lbDocAct = new JLabel("顯示文字區內容的編輯動作");

//定義實作CaretListener介面回應CaretEvent事件的監聽器

CaretListener cl = new CaretListener(){

public void caretUpdate(CaretEvent e){

lbTextPos.setText("文字區游標位置 : " + e.getDot());

if(e.getDot() != e.getMark()) //判斷是否執行範圍選取

lbTextPos.setText(lbTextPos.getText()

+ " 選取範圍從 " + e.getDot() + "至" + e.getMark());

}

};


TextAreaEX(){

taText.setLineWrap(true); //設定文字區自動斷行

taText.setFont(new Font("Times-Roman", Font.BOLD, 15)); //設定使用的字型

taText.addCaretListener(cl); //註冊回應CaretEvent事件的監聽器

Document doc = taText.getDocument(); //取得文字區的Document物件

//註冊Document物件之事件的監聽器, 以回應文字內容的新增與刪除動作,

//並示範在呼叫addDocumentListener()時, 定義監聽器類別

doc.addDocumentListener(new DocumentListener(){

public void insertUpdate(DocumentEvent e) { //資料新增動作

lbDocAct.setText("資料新增至文字區");

}

public void removeUpdate(DocumentEvent e) { //資料刪除動作

lbDocAct.setText("移除文字區內容");

}

public void changedUpdate(DocumentEvent e) { }

});

//建立包含顯示文字區狀態之標籤的Box容器

Box bxShow = new Box(BoxLayout.Y_AXIS);

bxShow.add(lbTextPos);

bxShow.add(lbDocAct);

Container cp = getContentPane(); //取得內容面版

cp.setLayout(new FlowLayout(FlowLayout.LEFT)); //設定使用FlowLayout配置

cp.add(lbText); //將元件加入面版

cp.add(spText);

cp.add(bxShow);

//設定視窗關閉動作、視窗大小, 並顯示視窗

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(350, 230);

setVisible(true);

}

public static void main(String args[]) {

new TextAreaEX(); //建立視窗框架

}

}


Jcheckbox
JCheckBox

import javax.swing.*;

import java.awt.*;

import java.awt.event.*; //引用處理事件的event套件

import java.util.*;

public class CheckBoxEX extends JFrame{

JLabel lbTitle = new JLabel("選取項目 : ");

//宣告核取方塊物件, 並設定標籤文字

JCheckBox cbSpe1 = new JCheckBox("PHP"),

cbSpe2 = new JCheckBox("JSP"),

cbSpe3 = new JCheckBox("ASP"),

cbSpe4 = new JCheckBox("ASP.NET"),

cbSpe5 = new JCheckBox("Perl");

//定義並宣告回應ItemEvent事件的監聽器

ItemListener il = new ItemListener(){

LinkedList<JCheckBox> llSel = new LinkedList<JCheckBox>();

//宣告儲存狀態為選取的JCheckBox物件

public void itemStateChanged(ItemEvent e) {

JCheckBox cbSource = (JCheckBox) e.getSource(); //取得事件來源物件

//判斷觸發的為選取事件還是取消選取事件

if(e.getStateChange() == e.SELECTED)

llSel.add(cbSource);

else if(e.getStateChange() == e.DESELECTED)

llSel.remove(cbSource);

StringBuffer sbActionCommand = new StringBuffer("選取項目 : ");

//運用for迴圈輸出被選取之JCheckBox物件的動作命令字串

for(JCheckBox elm: llSel)

sbActionCommand.append(elm.getActionCommand() + ", ");

lbTitle.setText(sbActionCommand.toString());

//設定被選取lbText的顯示內容

}

};


CheckBoxEX(){

Box boxSpe = new Box(BoxLayout.X_AXIS); //建立放置核取方塊的Box

boxSpe.add(cbSpe1); //將元件放入Box容器

boxSpe.add(Box.createHorizontalStrut(10));

boxSpe.add(cbSpe2);

boxSpe.add(Box.createHorizontalStrut(10));

boxSpe.add(cbSpe3);

boxSpe.add(cbSpe4);

boxSpe.add(Box.createHorizontalStrut(10));

boxSpe.add(Box.createHorizontalStrut(10));

boxSpe.add(cbSpe5);

cbSpe1.addItemListener(il); //註冊回應ItemEvent事件的監聽器

cbSpe2.addItemListener(il);

cbSpe3.addItemListener(il);

cbSpe4.addItemListener(il);

cbSpe5.addItemListener(il);

Container cp = getContentPane(); //取得內容面版

cp.setLayout(new GridLayout(2, 1)); //設定運用GridLayout配置版面

cp.add(lbTitle); //將元件加入內容面版

cp.add(boxSpe);

//設定視窗預設的關閉動作、視窗大小, 並顯示視窗

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(350, 100);

setVisible(true);

}

public static void main(String args[]) {

new CheckBoxEX(); //建立視窗框架物件

}

}


Jpanel jradiobutton
JPanel, JRadioButton

import javax.swing.*;

import javax.swing.border.*;

import java.awt.*;

import java.awt.event.*; //引用處理事件的event套件

public class RadioEX extends JFrame{

JLabel lbTitle = new JLabel("設定對齊方式");

ButtonGroup bgHAlign = new ButtonGroup();

JRadioButton rbLeft = new JRadioButton("靠左");

JRadioButton rbCenter = new JRadioButton("置中");

JRadioButton rbRight = new JRadioButton("靠右");

//定義並宣告監聽器

ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent e){

String command = e.getActionCommand();

//取得觸發事件之元件的動作命令字串

//判斷動作命令字串以設定標籤內文字的對齊方式

if(command.equals("Left"))

lbTitle.setHorizontalAlignment(SwingConstants.LEFT);

else if(command.equals("Center"))

lbTitle.setHorizontalAlignment(SwingConstants.CENTER);

else if(command.equals("Right"))

lbTitle.setHorizontalAlignment(SwingConstants.RIGHT);

}

};


RadioEX(){

lbTitle.setVerticalAlignment(SwingConstants.CENTER);

//設定標籤內文字的垂直對齊方式

JPanel plHAlign =

new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 5));

//建立放置核取方塊的JPanel

plHAlign.add(rbLeft); //將選擇鈕加入JPanel內

plHAlign.add(Box.createHorizontalStrut(10));

plHAlign.add(rbCenter);

plHAlign.add(Box.createHorizontalStrut(10));

plHAlign.add(rbRight);

bgHAlign.add(rbLeft); //將選擇鈕加入ButtonGroup

bgHAlign.add(rbCenter);

bgHAlign.add(rbRight);

Border border = BorderFactory.createTitledBorder(

BorderFactory.createLineBorder(Color.gray, 2),

"設定文字的水平對齊方式"

, TitledBorder.LEFT, TitledBorder.TOP);

//宣告標題框線物件, 標題文字將靠左靠上對齊, 顏色為淺灰, 寬度為2

plHAlign.setBorder(border); //設定JPanel使用的框線

rbLeft.setActionCommand("Left"); //設定選擇鈕的動作命令字串

rbCenter.setActionCommand("Center");

rbRight.setActionCommand("Right");

rbLeft.addActionListener(al); //註冊回應ActionEvent事件的監聽器

rbCenter.addActionListener(al);

rbRight.addActionListener(al);

rbLeft.setSelected(true); //設定選取靠左對齊

Container cp = getContentPane(); //取得內容面版

cp.setLayout(new GridLayout(2, 1)); //設定使用GridLayout管理版面

cp.add(lbTitle); //將元件加入內容面版

cp.add(plHAlign);

//設定視窗預設的關閉動作、視窗大小, 並顯示視窗

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(350, 200);

setVisible(true);

}

public static void main(String args[]) {

new RadioEX(); //宣告視窗框架物件

}

}


Jcombobox
JComboBox

import javax.swing.*;

import javax.swing.event.*; //引用包含ItemListener介面的套件

import java.awt.*;

import java.awt.event.*; //引用處理事件的event套件

public class ComboEX extends JFrame{

//宣告建立組合方塊選項內容的字串

String[] strPay = {"信用卡", "現金", "刷卡", "轉帳", "支票"};

String[] strBank = {"郵局", "建華銀行", "中華商銀", "合作金庫"};

//以包含選項內容的字串建立組合方塊

JComboBox cmbPay = new JComboBox(strPay);

JComboBox cmbBank = new JComboBox(strBank);

JLabel lbSelPay = new JLabel(); //顯示組合方塊選取結果的標籤

JLabel lbSelBank = new JLabel();

//定義並宣告回應在組合方塊內

//按下 Enter 鍵觸發之ActionEvent的監聽器物件

ActionListener al = new ActionListener(){

public void actionPerformed(ActionEvent e){

boolean addItem = true;

String strInp =

(String) ((JComboBox)e.getSource()).getSelectedItem();

//取得被選取項目

//以for迴圈比對取得選項是否為組合方塊的選項

for(int i=0; i<cmbBank.getItemCount(); i++){

//比對選項字串

if(cmbBank.getItemAt(i).equals(strInp)){

addItem = false; //設定欲將字串加入

break;

}

}

//若取得選項不是組合方塊的選項則將選項插入為選取項目

if(addItem)

cmbBank.insertItemAt(strInp, 0); //插入第一個選項

}

};


ComboEX(){

cmbPay.setSelectedIndex(1); //設定選項的項目

cmbBank.setSelectedIndex(1);

cmbBank.setEditable(true); //設定輸入內容可編輯

ComboBoxEditor cmbeBank = cmbBank.getEditor();

//取得組合方塊內容編輯元件

cmbBank.configureEditor(cmbeBank, "選取或輸入銀行名稱");

//設定內容編輯元件的預設值

//定義並宣告回應ItemEvent事件的監聽器物件

cmbPay.addItemListener(new ItemListener(){

//回應改變選項狀態的動作

public void itemStateChanged(ItemEvent e){

if(e.getStateChange() == ItemEvent.SELECTED)

lbSelPay.setText(cmbPay.getSelectedItem()

+ "[" + cmbPay.getSelectedIndex() + "]");

}

});

cmbBank.addItemListener(new ItemListener(){

//回應改變選項狀態的動作

public void itemStateChanged(ItemEvent e){

if(e.getStateChange() == ItemEvent.SELECTED)

lbSelBank.setText(cmbBank.getSelectedItem()

+ "[" + cmbBank.getSelectedIndex() + "]");

}

});


//註冊回應cmbBank元件ActionEvent事件的監聽器

cmbBank.addActionListener(al);

//建立包含各元件的Box容器, 並將元件加入

Box bxPay = new Box(BoxLayout.X_AXIS);

bxPay.add(Box.createHorizontalStrut(10));

bxPay.add(new JLabel("付款方式 : ", JLabel.RIGHT));

bxPay.add(cmbPay);

bxPay.add(Box.createHorizontalStrut(10));

bxPay.add(new JLabel("選取結果 : ", JLabel.RIGHT));

bxPay.add(lbSelPay);

bxPay.add(Box.createHorizontalGlue());

//建立包含各元件的Box容器, 並將元件加入

Box bxBank = new Box(BoxLayout.X_AXIS);

bxBank.add(Box.createHorizontalStrut(10));

bxBank.add(new JLabel("匯入銀行 : ", JLabel.RIGHT));

bxBank.add(cmbBank);

bxBank.add(Box.createHorizontalStrut(10));

bxBank.add(new JLabel("選取結果 : ", JLabel.RIGHT));

bxBank.add(lbSelBank);

bxBank.add(Box.createHorizontalGlue());

Container cp = getContentPane(); //取得內容面版

cp.setLayout(new GridLayout(2, 1, 10, 10));

//設定內容面版使用GridLayout管理版面

cp.add(bxPay); //將Box容器加入內容面版

cp.add(bxBank);

//設定視窗預設的關閉動作、視窗大小, 並顯示視窗

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setSize(400, 100);

setVisible(true);

}

public static void main(String args[]) {

new ComboEX(); //宣告視窗框架物件

}

}


ad