GUI(Graphical User Interface)圖形化介面編程總結
Java.Awt:Abstract Window ToolKit(抽象視窗工具包)
Javax.Swing:
圖形化介面基本設定:
設定表單大小:setSize(長,寬);
設定表單位置:setLocation(距離左,距離上);setBounds(長,寬,距離左,距離上);
設定布局:setLayout(new FlowLayout());
使表單可見:setVisible(true);
事件監聽機制:
事件監聽機制的特點:
1,事件來源。
2,事件。
3,監聽器。
4,事件處理。
事件來源:就是awt包或者swing包中的那些圖形介面組件。
事件:每一個事件來源都有自己特有的對應事件和共性事件。
監聽器:將可以觸發某一個事件的動作(不只一個動作)都已經封裝到了監聽器中。
以上三者,在java中都已經定義好了。
直接擷取其對象來用就可以了。
我們要做的事情是,就是對產生的動作進行處理
Eg:編寫程式,練習圖形化介面編程。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyWindowDemo
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okBut;
MyWindowDemo()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(60);
but = new Button("轉到");
ta = new TextArea(25,70);
d = new Dialog(f,"提示資訊-self",true);
d.setBounds(400,200,240,150);
d.setLayout(new FlowLayout());
lab = new Label();
okBut = new Button("確定");
d.add(lab);
d.add(okBut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okBut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
showDir();
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
private void showDir()
{
String dirPath = tf.getText();
File dir = new File(dirPath);
if(dir.exists() && dir.isDirectory())
{
ta.setText("");
String[] names = dir.list();
for(String name : names)
{
ta.append(name+"\r\n");
}
}
else
{
String info = "您輸入的資訊:"+dirPath+"是錯誤的。請重輸";
lab.setText(info);
d.setVisible(true);
}
}
public static void main(String[] args)
{
new MyWindowDemo();
}
}
菜單:
MenuBar 菜單整體;Menu 包含於MenuBar中;MenuItem包含於Menu中。
以上3個關係添加用add();
將MenuBar放進Frame中用setMenuBar();
FileDialog.LOAD:開啟的mode
FileDialog.Save:儲存的mode
練習:一個簡易的記事本
/**
寫一個關於記事本的小程式
@author田建
@version v1.1
*/
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class MyMenuDemo
{
private Frame f;
private TextArea ta;
private MenuBar mb;
private Menu me;
private MenuItem miNew,miSave,miLoad;
private File file;
private FileDialog openDia,saveDia;
MyMenuDemo()
{
init();
}
public void init()
{
f=new Frame("田建--記事本");
f.setBounds(300,100,650,600);
ta=new TextArea();
//ta.setBounds(302,105,400,300);
//f.setLayout(new FlowLayout());//為什麼設定成了流式布局之後再設定文本地區沒有效果
mb=new MenuBar();
me=new Menu("檔案(F)");
miNew=new MenuItem("建立");
miSave=new MenuItem("儲存(S)");
miLoad=new MenuItem("開啟(O)");
mb.add(me);
me.add(miNew);
me.add(miLoad);
me.add(miSave);
f.setMenuBar(mb);
f.add(ta);
openDia=new FileDialog(f,"開啟檔案",FileDialog.LOAD);
saveDia=new FileDialog(f,"儲存檔案",FileDialog.SAVE);
myEvent();
f.setVisible(true);
}
public void myEvent()
{
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
miLoad.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
openDia.setVisible(true);
String dirPath=openDia.getDirectory();
String fileName=openDia.getFile();
if(dirPath==null||fileName==null)
return;
ta.setText("");
file=new File(dirPath,fileName);
try
{
BufferedReader bufr=new BufferedReader(new FileReader(file));
String line=null;
while((line=bufr.readLine())!=null)
{
ta.append(line+"\r\n");
}
bufr.close();
}
catch(IOException ex)
{
throw new RuntimeException("讀取失敗");
}
}
});
miSave.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(file==null)
{
saveDia.setVisible(true);
String dirPath=saveDia.getDirectory();
String fileName=saveDia.getFile();
if(dirPath==null||fileName==null)
return;
file=new File(dirPath,fileName);
}
try
{
BufferedWriter bufw=new BufferedWriter(new FileWriter(file));
String text=ta.getText();
bufw.write(text);
bufw.close();
}
catch(IOException ex)
{
throw new RuntimeException("");
}
}
});
}
public static void main(String[] args)
{
new MyMenuDemo();
}
}
打jar包的過程:
1、編譯包 jar –cvf jar名 包名
2、寫一個檔案main-class: 包名.類名【加個