Swing是用戶界面類,AWT是底層機制。
一、創建JFrame框架
注:Swing組件類都以“J”開頭,如 JButton,JFrame等,AWT組件不帶“J"。如果Swing組件和AWT組件一起用可能會導致視覺和行為的不一致。
現在,我們來創建一個空框架:
import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * GuiDemo01 * * @author wanghao * @create 2018-07-07 下午4:17 **/ public class DemoGuiOne { public static void main(String [] args){ // 聲明窗口對象 JFrame f = new JFrame("簡單的GUI窗口"); // 設置窗口寬高與坐標 f.setSize(300,500); f.setLocation(300,500); // 設置布局樣式 f.setLayout(new FlowLayout()); /** * * 布局管理器 * 1)容器中的組件的排放方式,就是布局. * 2)常見的布局管理器 * FlowLayout(流式布局管理器) * 從左到右的順序排列 * Panel默認的布局管理器 * BorderLayout(辯解布局管理器) * 東 南 西 北 中 * Frame 默認的布局管理器 * 不指定布局方式,默認 滿屏覆蓋,在添加一個 也是 滿屏覆蓋 * GridLayout (網格布局管理器) * 規則的矩陣 * CardLayout (卡片布局管理器) * 選項卡 * GridBagLayout(網格包布局管理器) * 非規則的矩陣 * 事件監聽機制組成 * 事件源: * 事件:Event * 監聽器:Listener * 時間處理:(引發事件后處理方式) * * 事件源:就是awt包或者swing包中的那些圖像界面組件. * 事件:每個事件源都有自己特定的對應時間和共性時間. * 監聽器:可以出發某一個事件的動作都已經封裝到監聽器中. */ JButton jButton1 = new JButton("我是按鈕1"); JButton jButton2 = new JButton("我是按鈕2"); JButton jButton3 = new JButton("我是按鈕3"); f.add(jButton1); f.add(jButton2); f.add(jButton3); f.addWindowListener(new MyWindow()); f.setVisible(true); } } /** * 因為接口WindowLinstener的方法被WindowAdapter實現,所以我們簡介繼承WindowAdapter,並復寫我們需要更改的方法。 */ class MyWindow extends WindowAdapter { @Override public void windowOpened(WindowEvent e) { System.out.println("窗口打開"); } @Override public void windowClosing(WindowEvent e) { // 點×關閉窗口 System.out.println("窗口關閉"); System.exit(0); } @Override public void windowActivated(WindowEvent e) { System.out.println("窗口激活"); } }
運行后的樣子如下:
插入鼠標點擊事件:
/* 事件監聽機制組成 * 事件源: * 事件:Event * 監聽器:Listener * 時間處理:(引發事件后處理方式) * * 事件源:就是awt包或者swing包中的那些圖像界面組件. * 事件:每個事件源都有自己特定的對應時間和共性時間. * 監聽器:可以出發某一個事件的動作都已經封裝到監聽器中. */ JButton jButton1 = new JButton("我是按鈕1"); JButton jButton2 = new JButton("我是按鈕2"); JButton jButton3 = new JButton("我是按鈕3"); jButton1.addActionListener(new ActionListener(){ @Override public void actionPerformed(ActionEvent e) { System.exit(10); System.out.println("按鈕干的退出"); } });
插入鍵盤按鍵事件:
// 鍵盤和鼠標事件 TextField textField = new TextField(); textField.addKeyListener(new KeyAdapter() { /** * keyPressed:鍵盤按下,未釋放 * keyTyped:鍵盤按下,然后釋放。 * 對於按下鍵和釋放鍵事件,getKeyCode 方法返回該事件的 keyCode。對於鍵入鍵事件, * getKeyCode 方法總是返回 VK_UNDEFINED。 * * 所以鍵入鍵事件只能靠 getKeyChar 來區分了 * * KeyPressed是鍵被按下,KeyReleased是鍵被彈起,這兩個都是更底層一些的事件。 KeyTypede是指有字符被輸入,比如按住shift,再按A鍵,如果當時Caps Lock不亮,就產生一個輸入大寫A的事件。 */ @Override public void keyTyped(KeyEvent e) { super.keyTyped(e); System.out.println("是指字符被輸入。Code:"+e.getKeyCode()); System.out.println("是指字符被輸入。Char:"+e.getKeyChar()); } @Override public void keyPressed(KeyEvent e) { super.keyPressed(e); System.out.println("是指鍵被按下。Code:"+e.getKeyCode()); System.out.println("是指鍵被按下。Char:"+e.getKeyChar()); } @Override public void keyReleased(KeyEvent e) { super.keyReleased(e); System.out.println("是指鍵被彈起。Code:"+e.getKeyCode()); System.out.println("是指鍵被彈起。Char:"+e.getKeyChar()); } }); f.add(textField);
插入自定義布局:
import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; /** * 點擊按鈕改變顏色 * * @author wanghao * @create 2018-07-07 下午4:57 **/ public class ColorWindow { public static void main(String[] args) { final Frame f=new Frame("XXX"); f.setLayout(null); f.setSize(500,400); f.setLocation(300,200); // 設置背景顏色 初始背景顏色黃色 f.setBackground(new Color(255, 255, 0)); Button red=new Button("紅色"); red.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("改變背景顏色"); Color bgColor = new Color(255, 0, 0); // 更改背景顏色為紅色 f.setBackground(bgColor); } }); Button green=new Button("綠色"); green.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("改變背景顏色"); Color bgColor = new Color(0, 255, 0); // 更改背景顏色綠色 f.setBackground(bgColor); } }); Button Blue=new Button("蘭色"); Blue.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("改變背景顏色"); Color bgColor = new Color(0, 0, 255); // 更改背景顏色藍色 f.setBackground(bgColor); } }); Button revert=new Button("還原"); revert.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { System.out.println("改變背景顏色"); Color bgColor = new Color(255, 255, 0); // 更改背景顏色藍色 f.setBackground(bgColor); } }); Button title=new Button("設置背景顏色"); title.setBounds(0,20,250,50); //設置按鈕尺寸 red.setBounds(0,70,100,50); green.setBounds(0,120,100,50); Blue.setBounds(0,170,100,50); revert.setBounds(0,220,100,50); f.add(title); f.add(red); f.add(green); f.add(Blue); f.add(revert); f.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { System.out.println("我關了"); System.exit(0); } }); f.setVisible(true); } }
運行后的效果如下: