62.【GUI编程】(一)

简介: 62.【GUI编程】

Java.Swing与Java.Awt

(一)、AWT

1.Frame (初阶窗体)

使用简单的封装技术,实现多线程的效果.
基本思想:
因为构造函数,也就是构造器。再被调用类的时候,会被默认启动一次。因为我们
一次性启动很多次类所以达到了我们所需要的多线程(Runable)效果.
知识掌握:
调用类之后,构造器会自动被执行。

JavaBean类

package KuangStudy;
import java.awt.*;
public class Student extends Frame {  //继承的是awt包中的Frame
    static int number=0;
    public Student(){}
public Student(int x,int y){  //定义窗体的长和宽
    super("MyFrame "+(++number));
    Color c=new Color(121, 25, 25);
    this.setBackground(c);
    this.setLocationRelativeTo(null);
    setSize(x,y);  
    setVisible(true);
}
}

Test类

package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
     Student student=new Student(300,300);
     Student student1=new Student(300,300);
     Student student2=new Student(300,300);
     Student student3=new Student(300,300);
     Student student4=new Student(300,300);
}
}

2.Panel and Frame(适配器)

窗体和画板的结合,画板要内嵌到窗体中去
基本思路:
我们在内嵌画板的时候,需要使用到add()添加的操作。继承分为(继承中)与
(继承完毕)。继承中,子类是不能代替父类的。继承完毕,那么子类就可以代替
父类。比如本次内嵌的操作中,add()的需要是panel.
知识掌握:
继承:(继承中)与(继承完毕)
适配器: (new WindoeAdapter),适配器不需要注册监听.

JavaBean:

package KuangStudy;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Student extends Frame {  //继承的是awt包中的Frame
public Student(){
    //窗体
    Color c=new Color(121, 25, 25);
    this.setLayout(null);//流布局
    setBounds(300,300,500,500);//位置,长度
    this.setBackground(c);
    //画板
    Panel panel=new Panel();
    panel.setBounds(350,350,250,250);//位置,长度
    panel.setBackground(new Color(0,0,0));
    this.add(panel);  //画板添加到窗体中去
    panel.setVisible(true);
    this.setVisible(true);
    //接口的适配器 进行退出
    this.addWindowListener(new WindowAdapter() {
        @Override
        public void windowClosing(WindowEvent e) {  //窗口点击关闭的时候,我们需要做什么?
            System.exit(0);
        }
    });
}
}

Test:

package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
    Student student=new Student();
}
}

3.Frame_Layout(窗体和布局)

按钮、翻滚页等按钮的添加,进行添加
基本思路:窗体内内嵌按钮对象,
知识掌握: 流布局(FlowLayout())、表格布局(GirdLayout())、
      窗体布局(BorderLayout())、以及按钮类的创建Button();-
package KuangStudy;
import java.awt.*;
public class Test {
    public static void main(String[] args) {
     //窗体
    Frame frame=new Frame();
    frame.setSize(300,300);
    //按钮
      Button button_A=new Button("A");
      Button button_B=new Button("B");
      Button button_C=new Button("C");
      Button button_D=new Button("D");
     //按钮布局
     frame.setLayout(new GridLayout(4,1));
     //按钮进行添加窗体
     frame.add(button_A);
     frame.add(button_B);
     frame.add(button_C);
     frame.add(button_D);
     frame.pack();  //帮助我们自动优化
     frame.setVisible(true);
     frame.setLocationRelativeTo(null);
}
}

4.Frame_Layout (作业题)

package KuangStudy;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test {
    public static void main(String[] args) {
     //窗体
    Frame frame=new Frame();
    frame.setSize(300,300);
    //按钮
      Button button_A=new Button("A");
      Button button_B=new Button("B");
      Button button_C=new Button("C");
      Button button_D=new Button("D");
      Button button_E=new Button("E");
      Button button_F=new Button("F");
      Button button_G=new Button("G");
      Button button_H=new Button("H");
      Button button_I=new Button("I");
      Button button_J=new Button("J");
      //第一个画板
      Panel panel_one=new Panel();
      panel_one.setLayout(new GridLayout(1,2));
      panel_one.add(button_A);
      panel_one.add(button_B);
      //第二个画板
      Panel panel_two=new Panel();
      panel_two.setLayout(new GridLayout(2,2));
      panel_two.add(button_C);
      panel_two.add(button_D);
      panel_two.add(button_E);
      panel_two.add(button_F);
        //第一次整合
        Panel panel_five=new Panel();
        panel_five.setLayout(new BorderLayout());
        panel_five.add(button_G,BorderLayout.WEST);
        panel_five.add(button_H,BorderLayout.EAST);
        panel_five.add(panel_one,BorderLayout.CENTER);
        //第二次整合
        Panel panel_six=new Panel();
        panel_six.setLayout(new BorderLayout());
        panel_six.add(button_I,BorderLayout.WEST);
        panel_six.add(button_J,BorderLayout.EAST);
        panel_six.add(panel_two,BorderLayout.CENTER);
        //窗体整合
        frame.setLayout(new BorderLayout());
        frame.add(panel_five,BorderLayout.NORTH);
        frame.add(panel_six,BorderLayout.CENTER);
        frame.setVisible(true);
        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
               System.exit(0);
            }
        });
     frame.setLocationRelativeTo(null);
}
}

5.EventListener(事件监听)

实现事件监听的操作:
基本思想:
implements (监听事件),设置监听,实现监听,实现监听的操作。
知识掌握:
实现监听:  事件源.add(设置监听的对象);
package KuangStudy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test {
    public static void main(String[] args) {
      Frame frame=new Frame();
      frame.setSize(300,300);
      frame.setLocationRelativeTo(null);
      Button button=new Button("请");
      frame.add(button);
      MyActionListener myActionListener=new MyActionListener();
      button.addActionListener(myActionListener);
     frame.addWindowListener(new WindowAdapter() {
         @Override
         public void windowClosing(WindowEvent e) {
             if(JOptionPane.showConfirmDialog(null,"您确定要退出么?")==0){
                System.exit(0);
             }
         }
     });
      frame.setVisible(true);
}
    }
 class MyActionListener implements ActionListener {
    public MyActionListener() {
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("点我干嘛");
        JOptionPane.showMessageDialog(null, "别点我");
    }
}



相关文章
|
8月前
|
容器
|
8月前
|
数据安全/隐私保护
|
7天前
|
开发框架 程序员 开发者
Python GUI编程:从入门到精通3.2 GUI编程:学习使用Tkinter、PyQt或wxPython等库创建图形用户界面。
Python GUI编程:从入门到精通3.2 GUI编程:学习使用Tkinter、PyQt或wxPython等库创建图形用户界面。
|
2月前
|
API 开发工具 C++
Python图形用户界面(GUI)编程:大解密
Python图形用户界面(GUI)编程:大解密
67 0
|
8月前
|
8月前
|
8月前
|
安全 网络安全 图形学
SAP GUI 一些实用技巧分享
SAP GUI 一些实用技巧分享
58 0
|
11月前
|
API 图形学
【unity专题篇】—GUI(IMGUI)思维导图详解
【unity专题篇】—GUI(IMGUI)思维导图详解
206 0
|
前端开发 数据可视化 Python
GUI编程中的tkinter入门教程以及经典写法
GUI编程中的tkinter入门教程以及经典写法
126 0
|
编解码 前端开发 Java
GUI 编程
图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面。
GUI 编程