AWT知识点
1.AWT
2.1、Awt介紹 抽象的窗口工具
1.包含了很多的類和接口! GUI:圖形用戶界面 Eeclipse: java
2.元素:窗口,按鈕,文本框
3.javaawt
4.組件: component 按鈕:button 文本域:TextArea 容器: Container
按鈕需要存放在容器中 add
容器分兩部分:1. Windows窗口 2.面板Panel
Windows: 1.Frame 窗口
2.Dialog 彈窗
面板:Applet
2.2組件和容器
1.Frame
package lesson01; ? import java.awt.*; ? //GUI的第一個界面 public class TextFrame {public static void main(String[] args){//Frame JDK, 查源碼!Frame frame = new Frame("我的第一個Java圖像界面窗口");//需要設置可見性frame.setVisible(true); ?//設置窗口大小frame.setSize(400,400); ?//設置背景顏色 Colorframe.setBackground(Color.BLACK); ?//彈出的初始位置frame.setLocation(200,200); ?//設置大小固定frame.setResizable(false);} }多個窗口
package lesson01; ? import java.awt.*; ? public class TextFrame02 {public static void main(String[] args) {//展示多個窗口 newMyFrame myFrame1 = new MyFrame(100, 100, 200, 200, Color.blue);MyFrame myFrame2 = new MyFrame(300, 100, 200, 200, Color.yellow);MyFrame myFrame3 = new MyFrame(100, 300, 200, 200, Color.red);MyFrame myFrame4= new MyFrame(300, 300, 200, 200, Color.MAGENTA);} } class MyFrame extends Frame{static int id = 0;//可能存在多個窗口,所以說我們需要一個計數器 ?public MyFrame(int x,int y,int w,int h,Color color){super("MyFrame+"+(++id));setBackground(color);setVisible(true);setBounds(x,y,w,h);} ? ? }2.面板 Panel
解決了我們的關閉事件!
package lesson01; ? import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? //Panel 可以看成是一個空間,但是不能單獨存在 public class TextPanel {public static void main(String[] args) {Frame frame = new Frame();//布局的概念Panel panel = new Panel();//設置布局frame.setLayout(null); ?//坐標frame.setBounds(300,300,500,500);frame.setBackground(new Color(40,161,35)); ?//Panel設置坐標,相對于framepanel.setBounds(50,50,400,400);panel.setBackground(new Color(171, 16, 0)); ?//frame.add(panel)frame.add(panel); ?frame.setVisible(true); ?//監聽事件,監聽窗口關閉事件 System.exit(0)//適配器模式:frame.addWindowListener(new WindowAdapter() {@Override//窗口點擊關閉的時候,需要做的事情public void windowClosing(WindowEvent e) {super.windowClosing(e);//結束程序System.exit(0);}});} }3.布局管理器
-
流式布局
package lesson01; ? import java.awt.*; ? public class TestFlowLayout {public static void main(String[] args) {//創建一個新的窗口Frame frame = new Frame(); ?//組件-按鈕Button button1 = new Button("button1");Button button2 = new Button("button2");Button button3 = new Button("button3"); ?//設置為流式布局時frame.setLayout(new FlowLayout()); ?//設置一個大小frame.setSize(200,200);frame.setVisible(true);//把按鈕添加上去frame.add(button1);frame.add(button2);frame.add(button3);} } -
東西南北中
package lesson01; ? import java.awt.*; ? public class TestBorderLayout {public static void main(String[] args){Frame frame = new Frame("TestBorderLayout"); ?Button east = new Button("East");Button west = new Button("West");Button south = new Button("South");Button north = new Button("North");Button center = new Button("Center");frame.add(east,BorderLayout.EAST);frame.add(west,BorderLayout.WEST);frame.add(south,BorderLayout.SOUTH);frame.add(north,BorderLayout.NORTH);frame.add(center,BorderLayout.CENTER);frame.setSize(200,200);frame.setVisible(true); ?} } -
表格布局 Grid
題目:運用表格布局,東西南北中布局,流式布局來完成
思考:
-
一個窗口
-
四個面板
-
10個按鈕
總結:
1.Frame 是一個頂級窗口
2.Panel無法單獨顯示,必須添加到容器當中
3.布局管理器:
1.流式布局FlowLayout
2.東西南北中BorderLayout
3.表格布局GridLayout
4.大小 setSize 定位 setLocation
背景顏色 setBackground
可見性 setVisible
監聽 addWindowListener
//監聽事件,監聽窗口關閉事件 System.exit(0)
? //適配器模式:frame.addWindowListener(new WindowAdapter() {@Override//窗口點擊關閉的時候,需要做的事情public void windowClosing(WindowEvent e) {super.windowClosing(e);//結束程序System.exit(0);}});5.事件監聽
package lesson02; ? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? public class TestActionEvent {public static void main(String[] args) {//按下按鈕的時候觸發一些事件Frame frame = new Frame();Button button = new Button(); ?//因為addActionListener 需要一個ActionListener,所以我們構造一個ActionListenerMyActionListener myActionListener = new MyActionListener();button.addActionListener(myActionListener);frame.add(button,BorderLayout.CENTER);frame.pack();//大小自適應windowClose(frame);//關閉窗口frame.setVisible(true);}//關閉窗口的事件private static void windowClose(Frame frame){frame.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {System.exit(0);}});} } //事件監聽 class MyActionListener implements ActionListener{ ?@Overridepublic void actionPerformed(ActionEvent e) {System.out.println("aaa,bbb,ccc");} }多個按鈕,共享一個事件
package lesson02; ? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ? public class TestActionEvent02 {public static void main(String[] args) {//兩個按鈕實現同一個監聽//開始 停止Frame frame = new Frame("開始-停止");Button button1 = new Button("start");Button button2 = new Button("stop"); ?button2.setActionCommand("button2-stop");MyMonitor myMonitor = new MyMonitor();button1.addActionListener(myMonitor);button2.addActionListener(myMonitor); ?frame.add(button1,BorderLayout.NORTH);frame.add(button2,BorderLayout.SOUTH);frame.pack();frame.setVisible(true);} } class MyMonitor implements ActionListener{ ?@Overridepublic void actionPerformed(ActionEvent e) {//e.getActionCommand()獲取按鈕上的一些信息//可以多個按鈕只寫一個監聽類System.out.println("按鈕被點擊了:msg"+e.getActionCommand()); ?} }6.輸入框TextField 監聽
package lesson02; ? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ? public class TestTextField {public static void main(String[] args) {Myframe myframe = new Myframe();} } class Myframe extends Frame{public Myframe(){TextField textField = new TextField();add(textField);//監聽這個文本框輸入的文字MyActionListener2 myActionListener2 = new MyActionListener2();//按下enter 就會觸發這個輸入框的事件textField.addActionListener(myActionListener2);textField.setEchoChar('*'); //設置替換編碼setVisible(true);//可以看見文本框pack();//自適應} } class MyActionListener2 implements ActionListener{ ?@Overridepublic void actionPerformed(ActionEvent e) {TextField field = (TextField) e.getSource();//獲得一些資源, 返回了一個對象System.out.println(field.getText()); ?//獲得輸入框中的文本field.setText("");} }7.簡易計算器
oop原則:組合大于繼承!
目前代碼:
package lesson02; ? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ? //簡易計算器 public class TestCalc {public static void main(String[] args) {new Calculator();} } //計算器類 class Calculator extends Frame{public Calculator() {//三個文本框TextField num1 = new TextField(10);//字符數TextField num2 = new TextField(10);TextField num3 = new TextField(20);//一個按鈕Button button = new Button("=");//監聽button.addActionListener(new MyCalculatorListener(num1,num2,num3));//一個標簽Label label = new Label("+");//布局setLayout(new FlowLayout());add(num1);add(label);add(num2);add(button);add(num3);pack();setVisible(true);} } //監聽器類 class MyCalculatorListener implements ActionListener{//獲取三個變量private TextField num1,num2,num3;public MyCalculatorListener(TextField num1,TextField num2,TextField num3){this.num1=num1;this.num2=num2;this.num3=num3;}@Overridepublic void actionPerformed(ActionEvent e) {//1. 獲得加數和被加數int n1 = Integer.parseInt(num1.getText());int n2 = Integer.parseInt(num2.getText()); ?//2. 將這個值 + 法運算后放到第三個框num3.setText(""+(n1+n2));//3. 清除前兩個框num1.setText("");num2.setText("");} }完全改造為面向對象寫法
package lesson02; ? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ? //簡易計算器 public class TestCalc {public static void main(String[] args) {new Calculator();} } //計算器類 class Calculator extends Frame{TextField num1,num2,num3;public Calculator() {//三個文本框num1 = new TextField(10);//字符數num2 = new TextField(10);num3 = new TextField(20);//一個按鈕Button button = new Button("=");//監聽button.addActionListener(new MyCalculatorListener(this));//一個標簽Label label = new Label("+");//布局setLayout(new FlowLayout());add(num1);add(label);add(num2);add(button);add(num3);pack();setVisible(true);} } //監聽器類 class MyCalculatorListener implements ActionListener{//獲取計算器這個變量,在一個類中組合另外一個類Calculator calculator =null;public MyCalculatorListener(Calculator calculator){this.calculator= calculator;}@Overridepublic void actionPerformed(ActionEvent e) {//1. 獲得加數和被加數int n1 = Integer.parseInt(calculator.num1.getText());int n2 = Integer.parseInt(calculator.num2.getText()); ?//2. 將這個值 + 法運算后放到第三個框calculator.num3.setText(""+(n1+n2));//3. 清除前兩個框calculator.num1.setText("");calculator.num2.setText("");} }內部類:
-
更好的計算
- package lesson02; ? import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; ? //簡易計算器 public class TestCalc {public static void main(String[] args) {new Calculator();} } //計算器類 class Calculator extends Frame{TextField num1,num2,num3;public Calculator() {num1 = new TextField(10); //三個文本框//字符數num2 = new TextField(10);num3 = new TextField(20);Button button = new Button("="); ? ? ? ?//一個按鈕button.addActionListener(new MyCalculatorListener()); ? ? ? ?//監聽Label label = new Label("+"); ? //一個標簽setLayout(new FlowLayout()); ? ? ? ?//布局add(num1);add(label);add(num2);add(button);add(num3);pack();setVisible(true);}//監聽器類//內部類的最大的好處,就是可以暢通無阻的訪問外部類的屬性和方法!class MyCalculatorListener implements ActionListener{@Overridepublic void actionPerformed(ActionEvent e) {//1. 獲得加數和被加數int n1 = Integer.parseInt(num1.getText());int n2 = Integer.parseInt(num2.getText()); ?//2. 將這個值 + 法運算后放到第三個框num3.setText(""+(n1+n2));//3. 清除前兩個框num1.setText("");num2.setText("");}} }
畫筆
package lesson03; ? import java.awt.*; ? public class TextPaint {public static void main(String[] args) {new MyPaint().lodeFrame();} } class MyPaint extends Frame {public void lodeFrame(){setBounds(200,200,600,400);setVisible(true);}//畫筆@Overridepublic void paint(Graphics g) {g.setColor(Color.red);g.drawOval(100,100,100,100);g.fillOval(100,100,100,100);//實心的圓 ?g.setColor(Color.GREEN);g.fillRect(200,250,180,180);//養成習慣,畫筆用完,將它還原到最初的顏色} }鼠標監聽
目的:想要實現鼠標畫畫
package lesson03; ? import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.Iterator; ? //鼠標監聽 public class TextMouseListener {public static void main(String[] args) {new MyFrame("畫圖");} } //自己的類 class MyFrame extends Frame{//畫畫需要畫筆,需要監聽鼠標當前位置,需要集合來存儲這個點ArrayList points;public MyFrame(String title){super(title);setBounds(200,200,400,300);//存儲鼠標點擊的點points = new ArrayList<>();setVisible(true);//鼠標監聽器,相對于這個窗口this.addMouseListener(new MyMouseListener());} ?@Overridepublic void paint(Graphics g) {//畫畫,監聽鼠標的位置Iterator iterator = points.iterator();while (iterator.hasNext()){Point point = (Point) iterator.next();g.setColor(Color.BLUE);g.fillOval(point.x ,point.y,10,10);}}//添加一個點到我們的界面上面public void addPaint(Point point){points.add(point);}private class MyMouseListener extends MouseAdapter{//鼠標 按下 彈起 按住不放 ?@Overridepublic void mouseClicked(MouseEvent e) {MyFrame myFrame = (MyFrame) e.getSource();//這里我們點擊的時候,就會在界面上產生一個點!//這個點就是鼠標的點myFrame.addPaint( new Point(e.getX(),e.getY()));//每次點擊鼠標都需要重新畫一遍myFrame.repaint();//刷新一下}} }窗口監聽
package lesson03; ? import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; ? public class TestWindow {public static void main(String[] args) {new WindowFrame();} } class WindowFrame extends Frame {public WindowFrame() {setBackground(Color.blue);setBounds(100,100,200,200);setVisible(true);//addWindowFocusListener(new MyWindowListener());this.addWindowListener(//內部類new WindowAdapter() {@Overridepublic void windowOpened(WindowEvent e) {System.out.println("windowOpened");} ?@Overridepublic void windowClosing(WindowEvent e) {System.out.println("windowClosing");System.exit(0);} ?@Overridepublic void windowClosed(WindowEvent e) {System.out.println("windowClosed");} ?@Overridepublic void windowActivated(WindowEvent e) {System.out.println("windowActivated");//窗口激活}});} }鍵盤監聽
package lesson03; ? import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; ? public class TestKeyListener {public static void main(String[] args) {new KeyFrame();} } class KeyFrame extends Frame{public KeyFrame(){setBounds(1,2,300,400);setVisible(true); ?this.addKeyListener(new KeyAdapter() {//鍵盤按下@Overridepublic void keyPressed(KeyEvent e) {//獲得鍵盤按下的鍵是哪一個int keyCode = e.getKeyCode();if (keyCode == KeyEvent.VK_UP){System.out.println("你按下了上鍵");}}});} }總結
- 上一篇: 实例分析正版手游的安卓反破解之路
- 下一篇: Rk3566 Rk3326s Andro