图形化界面知识点

来源:互联网 发布:淘宝小号出售平台2016 编辑:程序博客网 时间:2024/04/30 04:03

一、创建图形化界面
1.awt,需要调用本地系统方法实现功能。Swing,建立的一套图形化界面系统,其中提供了更多的组间,完全java实现。所以使用一般是两者合起来使用。
class AwtDemo{public static void main(String[] args){Frame f= new Frame("my awt");//实例化一个界面,并且创建名称f.setSize(500,400);//设定大小,500为横轴,400为纵轴f.setLocation(300,200);//设定窗体位置。300为顶点到横的。f.setLayout(new FlowLayout());//设定布局Button b=new Button("我是一个按钮");f.add(b);//f.addWindowListener(new Mywin());f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.out.println("windowClosing"+e.toString);System.exit(0);//关闭程序     } public void windowActivated(WindowEvent e){ System.out.println("我活了。。。"); }});f.setVisible(true);//显示}}
2.组件:component:container、Button、Label、CheckBox、TextComponent(文本组间);container:window、panel;window:Frame、Dialog。

3.布局管理:流式布局、边界布局、卡片布局、坐标式布局。

4.事件监听机制。包括:事件源、外部动作、事件对象、监听器。特点:每个事件源都有自己特有的对应事件和共性事件;监听器可以触发某一个事件的动作。
总结:以上,在java中定义好了,直接获取对象来用就可以,我们要做的事情是就是产生的动作进行处理。
代码演示:
import java.awt.*;import java.awt.event.*;class FrameDemo{//定义该图形所需的组间应用。private Frame f;private Button but;FrameDemo(){}public void init(){f=new Frame("My frame");//对frame进行基本设置f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());but =new Button();//将组件添加到frame中f.add(but);//加载一下窗体上的事件myEvent();//显示窗体f.setVisible(true);}private void myEvent(){//添加事件,也就是监听器f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.out.println("windowClosing"+e.toString);System.exit(0);//关闭程序     } }); //让按钮具备退出程序的功能 /* 按钮就是事件源 那么选择哪个监听器呢? 通过关闭窗体示例了解到,想要知道那个组件具备什么样的特有监听器 需要查看该组件对象的功能。 通过查阅button的描述,发现按钮支持一个特有监听addActionListener */ but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){//什么监听器对应什么事件按钮。 System.out.println("退出,按钮干的"); System.exit(0); } });}public static void main(String[] args){new FrameDemo();}}
5.适配器。作用:为了更好的监听对象。ActionListener(没有适配器)方法不能超过3个,超过就有适配器。
代码演示:使用鼠标,监听鼠标。。和键盘。
import java.awt.*;import java.awt.event.*;class MousAndKeyEvent{//定义该图形所需的组间应用。private Frame f;private Button but;private TextField tf;//文本框MousAndKeyEvent(){init();//一建立对象,就调用init函数,进行窗体的基本设置}public void init(){f=new Frame("My frame");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf=new TextField(20);//初始化,并且指定列数。只能指定列数。but =new Button();f.add(tf);f.add(but);myEvent();f.setVisible(true);}private void myEvent(){f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);     } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){//什么监听器对应什么事件按钮。 System.out.println("action ok"); } }); but.addMouseListener(new MouseAdapter(){//使用鼠标作为事件源 private int count=1; private int clikCount=1; public void mouseEntered(MouseEvent e){ System.out.println("鼠标进入到该组件"+count++);  } public void mouseCliked(MouseEvent e){ if(e.getClikCount()==2)//双击  System.out.println("双击击动作"+clikCount++); }  });  tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ int code=e.getKeyCode(); if(!(code>=KeyEvent.VK_0 && code<=KeyEvent.VK_9)){ System.out.orintln(code+"....是非法的"); e.consume();//使用此事件,以便不会按照默认的方式由产生此事件的源代码来处理此事件。就是不会进入源代码处理。在类 InputEvent中 } }  }); //给butoon添加一个键盘监听 but.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){ System.out.println(e.getKeyChar()+"...."+e.getKeyCode());//当不知道是什么意思,打印是最好的方法  } /*public void keyPressed(KeyEvent e){   if(e.getKeyCode()==KeyEvent.VK_ENTER)//按回车键退出,其它照此类比 // if(e.isControlDown()&&e.getKeyCode()==KeyEvent.VK_ENTER)//组合健 System.out.println("Ctrl+Enter is run"); System.exit(0); //System.out.println(KeyEvent.getText(e.getKeyCode())+"...."+e.getKeyCode());//打印情况为Caps Lock....20   }     */ });}public static void main(String[] args){new MousAndKeyEvent();}}//练习,列出指定目录内容import java.awt.*;import java.awt.event.*;class MyWindowDemo{private Frame f;private Button but;private TextField tf;private TextArea ta;MyWindowDemo(){init();}public void init(){f=new Frame("My window");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf=new TextField(30);but=new Button("转到")ta=new TextArea(15,40);f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);}private void myEvent(){f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);     } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){String dirPath=tf.getText();File dir =new File(dirPath);if(dir.exists() && dir.isDirectory()){ta.setText("");//填充文本,如果重新添加会覆盖原有内容。要添加一般是set和get获取。思想重要String[] names=dir.list();for(String name:names){ta.append(name+"\r\n");//添加}} } }); }public static void main(String[] args){new MyWindowDemo();}}



二、对话框和菜单
1.对话框,代码演示:
import java.awt.*;import java.awt.event.*;class MyWindowDemo{private Frame f;private Button but;private TextField tf;private TextArea ta;private Dialog d;private Label lad;private Button okBut;MyWindowDemo(){init();}public void init(){f=new Frame("My window");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf=new TextField(30);but=new Button("转到")ta=new TextArea(25,70);//为了演示方便,把对话框,放在这里。d=new Dialog(f,"提示信息-self",true);//注意这个true模式。lad=new Label();d.setBounds(400,200,200,240);d.setLayout(new FlowLayout());okBut =new Button("确定");d.add(lad);d.add(okBut);f.add(tf);f.add(but);f.add(ta);myEvent();f.setVisible(true);}private void myEvent(){d.addWindowListener(new WindowAdapter(){//对话框的public void windowClosing(WindowEvent e){d.setVisible(false);//不显示     } }); okBut.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){d.setVisible(false);//不显示 } }); tf.addKeyListener(new KeyAdapter(){ public void keyPressed(KeyEvent e){if(e.getKeyCode()==KeyEvent.VK_ENTER)showDir(); } });f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);     } }); but.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){showDir(); } }); private void showDir(){ String dirPath=tf.getText();File dir =new File(dirPath);if(dir.exists() && dir.isDirectory()){ta.setText("");//填充文本,如果重新添加会覆盖原有内容。要添加一般是set和get获取。思想重要String[] names=dir.list();for(String name:names){ta.append(name+"\r\n");//添加}}else{String info ="你输入的信息:"+dirPath+"是错误的,请重新输入。"lad.setText(info);d.setVisible(true);//显示} }}public static void main(String[] args){new MyWindowDemo();}}





0 0
原创粉丝点击