您的位置:首页 > 职场人生

黑马程序员 图形化界面编程

2013-12-30 18:07 253 查看
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------

GUI(Graphical User Interface)图形化界面编程

Java.Awt:Abstract Window ToolKit(抽象窗口工具包)

Javax.Swing:

图形化界面基本设置:
设置窗体大小:setSize(长,宽);
设置窗体位置:setLocation(距离左,距离上);setBounds(长,宽,距离左,距离上);
设置布局:setLayout(new FlowLayout());
使窗体可见:setVisible(true);
事件监听机制:

事件监听机制的特点:
1,事件源。
2,事件。
3,监听器。
4,事件处理。
事件源:就是awt包或者swing包中的那些图形界面组件。
事件:每一个事件源都有自己特有的对应事件和共性事件。
监听器:将可以触发某一个事件的动作(不只一个动作)都已经封装到了监听器中。
以上三者,在java中都已经定义好了。
直接获取其对象来用就可以了。
我们要做的事情是,就是对产生的动作进行处理
 
Eg:编写程序,练习图形化界面编程!
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class  MyWindowDemo
{
    private Frame f;
    privateTextField tf;
    private Buttonbut;
    private TextAreata;
   
    private Dialogd;
    private Labellab;
    private ButtonokBut;
 
 
    MyWindowDemo()
    {
       init();
    }
    public voidinit()
    {
       f = newFrame("my window");
       f.setBounds(300,100,600,500);
       f.setLayout(newFlowLayout());
       tf = newTextField(60);
       but = new Button("转到");
       ta = newTextArea(25,70);
       d = newDialog(f,"提示信息-self",true);
       d.setBounds(400,200,240,150);
       d.setLayout(newFlowLayout());
       lab = newLabel();
       okBut = newButton("确定");
 
       d.add(lab);
       d.add(okBut);
       f.add(tf);
       f.add(but);
       f.add(ta);
       myEvent();
       f.setVisible(true);
    }
    privatevoid  myEvent()
    {
 
       okBut.addActionListener(newActionListener()
       {
           publicvoid actionPerformed(ActionEvent e)
           {
              d.setVisible(false);
           }
       });
       d.addWindowListener(newWindowAdapter()
       {
           publicvoid windowClosing(WindowEvent e)
           {
              d.setVisible(false);
           }
       });
 
       tf.addKeyListener(newKeyAdapter()
       {
           publicvoid keyPressed(KeyEvent e)
           {
              if(e.getKeyCode()==KeyEvent.VK_ENTER)
                  showDir();
           }
       });
 
 
       but.addActionListener(newActionListener()
       {
           publicvoid actionPerformed(ActionEvent e)
           {
              showDir();
             
           }
       });
 
       f.addWindowListener(newWindowAdapter()
       {
           publicvoid windowClosing(WindowEvent e)
           {
              System.exit(0);  
           }
       });
    }
 
    private voidshowDir()
    {
       StringdirPath = tf.getText();
             
       File dir =new File(dirPath);
 
       if(dir.exists()&& dir.isDirectory())
       {
           ta.setText("");
           String[]names = dir.list();
           for(Stringname : names)
           {
              ta.append(name+"\r\n");
           }
       }
       else
       {
           Stringinfo = "您输入的信息:"+dirPath+"是错误的。请重输";
           lab.setText(info);
           d.setVisible(true);
       }
    }
 
    public staticvoid main(String[] args)
    {
       newMyWindowDemo();
    }
}
 
菜单:

MenuBar 菜单整体;Menu 包含于MenuBar中;MenuItem包含于Menu中!
以上3个关系添加用add();
将MenuBar放进Frame中用setMenuBar();
 
FileDialog.LOAD:打开的mode
FileDialog.Save:保存的mode
----------------------
ASP.Net+Android+IOS开发、.Net培训、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  黑马程序员