涉及的兩個常用的包:java:Awt 和javax:Swing
組織圖:
與Frame是Awt包中的,JFrame是Swing包中的:兩者區別如下
Frame 不同,當使用者試圖關閉視窗時,JFrame 知道如何進行響應。使用者關閉視窗時,預設的行為只是簡單地隱藏 JFrame。要更改預設的行為,可調用方法
setDefaultCloseOperation(int)。
操作
建立圖形化介面:
1,建立frame表單。
Frame f = new Frame("my awt");
2,對表單進行基本設定。或者還可以直接設定f.setBounds(300,100,600,500);,四個參數分別是指:表單長、寬;頂點距離左邊的距離和距離上邊的距離
f.setSize(500,400);
f.setLocation(300,200);
f.setLayout(new FlowLayout());//設定布局方式流式布局
3,定義組件。
Button b = new Button("按鈕");
4,將組件通過表單的add方法添加到表單中。
f.add(b);
f.addWindowListener(new WindowAdapter() { //定義監聽器及其事件處理
public void windowClosing(WindowEvent e) {
System.out.println("我關");
System.exit(0);
}
public void windowActivated(WindowEvent e) {
System.out.println("我活了。");
}
public void windowOpened(WindowEvent e) {
System.out.println("我被開啟了,hahahhahah"); }
}
);
5,讓表單顯示,通過setVisible(true)
f.setVisible(true);
事件監聽機制的特點: 1,事件來源。 2,事件。 3,監聽器。 4,事件處理。
事件來源:就是awt包或者swing包中的那些圖形介面組件。
事件:每一個事件來源都有自己特有的對應事件和共性事件。
監聽器:將可以觸發某一個事件的動作(不只一個動作)都已經封裝到了監聽器中。
我們要做的事情是,就是對產生的動作進行處理。(前三項在java中都已經定義好了。直接擷取其對象來用就可以了。)
import java.awt.*;
import java.awt.event.*;
class MouseAndKeyEvent {
private Frame f;
private Button but;
private TextField tf;
MouseAndKeyEvent() {
init();
}
public void init() {
f = new Frame("my frame");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf = new TextField(20);
but = new Button("my button");
f.add(tf);
f.add(but);
myEvent();
f.setVisible(true);
}
private void myEvent() {
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
tf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
int code = e.getKeyCode(); 按下某個鍵獲得的常量值
if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)) {
System.out.println(code+".....是非法的");
e.consume(); 這個方法就是將預設的處理辦法不能正常觸發
}
}
});
//給But添加一個鍵盤監聽。
but.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e) {
if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER) KeyEvent.VK_ENTER:常量
//System.exit(0);
System.out.println("ctrl+enter is run");
//System.out.println(KeyEvent.getKeyText(e.getKeyCode())+"...."+e.getKeyCode());
}
});
but.addMouseListener(new MouseAdapter() {
private int count = 1;
private int clickCount = 1;
public void mouseEntered(MouseEvent e) {
System.out.println("滑鼠進入到該組件"+count++);
}
public void mouseClicked(MouseEvent e) {
if(e.getClickCount()==2)
System.out.println("雙擊動作"+clickCount++);
}
});
}
public static void main(String[] args) {
new MouseAndKeyEvent();
}
}
...................................................................................................................
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();
}
}
.......................................................................................................
package mymenu;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class MyMenuTest {
private Frame f;
private MenuBar bar;
private TextArea ta;
private Menu fileMenu;
private MenuItem openItem,saveItem,closeItem;
private FileDialog openDia,saveDia;
private File file;
MyMenuTest() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300,100,650,600);
bar = new MenuBar();
ta = new TextArea();
fileMenu = new Menu("檔案");
openItem = new MenuItem("開啟");
saveItem = new MenuItem("儲存");
closeItem = new MenuItem("退出");
fileMenu.add(openItem);
fileMenu.add(saveItem);
fileMenu.add(closeItem);
bar.add(fileMenu);
f.setMenuBar(bar);
openDia = new FileDialog(f,"我要開啟",FileDialog.LOAD);
saveDia = new FileDialog(f,"我要儲存",FileDialog.SAVE);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent() {
saveItem.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.flush();
bufw.close();
}
catch (IOException ex) {
throw new RuntimeException();
}
}
});
openItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile(); // System.out.println(dirPath+"..."+fileName);
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("讀取失敗"); }
}
});
closeItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public static void main(String[] args) {
new MyMenuTest();
}
}
如何製作可以雙擊執行的jar包呢?
1,將多個類封裝到了一個包(package)中。
2,定義一個jar包的配置資訊。 定義一個檔案a.txt 。檔案內容內容為: Main-Class:(空格)包名.類名(斷行符號)
3,打jar包。 jar -cvfm my.jar a.txt 包名
4,通過winrar程式進行驗證,查看該jar的設定檔中是否有自訂的配置資訊。
5,通過工具檔案類型--jar類型檔案,通過進階,定義該jar類型檔案的開啟動作的關聯程式。 jdk\bin\javaw.exe -jar
6,雙擊試試!。