JAVA学习第五十八课 — GUI

来源:互联网 发布:网络剧ip什么意思 编辑:程序博客网 时间:2024/06/13 21:15

GUI

Graghical User Interface(图形用户接口)

java为GUI提供的对象都存在java.awt和java.swing包中


Java的GUI做的的确干不过C++等,不打算浪费过多的时间在这上面

一个简单的窗体演示

public static void main(String[] args){Frame f = new Frame("新窗体");f.setLocation(400, 200);//设置窗体的位置f.setSize(500, 400);//设置窗口大小//f.setBounds(400, 200, 500, 400);功能相当于上面两句f.setLayout(new FlowLayout());//设置 流式 布局Button bt = new Button("一个按钮");f.add(bt);f.setVisible(true);//显示窗口}

显示出窗体后发现无法关闭窗体,就用到了事件监听机制

其组成:事件源(组件)、事件(Event)、监听器(Listener)、事件处理(引发事件后处理方式)

 

public static void main(String[] args){Frame f = new Frame("新窗体");f.setLocation(400, 200);//设置窗体的位置f.setSize(500, 400);//设置窗口大小//f.setBounds(400, 200, 500, 400);功能相当于上面两句f.setLayout(new FlowLayout());//设置 流式 布局Button bt = new Button("一个按钮");f.add(bt);//因为关不掉这一事件, 要注册一个监听器//窗体适配器类WindowAdapter,已经覆盖了所有方法,便捷于创建监听器f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});//添加窗体监听f.setVisible(true);//显示窗口}

用户对组件操作,就是一个事件,那么产生事件的组件就是事件源。

ActionListener演示:

public static void main(String[] args){Frame f = new Frame("新窗体");f.setLocation(400, 200);//设置窗体的位置f.setSize(500, 400);//设置窗口大小//f.setBounds(400, 200, 500, 400);功能相当于上面两句f.setLayout(new FlowLayout());//设置 流式 布局Button bt = new Button("一个按钮");f.add(bt);//因为关不掉这一事件, 要注册一个监听器//窗体适配器类WindowAdapter,已经覆盖了所有方法,便捷于创建监听器f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});//添加窗体监听//在按钮上添加一个监听事件:点击一下退出bt.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {//System.out.println("按了");System.exit(0);}});f.setVisible(true);//显示窗口}

键盘和鼠标监听事件

import java.awt.Button;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.TextField;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class Main {private Frame f;private TextField tf;//文本框private Button bt;public Main(){init();}private void init(){f = new Frame("鼠标和键盘监听");bt = new Button("按钮");f.setBounds(400, 200, 500, 400);f.setLayout(new FlowLayout());tf = new TextField(40);//指定列数f.add(tf);f.add(bt);myEvent();f.setVisible(true);}private void myEvent(){//给文本框添加键盘监听tf.addKeyListener(new KeyAdapter() {@Overridepublic void keyPressed(KeyEvent e) {//按下//System.out.println("key Pressed : "+e.getKeyChar()+" : "+e.getKeyCode()+" : "+e.getKeyText(e.getKeyCode()));//按下即打印/*int code = e.getKeyCode();if(!(code>=KeyEvent.VK_0 && code <= KeyEvent.VK_9))//判断{System.out.println("必须数字");e.consume();//使用,不会按照默认的事件处理方式}if(e.getKeyCode()==KeyEvent.VK_ENTER){//按下回车System.out.println("enter.....");}*/if(e.isControlDown() && e.getKeyCode()==KeyEvent.VK_ENTER){//按下Ctrl+回车System.out.println("Crtl enter.....");}}});//在窗体上添加退出监听器f.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e){System.exit(0);}});//在按钮上添加鼠标监听器bt.addMouseListener(new MouseAdapter() {private int count = 1;//计数器public void mouseEntered(MouseEvent e) {//鼠标碰到即触发tf.setText("mouse enter : "+count++);//信息添加到文本框}public void mouseClicked(MouseEvent e) {//点击if(e.getClickCount()==2){//得到点击次数,双击System.out.println("mouseClicked Double click");}/*else if(e.getClickCount()==1){System.out.println("mouseClicked only click");}*/}});}public static void main(String[] args){new Main();}}

关于Swing包中的,需要在Ecplice安装插件。


9 1