1. GUIjava
Graphical user interface(圖形用戶接口):用圖形的方式,來顯示計算機操做的界面,這樣更加直觀設計模式
CLI:command line User interface(命令行用戶接口):經常使用的Dos命令行操做,須要記住一些經常使用的命令,操做不直觀app
java.awt:Abstract Window ToolKit(抽象窗口工具包):須要調用本地系統的方法實現功能,屬於重量級控件ide
javax.swing:在AWT的基礎上,創建的一套圖形界面系統,提供了不少的組件,並且徹底由java實現,增長了移植性,屬於輕量級控件工具
2. Framespa
public void setTitle(String s):設置窗體的標題命令行
public void setSize(width, height):設置窗體的大小設計
public void setLocation(x, y):設置窗體的區域code
public void setBounds(x, y, width, height):同時設置窗體的大小和所在區域orm
public void setVisible(true):設置窗體是否可見
public class GUIDemo { public static void main(String[] args){ Frame f = new Frame(); // Frame f = new Frame("窗口標題"); f.setTitle("frame"); // 設置窗體標題 f.setSize(400, 300); // 單位:像素,設置窗體的大小 /* 等同於: * Dimension d = new Dimension(400,300); * f.setSize(400, 300); */ f.setLocation(400,200); // 設置窗體所在的區域 /* 等同於 * Point p = new Point(400, 200); * f.setLocation(p); */ /* * f.setBounds(x, y, width, height); * 等同於: * f.setLocation(x, y); * f.setSize(width, height); */ f.setVisible(true); } }
3. 事件監聽機制
事件源:事件發生的地方
事件:要發生的事情
事件處理:針對發生的事情作出的處理方案
事件監聽:把事件源和事件聯繫起來
public class GUIDemo { public static void main(String[] args){ Frame f = new Frame(); // Frame f = new Frame("窗口標題"); f.setTitle("frame"); // 設置窗體標題 f.setSize(400, 300); // 單位:像素,設置窗體的大小 /* 等同於: * Dimension d = new Dimension(400,300); * f.setSize(400, 300); */ f.setLocation(400,200); // 設置窗體所在的區域 /* 等同於 * Point p = new Point(400, 200); * f.setLocation(p); */ /* * f.setBounds(x, y, width, height); * 等同於: * f.setLocation(x, y); * f.setSize(width, height); */ f.addWindowListener(new WindowListener(){ @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } }); f.setVisible(true); } }
4. 適配器設計模式
接口的方法比較多,也得把其餘的實現也提供了,即便是空實現
解決方案:接口(方法比較多)------>適配器(實現接口,空實現)------->實現類(用哪一個重寫哪一個)
針對上例的改進版:
f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } });
例:文本框的數據轉移到文本域
public class FrameDemo { public static void main(String[] args){ Frame f = new Frame("數據轉移"); f.setBounds(400, 200, 400, 300); f.setLayout(new FlowLayout()); final TextField tf = new TextField(20); Button bu = new Button("heoo"); final TextArea ta = new TextArea(10, 40); f.add(tf); f.add(bu); f.add(ta); f.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e){ System.exit(0); } }); bu.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String tf_str = tf.getText().trim(); tf.setText(""); // ta.setText(tf_str);; ta.append(tf_str + "\r\n"); tf.requestFocus(); // 獲取光標 } }); f.setVisible(true); } }
效果圖:
例:顏色變換
public class ColorTranverce { public static void main(String[] args) { final Frame f = new Frame(); f.setBounds(400, 200, 400, 300); f.setLayout(new FlowLayout()); Button redButton = new Button("red"); /* * 按鈕的動做實現更改背景色 redButton.addActionListener(new ActionListener(){ * * @Override public void actionPerformed(ActionEvent e) { // TODO * Auto-generated method stub f.setBackground(Color.red); } * * }); */ // 對按鈕田間鼠標點擊事件 redButton.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { f.setBackground(Color.red); } @Override public void mouseEntered(MouseEvent e){ f.setBackground(Color.gray); } }); f.add(redButton); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); f.setVisible(true); } }
文本框只能輸入數字和字符案例
public class NumbericDemo { public static void main(String[] args){ Frame f = new Frame("number"); f.setBounds(400, 200, 400, 300); f.setLayout(new FlowLayout()); Label label = new Label("please input your qq number"); TextField tf = new TextField(40); f.add(label); f.add(tf); tf.addKeyListener(new KeyAdapter(){ @Override public void keyPressed(KeyEvent e){ char ch = e.getKeyChar(); if(!(ch >= '0' && ch <= '9')){ e.consume(); // 事件取消 } } }); f.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e){ System.exit(0); } }); f.setVisible(true); } }
5. 菜單組件
5.1 單級菜單
public class MenuDemo { public static void main(String[] args){ Frame f = new Frame("單極菜單"); f.setBounds(400, 200, 400, 300); f.setLayout(new FlowLayout()); MenuBar mb = new MenuBar(); Menu m = new Menu("file"); MenuItem mi = new MenuItem("exit"); m.add(mi); mb.add(m); f.setMenuBar(mb); mi.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); f.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e){ System.exit(0); } }); f.setVisible(true); } }
5.2 多級菜單
public class MenuDemo { public static void main(String[] args){ final Frame f = new Frame("title"); // 匿名內部類調用的變量必須使用final修飾 f.setBounds(400, 200, 400, 300); f.setLayout(new FlowLayout()); MenuBar mb = new MenuBar(); Menu m1 = new Menu("file"); Menu m2 = new Menu("change title"); final MenuItem mi1 = new MenuItem("t"); final MenuItem mi2 = new MenuItem("g"); MenuItem mi3 = new MenuItem("return titile"); MenuItem mi4 = new MenuItem("open notepad"); MenuItem mi5 = new MenuItem("exit"); final String name = f.getTitle(); m2.add(mi1); m2.add(mi2); m2.add(mi3); m1.add(m2); m1.add(mi4); m1.add(mi5); mb.add(m1); f.setMenuBar(mb); mi1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub f.setTitle(mi1.getLabel()); } }); mi2.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub f.setTitle(mi2.getLabel()); } }); mi3.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub f.setTitle(name); } }); mi4.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub Runtime rt = Runtime.getRuntime(); try { rt.exec("notepad"); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }); mi5.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub System.exit(0); } }); f.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e){ System.exit(0); } }); f.setVisible(true); } }
效果圖: