GUI(Graphical User Interface)圖形化用戶接口
CLI(Command Line User Interface)命令行用戶接口
java爲GUI提供的對象都在java.awt和java.swing兩個包中
java.awt:(abstract window toolkit(抽象窗口工具包))
需要調用本地系統方實現功能,是重量級控件
java.swing:在awt的基礎上,建立的一套圖形界面系統,其中
提供了更多的組件,而且完全由java實現,增加移植性,屬於輕量級
控件
Component組件
container:容器,是一個特殊的組件,該組件可以通過add方法添加
其他組件進來
容器中組件的排版方式就是佈局:
常見的佈局管理器:
FlowLayout:流式佈局 默認居中
.從左到右的順序排列
.Panel默認的佈局管理器
BorderLayout:邊界式佈局 默認居中
.東南西北中
.Frame默認的佈局管理器
GridLayout:網格佈局
.規則的矩陣
CradleLayout:卡片佈局
.選項卡
GridBagLayout:網格包佈局
.非規則的矩陣
最厲害的是座標式佈局, Eclipse可以安裝插件輕鬆佈局
cmd命令:
md 目錄名 //創建目錄
rd 目錄名 //刪除目錄
創建圖形化界面步驟:
1.創建Frame窗體
2.對窗體進行基本設置(比如大小,位置,佈局等)
3.定義組件
4.將組件通過窗體的add方法添加到窗體中
5.讓窗體顯示
事件監聽機制
事件源(組件)
事件(Event)
監聽器(Listener)
事件處理(引發事件後的處理方式)
抽象類沒有抽象方法,該類只是爲了不讓創建對象,
因爲創建對象沒有意義.
窗體事件
事件和圖形化界面分離出來
按鈕事件
/*
按鈕就是事件源
那麼選擇哪個監聽器呢?
通過關閉窗體示例瞭解到,想要知道哪個組件具備什麼樣的監聽器
需要查看該組件對象的功能
通過查詢Button的描述,發現按鈕支持一個特有的監聽addActionListener
*/
鼠標事件
鍵盤事件
/*
創建圖形化界面步驟:
1.創建Frame窗體
2.對窗體進行基本設置(例如大小,位置,佈局方式等)
3.定義組件
4.將組件通過窗體的add方法添加到窗體中
5.讓窗體顯示
讓事件和圖形化界面分離出來
窗體事件
按鈕事件
鼠標事件
鍵盤事件
*/
import java.awt.*;
import java.awt.event.*;
//窗體類
class MyFrame extends Frame
{
//定義該窗體中所需的控件
private Button b;
//構造函數 初始化窗體
public MyFrame(){
init();
}
//初始化
private void init(){
//setBounds(x,y,w,h);==setSize(w,h)+setLacation(x,y);
//設置組件基本屬性
//Frame
setTitle("my first program"); //設置標題
setBounds(300,200,500,400);//設置位置以及大小
setLayout(new FlowLayout());//設置佈局
//Button
b=new Button("first button");
//添加控件
add(b);//添加組件
//調用事件方法
windowEvent();
buttonEvent();
mouseEvent();
keyEvent();
//顯示窗體
setVisible(true);
}
//窗體事件
private void windowEvent(){
addWindowListener(new WindowAdapter(){
// void windowActivated(WindowEvent e)
// 激活窗口時調用。
public void windowActivated(WindowEvent e){
sop("我活動了");
}
// void windowClosing(WindowEvent e)
// 窗口正處在關閉過程中時調用。
public void windowClosing(WindowEvent e){
sop("正在關閉");
System.exit(0);
}
// void windowOpened(WindowEvent e)
// 已打開窗口時調用。
public void windowOpened(WindowEvent e){
sop("窗口打開");
}
});
}
//按鈕事件
private void buttonEvent(){
b.addActionListener(new ActionListener(){
//按鈕活動事件
public void actionPerformed(ActionEvent e){
// sop("按鈕關閉");
// System.exit(0);
}
});
}
//鼠標事件
private void mouseEvent(){
b.addMouseListener(new MouseAdapter(){
//鼠標移進
public void mouseEntered(MouseEvent e){
sop("鼠標移動進按鈕了");
}
//鼠標單機
public void mouseClicked(MouseEvent e){
//getClickCount判斷 實現雙擊
if(e.getClickCount()==2)
sop("鼠標被雙擊");
else
sop("鼠標被單機");
}
});
}
//鍵盤事件
private void keyEvent(){
b.addKeyListener(new KeyAdapter(){
//鍵盤按下
public void keyPressed(KeyEvent e){
//輸出文本形式的鍵 和鍵碼
sop(KeyEvent.getKeyText(e.getKeyCode())+" "+e.getKeyCode());
// boolean isControlDown()
//返回 Control 修飾符在此事件上是爲 down。
//組合鍵
if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_B)
sop("按下了ctrl+b 組合鍵");
}
});
}
private void sop(Object o){
System.out.println(o);
}
}
//程序運行入口
class Demo1
{
public static void main(String[] args)
{
new MyFrame();
}
}
/*
練習 文本框只能輸入數字,要使用consume()方法
*/
/*
練習 文本框只能輸入數字
*/
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{
private TextField tf;
public MyFrame(){
init();
}
private void init(){
setBounds(300,200,500,400);
setLayout(new FlowLayout());
setTitle("文本框輸入練習");
tf=new TextField(20);
add(tf);
keyEvent();
windowEvent();
setVisible(true);
}
//窗體事件
private void windowEvent(){
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
//鍵盤事件
private void keyEvent(){
tf.addKeyListener(new KeyAdapter(){
//鍵盤按下
public void keyPressed(KeyEvent e){
int code=e.getKeyCode();
//判斷是否在0-9
if(!(code>=0x30 && code<=0x39))
e.consume();//不是的話就不處理
}
});
}
}
class Demo2
{
public static void main(String[] args)
{
new MyFrame();
}
}
對話框(Dialog)
/*
練習 輸入目錄,列出目錄的列表
*/
/*
文本框輸入目錄 轉到
文本域輸出列表
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFrame extends Frame
{
private TextField tf; //文本框
private Button b; //轉到按鈕
private TextArea ta; //文本域
private Dialog d; //對話框
private Label l; //標籤
private Button sure; //確定按鈕
public MyFrame(){
super("輸出文件列表");
init();
}
//初始化
private void init(){
setBounds(300,200,500,500);
setLayout(new FlowLayout());
tf=new TextField(50);
b=new Button("轉到");
ta=new TextArea(28,60);
l=new Label();
sure=new Button("確定");
d=new Dialog(this,"溫馨提示:",true);
d.setBounds(400,400,300,100);
d.setLayout(new FlowLayout());
d.add(l);
d.add(sure);
add(tf);
add(b);
add(ta);
windowEvent();
keyEvent();
buttonEvent();
setVisible(true);
}
//窗體事件
private void windowEvent(){
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
d.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
d.setVisible(false);
}
});
}
//鍵盤事件
private void keyEvent(){
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==10)
showDir();
}
});
}
//按鈕事件
private void buttonEvent(){
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
showDir();
}
});
sure.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
d.setVisible(false);
}
});
}
//展示目錄
private void showDir(){
String filepath=tf.getText();
File file=new File(filepath);
if(file.exists() && file.isDirectory()){
ta.setText("");
tf.setText("");
String []strs=file.list();
for (String s : strs )
{
ta.append(s+"\r\n");
}
}else{
l.setText("該目錄"+filepath+"不存在,請重新輸入");
tf.setText("");
d.setVisible(true);
}
}
}
class Demo3
{
public static void main(String[] args)
{
new MyFrame();
}
}
菜單:
MenuBar 菜單欄
Menu 菜單
MunuItem 菜單項
菜單是菜單項的子類
文件對話框(FileDialog)
/*
打開文件
保存文件
簡單記事本程序
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyFrame extends Frame
{
private MenuBar filemenu;
private Menu file;
private MenuItem open;
private MenuItem save;
private Menu zicaidan;
private MenuItem zicaidanxiang;
private FileDialog of;
private FileDialog sf;
private TextArea ta;
private Dialog tishi;
private Label l;
private String path;
private String filename;
public MyFrame(){
super("打開與保存文件");
init();
}
//初始化
private void init(){
setBounds(300,200,500,500);
filemenu=new MenuBar();
file=new Menu("File");
open=new MenuItem("open");
save=new MenuItem("save");
zicaidan=new Menu("子菜單");
zicaidanxiang=new MenuItem("子菜單項");
ta=new TextArea(30,60);
tishi=new Dialog(this,"溫馨提示",true);
l=new Label();
tishi.setBounds(300,200,300,100);
tishi.add(l);
file.add(open);
file.add(save);
file.add(zicaidan);
zicaidan.add(zicaidanxiang);
this.setMenuBar(filemenu);
filemenu.add(file);
windowEvent();
menuItemEvent();
add(ta);
setVisible(true);
}
private void windowEvent(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
tishi.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
tishi.setVisible(false);
}
});
}
private void menuItemEvent(){
//打開
open.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
of=new FileDialog(MyFrame.this,"打開文件",FileDialog.LOAD);
of.setBounds(300,200,400,400);
of.setVisible(true);
path=of.getDirectory();
filename=of.getFile();
if(path!=null || filename!=null)
read();
}
});
//保存
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(of!=null){
write();
}else{
if(sf==null){
sf=new FileDialog(MyFrame.this,"保存文件",FileDialog.SAVE);
sf.setBounds(300,200,400,400);
sf.setVisible(true);
path=sf.getDirectory();
filename=sf.getFile();
if(path!=null && filename!=null)
write();
}else{
path=sf.getDirectory();
filename=sf.getFile();
if(path!=null && filename!=null)
write();
}
}
}
});
}
//讀取
private void read(){
File f=new File(path,filename);
BufferedReader br=null;
ta.setText("");
try
{
br=new BufferedReader(new FileReader(f));
String line=null;
while((line=br.readLine())!=null){
ta.append(line+"\r\n");
}
}
catch (IOException o)
{
l.setText("讀取失敗");
tishi.setVisible(true);
}
finally
{
try
{
if(br!=null)
br.close();
}
catch (IOException e)
{
l.setText("關閉流異常");
tishi.setVisible(true);
}
}
}
//寫入
private void write(){
File f=new File(path,filename);
BufferedWriter br=null;
try
{
br=new BufferedWriter(new FileWriter(f));
String text=ta.getText();
br.write(text);
br.flush();
}
catch (IOException o)
{
l.setText("讀取失敗");
tishi.setVisible(true);
}
finally
{
try
{
if(br!=null)
br.close();
}
catch (IOException e)
{
l.setText("關閉流異常");
tishi.setVisible(true);
}
}
}
}
class Demo4
{
public static void main(String[] args)
{
new MyFrame();
}
}
雙擊執行jar程序
創建個清單文件list.txt
寫入 Main-Class:空格包名.類名回車
注意空格,回車必須要
然後jar -cvfm jar名稱 list.txt 類名
電腦系統盤目錄-工具-文件夾選項-文件類型-jar-高級
-新建open -值爲安裝目錄的javaw.exe目錄 -jar(注意後面的-jar
是在exe的後面空格隔開)
—————————— ASP.Net+Android+IOS開發、.Net培訓、期待與您交流!——————————