黑马程序员——GUI

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

GUI

Graphical User Interface(图形用户接口 )

用图像的方式,来显示计算机操作的界面

CLI

Command line User Interface(命令行用户接口)

就是常见的Dos命令行操作

Java为GUI提供的对象都存在java.Awt和java.Swing两个包中

Awt和Swing

java.Awt:Abstract WindowToolKit()(抽象窗口工具包),需要调用本地系统方法实现功能。属于重量级控件

javax.Swing:在AWT的基础上,建立的一套图形界面系统,其中提供了更多的组件,而且完全由Java实现。增强了移植性,属轻量控件

 

●布局管理器

容器中的组件的排放方式,就是布局

常见的布局管理器:

         FlowLayout(流式布局管理器)

                   从左到右的顺序排列

                   Panel默认的布局管理器

         BorderLayout(边界布局管理器)

                   东南西北中

                   Frame默认的布局管理器

 

         GridLayout(网格布局管理器)

                   规则的矩阵

         CardLayout(卡片布局管理器)

                   选项卡

         GridBagLayout(网格包布局管理器)

                   非规则的矩阵

 

 

●创建图形化界面

    (1)创建frame窗体

    (2)对窗体进行基本设置,大小,位置,布局

    (3)定义组件

    (4)将组件通过窗体的add方法添加到窗体中

    (5)让窗体显示,通过setVisible(true)

●事件监听机制组成

         事件源(组件):就是awt包或者swing包中的哪些图形界面组件

         事件(Event):每一个事件源都有自己特有的对应事件和共性事件

         监听器(Listener):将可以触发某一个事件的动作(不止一个动作)都已经封装到了监听器中

         事件处理(引发事件后处理方式)

        

点名器:

package callname;

import java.awt.Button;

import java.awt.Font;

import java.awt.Frame;

import java.awt.Image;

import java.awt.Label;

import java.awt.Toolkit;

importjava.awt.event.ActionEvent;

importjava.awt.event.ActionListener;

importjava.awt.event.WindowAdapter;

importjava.awt.event.WindowEvent;

importjava.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

 

public class CallName {

         public static Label a;

         public static ArrayList al;

         public static void main(String[] args) {

            MyFrame mf = new MyFrame();

            ListInit();

         }

         private static void ListInit() {

            al=new ArrayList();

            BufferedReader br=null;

            try {

                FileReader fr = new FileReader("src/dianming.txt");

                br = new BufferedReader(fr);

                String content=null;

                while((content=br.readLine())!=null){

                    al.add(content);

                }

            } catch (IOException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }finally{

                if(br!=null){

                    try {

                        br.close();

                    } catch (IOException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                    }

                }

            }

         }

}

class MyFrame extends Frame{

         MyFrame(){

            init();

         }

         public void init(){

             CallName.a=newLabel();

             Button b1=newButton("点名");

             Font font = newFont(Font.DIALOG,10,100);

             Font font1 = newFont(Font.DIALOG,10,30);

             Image icon =Toolkit.getDefaultToolkit().getImage(

             "src/qq.png");

             CallName.a.setFont(font);

             CallName.a.setBounds(70,50,300,100);      

             CallName.a.setAlignment(1);

             b1.setBounds(180,230,100,50); 

             b1.setFont(font1);

             b1.addActionListener(new MyButtonListener());

             CallName.a.setText("点名器");

             this.setTitle("点名器");

             this.setIconImage(icon);

             this.setLayout(null);         

             this.setBounds(100, 100, 450, 350);

             this.add(CallName.a);

             this.add(b1);

             this.setVisible(true);

             this.addWindowListener(newMyWindowListener());

         }

}

class MyWindowListenerextends WindowAdapter{

         public void windowClosing(WindowEvent e){

             System.exit(0);

          }

}

class MyButtonListenerimplements ActionListener{

         @Override

         public void actionPerformed(ActionEvent e) {

            Collections.shuffle(CallName.al);

            Object object = CallName.al.get(0);

            CallName.a.setText((String)object);

         }

        

}

 

文件夹

package afternoon;

 

import java.awt.*;

import java.awt.event.*;

import java.io.File;

 

public class LayoutDemo {

    private Frame f=new Frame();;

    private TextArea ta = new TextArea(20,60);

    private TextField tf = new TextField(53);

    private Button but = new Button("转到");

    private File file=null;

    private Dialog dialog = new Dialog(f,"错误了",true);

    private Label label = new Label();

    private Button errbut = new Button("确认");

    private String text=null;

    public LayoutDemo(){

       init();

    }

    private void init(){

       Image img = Toolkit.getDefaultToolkit().getImage("src/qq.png");   

       f.setIconImage(img);

       f.setTitle("文件夹");

       f.setBounds(200, 100, 500, 500);

       f.setLayout(new FlowLayout());

       MyEvent();

       f.add(tf);

        f.add(but);

       f.add(ta);

       f.setVisible(true);

       dialog.setLayout(new FlowLayout());

       dialog.add(label);

       dialog.add(errbut);

       dialog.setBounds(350,250,200,150);

    }

    private void MyEvent(){

       f.addWindowListener(new WindowAdapter() {

           public void windowClosing(WindowEvent e){

              System.exit(0);

           }

       });

       dialog.addWindowListener(new WindowAdapter() {

           public void windowClosing(WindowEvent e){

              dialog.setVisible(false);

           }

       });

       errbut.addActionListener(new ActionListener() {

           public void actionPerformed(ActionEvent e) {

              dialog.setVisible(false);

             

           }

       });

       errbut.addKeyListener(new KeyAdapter() {

           public  voidkeyPressed(KeyEvent e){

              if(e.getKeyCode()==10){

                  dialog.setVisible(false);

              }

           }

       });

       tf.addKeyListener(new KeyAdapter() {

           public  voidkeyPressed(KeyEvent e){

              if(e.getKeyCode()==10){

                  Judge();

              }

           }

       });

       but.addActionListener(new ActionListener(){

           @Override

           public void actionPerformed(ActionEvent e) {

              Judge();

           }

       });

    }

    public void Judge() {

       ta.setText("");

       text = tf.getText();

       file= new File(text);

       if(file.exists()){

           String[] list = file.list();

           for (String string : list) {          

              ta.append(string+"\r\n");  

           }

       }else{

           label.setText("dsahsdfgjfhs");

           dialog.setVisible(true);

           //ta.setText("不存在的路径");

       }

    }

    public static void main(String[] args) {

       new LayoutDemo();

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值