gui 11

GUI

  1. 核心技术: Swing AWT

    AWT

    1. 包含了很多类和接口

    2. 元素: 窗口,按钮,文本框

    3. Java.

    4. package lesson01;
      ​
      import java.awt.*;
      ​
      public class TestFrame {
          public static void main(String[] args) {
              Frame frame = new Frame("我的第一个java图像界面窗口");
              //需要设置可见性
              frame.setVisible(true);
              //设置窗口大小
              frame.setSize(400,400);
              //设置背景颜色
              frame.setBackground(new Color(0, 253, 57));
              //弹出的初始位置
              frame.setLocation(200,200);
              //设置大小固定
              frame.setResizable(fase);
      ​
          }
      }
      ​
      ​
      ​

问题: 窗口关闭不了,停止运行java

尝试回顾封装:

package lesson01;
​
import java.awt.*;
​
public class TestFrame2 {
    public static void main(String[] args) {
        //展示多个窗口
        MyFrame 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.orange);
        MyFrame myFrame4 = new MyFrame(300, 300, 200, 200, Color.red);
​
​
    }
}
    class MyFrame extends Frame{
        //
        static int id = 0;//可能存在多个窗口,我们需要用一个计数器
        public MyFrame(int x,int y,int w,int h,Color color){
        super("MyFrame"+(++id)); //必须写在第一行
        setBounds(x,y,w,h);  包括了初始位置和大小
        setVisible(true); 可以最大窗口化
        setBackground(color);
​
​
​
        }
​
    }

面板(解决了关闭事件)

package lesson01;
//可以看成是一个空间,但不能单独存在
​
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
​
public class TestPanel {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel = new Panel();
        frame.setLayout(null);//设置布局
        //坐标和颜色
        frame.setBounds(100,100,300,300);
        frame.setBackground(new Color(1,2,3));
        //设置panel的相对坐标和颜色   panel是在frame里面的
        panel.setBounds(50,50,100,100);
        panel.setBackground(new Color(28, 60, 93));
        //在frame里面嵌入panel
        frame.add(panel);
        //设置可见性 让它能看得见
        frame.setVisible(true);
        //解决它的关闭事件 监听事件 
        //适配器模式
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
​
        });
​
​
​
​
    }
​
}
​

布局管理器
  1. 流式布局 (左到右)

  2. 东西南北中

  3. 表格布局

1.frame 一个窗口

panel 4个面板

border:

左:button

中:面板

右:button

public class LianXi {
    public static void main(String[] args) {
        Frame frame = new Frame();
        Panel panel1 = new Panel(new BorderLayout());
        Panel panel2 = new Panel(new GridLayout(2,1));
        Panel panel3 = new Panel(new BorderLayout());
        Panel panel4 = new Panel(new GridLayout(2,2));
​
​
        frame.setLayout(new GridLayout(2,1));
        frame.setSize(400,400);
        frame.setLocation(300,300);
        frame.setBackground(Color.BLUE);
        frame.setVisible(true);
        frame.add(panel1);
        frame.add(panel3);
        panel1.add(new Button("east-1"),BorderLayout.EAST);
        panel1.add(panel2,BorderLayout.CENTER);
        panel2.add(new Button("center-1"),BorderLayout.CENTER);
        panel2.add(new Button("center-2"),BorderLayout.CENTER);
​
        panel1.add(new Button("west-1"),BorderLayout.WEST);
        panel3.add(new Button("east-2"),BorderLayout.EAST);
        panel3.add(new Button("west-2"),BorderLayout.WEST);
        panel3.add(panel4,BorderLayout.CENTER);
        //for(int i=0; i<4;i++){
        //       panel4.add(new Button("for"+i))}
​
        panel4.add(new Button("center-3"));
        panel4.add(new Button("center-4"));
        panel4.add(new Button("center-5"));
        panel4.add(new Button("center-6"));
​
    frame.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
​
​
​
​
    }
}

总结:

  1. frame是一个顶级窗口

  2. panel无法的单独显示,必须添加到某个容器中

  3. 大小 定位 背景颜色 监听

    事件监听
    public class TestActionEvent {
        public static void main(String[] args) {
            //按下按钮,触发一些事件
            Frame frame = new Frame();
            Button button = new Button();
            //因为 addActionListener需要一个ActionListener,所以呢我们构造了一个ActionListener
    ​
            MyActionListener myActionListener = new MyActionListener();
            button.addActionListener(myActionListener);
            frame.add(button);
            frame.pack();
            frame.setVisible(true);
            windowClose(frame);
    ​
    ​
        }
        //关闭窗口的事件
        private  static void windowClose(Frame frame){
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
        }
    }
    ​
    class MyActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
    ​
            System.out.println("aaa");
        }
    }
    public class TestActionTwo {
        public static void main(String[] args) {
            
            Frame frame = new Frame("开始-停止");
            Button button1 = new Button("start");
            Button button2 = new Button("stop");
            button2.setActionCommand("button-stop");//如果设置了set那就get出来   如果没有set就输出默认值
            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);
            windowClose(frame);
    ​
    ​
    ​
        }
        //关闭事件
        private static void windowClose(Frame frame){
            frame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
    ​
        }
    }
    class MyMonitor implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("按钮被点击了:msg"+e.getActionCommand());
        }
    }
输入框TextField监听
public class TestText01 {
    public static void main(String[] args) {
//只写启动!!!!!
        Myframe myframe = new Myframe();
        windowClose(myframe);
​
    }
    private static void windowClose( Frame myframe){
        myframe.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
​
    }
}
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{
​
            @Override
            public void actionPerformed(ActionEvent e) {
               TextField field=(TextField)  e.getSource();//获取资源
                System.out.println(field.getText());//获得输入框的文本
                field.setText("");//null 输入结束了回车就清空
            }
        }
简易计算器
  1. oop原则: 组合大于继承

  2. public class TestCalur {
        public static void main(String[] args) {
            new Calculator();
        }
    }
    //计算器类
    class Calculator extends Frame {
        public Calculator(){
            //3个文本框
            TextField num1 = new TextField(10);//字符数
            TextField num2 = new TextField(10);
            TextField num3 = new TextField(20);
            //1个按钮
            Button button = new Button("=");
            button.addActionListener(new MyCalculatorListener(num1,num2,num3));
            //1个标签
            Label label = new Label("+");
            ///布局  流式布局从左往右
            setLayout(new FlowLayout());
            add(num1);
            add(label);
            add(num2);
            add(button);
            add(num3);
            pack();
            setVisible(true);
        }
    }
    //监听器类
    class MyCalculatorListener implements ActionListener{
        //获取3个变量
        private TextField num1,num2,num3;
    ​
        public MyCalculatorListener(TextField num1,TextField num2,TextField num3) {
            this.num1 = num1;
            this.num2 = num2;
            this.num3 = num3;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数 将字符串类型转换成int  Integer.parseInt()
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            //2.将这两个值+法运算以后,放到第三个框
            num3.setText(""+(n1+n2));
            //清除前两个框
            num1.setText("");
            num2.setText("");
            
        }
    }

完全改用为面向对象

public class TestCalur {
    public static void main(String[] args) {
        new Calculator().loadFrame();       
    }
}
//计算器类
class Calculator extends Frame {
    TextField num1,num2,num3;
   public void loadFrame(){
       //3个文本框
       num1 = new TextField(10);//字符数
       num2 = new TextField(10);
       num3 = new TextField(20);
       //1个按钮
       Button button = new Button("=");
       button.addActionListener(new MyCalculatorListener(this));
       //1个标签
       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;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //1.获得加数和被加数 将字符串类型转换成int  Integer.parseInt()
        int n1 = Integer.parseInt(calculator.num1.getText());
        int n2 = Integer.parseInt(calculator.num2.getText());
​
​
        //2.将这两个值+法运算以后,放到第三个框
        calculator.num3.setText(""+(n1+n2));
        //清除前两个框
         calculator.num1.setText("");
        calculator.num2.setText("");
​
​
    }
}

内部类:

更好的包装

内部类最大的好处,就是可以畅通无阻地访问外部类

public class TestCalur {
    public static void main(String[] args) {
        new Calculator().loadFrame();
    }
}
//计算器类
class Calculator extends Frame {
    TextField num1,num2,num3;
   public void loadFrame(){
       //3个文本框
       num1 = new TextField(10);//字符数
       num2 = new TextField(10);
       num3 = new TextField(20);
       //1个按钮
       Button button = new Button("=");
       button.addActionListener(new MyCalculatorListener());
       //1个标签
       Label label = new Label("+");
       ///布局  流式布局从左往右
       setLayout(new FlowLayout());
       add(num1);
       add(label);
       add(num2);
       add(button);
       add(num3);
       pack();
       setVisible(true);
   }
    //监听器类
    //内部类
  private  class MyCalculatorListener implements ActionListener{
        //获取计算器这个对象,在一个类中组合另外一个类
        @Override
        public void actionPerformed(ActionEvent e) {
            //1.获得加数和被加数 将字符串类型转换成int  Integer.parseInt()
            int n1 = Integer.parseInt(num1.getText());
            int n2 = Integer.parseInt(num2.getText());
            //2.将这两个值+法运算以后,放到第三个框
            num3.setText(""+(n1+n2));
            //清除前两个框
            num1.setText("");
            num2.setText("");
​
        }
    }
}
画笔
avpublic class TestPaint {
    public static void main(String[] args) {
    new Mypaint().loadFrame();
    }
}
class Mypaint extends Frame{
    public  void loadFrame(){
        setBounds(200,200,500,600);
        setVisible(true);
    }
    @Override//画笔
    public void paint(Graphics g) {
        //画笔需要颜色,画笔需要会画画
       // g.setColor(Color.black);
        g.drawOval(100,100,100,100);
        g.fillOval(100,100,100,100);//实心的圆
       // g.setColor(Color.ORANGE);
        g.fillRect(100,200,300,400);
        //养成习惯,画笔用完把它恢复到最初的颜色
    }
}
鼠标监听事件

public class TestMouseListener {
    public static void main(String[] args) {
     new MyFrame3("画图");
    }
}
//自己的类
class MyFrame3 extends Frame{
    //画画需要画笔,需要鼠标监听当前的位置,需要集合来存储这个点
    ArrayList points;
    public MyFrame3(String title)  {
        super(title);
        setBounds(200,200,200,200);
        //存鼠标点击的点
       points= new ArrayList<>();
        this.addMouseListener( new MYMouseListener());
        setVisible(true);
​
    }
    @Override
    public 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 {
        @Override
        public void mousePressed(MouseEvent e) {
            super.mousePressed(e);
            MyFrame3 myFrame3 = (MyFrame3) e.getSource();//这里我们点击的时候,就会在界面上产生一个点
            //这个点就是我们鼠标的点
            myFrame3.addPaint(new Point(e.getX(),e.getY()));
            myFrame3.repaint();//刷新 每一次都重画一遍
        }
    }
}

窗口监听
public class TestWindow {
    public static void main(String[] args) {
     new windowFrame();
    }
}
class windowFrame extends Frame{
    public windowFrame()  {
        setBounds(200,100,200,130);
        setBackground(Color.CYAN);
        setVisible(true);
        //addWindowListener(new MyWindowListener());
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.out.println("你点击了x ");
                System.exit(0);
            }
        });
    }
​
}

键盘监听

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() {
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘的键是哪一个,是当前的码
                int keyCode = e.getKeyCode();//不需要记这个数值,直接使用静态属性,VK-XXX
                System.out.println(keyCode);
                if ( keyCode == KeyEvent.VK_DOWN){
                    System.out.println("你按了下键");
                }
​
            }
        });
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值