JAVA-UI编程之-AWT界面编程

概要

JAVA中,界面编程从AWT,SWING,SWT,还有为富互联网(RIA)应用的JavaFX。有各自的存在道题。

AWT,最接近系统底层,比较受限系统,目前基本很少直接应用企业开发。但是更因为他接近系统层,性能上相对较高。特别是嵌入式开发的时候。也有一定的采用价值。

SWING,是在AWT的基础上,封装了更多,更适合跨平台的UI类库。目前企业应用开发比较多。因为是JDK集成的。开发的应用体积可以相对较小。

SWT是IBM基于eclipse的方式集成的一套稳定的UI框架。可以方便的开发UI应用。但是打包的应用,需要集成相关的jar包。

JavaFX的出现,是因为Oracle已经认识到了RIA技术的重要性,想把JavaFX打造成Flash和Sliverlight这样的顶级产品。然而目前H5当道。移动互联网时代FX并不是很流行.

 

AWT类关系

好了,回到主题。今天我们温习一下如何纯AWT编程。先看下AWT的几个主要类的关系图

 

JAVA-UI编程之-AWT界面编程_第1张图片

 

Component组件类下面包含容器类和控件类。容器类包含了窗口,对话框,Panel。控件包含了基础的按钮,文本标签,复选框,文本框等。一些简单的UI应用还是完全能满足的。

 

小例子

来一个简单例子。通过时间和原文,组合后MD5加密密码的例子。主要是showFrame中的添加控件(按钮,文本框,标签文本)

/**
 * 作者:     andy.zhang
 * 创建时间:2018年7月23日 下午4:14:05
 * 作用: 密码的二次加密工具
 */
package pwdtool2;

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.text.ParseException;
import java.util.Date;



/**
 * @author andy
 * @date 2018年7月23日
 */
public class Pwdtool extends Frame implements WindowListener  {



	/**
	 * 
	 */
	private static final long serialVersionUID = -2768217391703339722L;
	
	private static TextField textDate;
	private static TextField txtPwd;
	private static TextField textMd5;
	

     
	public void showFrame(){

        // 为窗口添加表单控件
		
		textDate = new TextField("");
		textDate.setText("2018-01-01 11:11:11");
		textDate.setBounds(62, 30, 197, 41);
		
		txtPwd = new TextField("");
		txtPwd.setText("cocopico123456");
		txtPwd.setBounds(62, 77, 197, 41);
		
		Button btnNewButton = new Button("");
		btnNewButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Date date;
				try {
                    // 此处是具体的处理逻辑。和本文主题没有直接关系
					//date = DateUtils.parseWithDefaultPattern(textDate.getText());
					//String md5=MDEncode.md5Encode(date.getTime()+"\n"+txtPwd.getText());
					textMd5.setText(md5);
					messageBox(md5);
				} catch (ParseException e1) {
					messageBox(e1.getMessage());

					e1.printStackTrace();
				}
				
			}
		});
		btnNewButton.setBounds(265, 75, 103, 43);
		btnNewButton.setLabel("生成加密密码");
		
		textMd5 = new TextField("");
		textMd5.setBounds(62, 124, 306, 37);
		
		Label lblNewLabel = new Label("");
		lblNewLabel.setBounds(10, 30, 32, 38);
		lblNewLabel.setFont(new Font("宋体", 1, 13));
		lblNewLabel.setText("时间");
		
		Label label = new Label("");
		label.setBounds(8, 80, 50, 38);
		label.setFont(new Font("宋体", 1, 13));
		label.setText("原字符");
		
		Label label_1 = new Label("");
		label_1.setBounds(8, 127, 48, 34);
		label_1.setFont(new Font("宋体", 1, 13));
		label_1.setText("加密后");



		this.add(label);
		this.add(label_1);
		this.add(lblNewLabel);
		this.add(textDate);
		this.add(txtPwd);
		this.add(textMd5);
		this.add(btnNewButton);
		
		this.setLayout(null);
		this.setSize(400,200);
		this.setLocation(400, 400);
		this.setVisible(true);
		this.setTitle("COCO密码工具");

		this.addWindowListener(this);

	}
	
	public void messageBox(String msg){
			final Dialog d = new Dialog(this, "提示信息", true);//弹出的对话框
	        d.setBounds(400, 200, 350, 150);//设置弹出对话框的位置和大小
	        d.setLayout(new FlowLayout());//设置弹出对话框的布局为流式布局
	        Label lab = new Label(msg);//创建lab标签填写提示内容
	        Button okBut = new Button("确定");//创建确定按钮

	        d.add(lab);//将标签添加到弹出的对话框内
	        d.add(okBut);//将确定按钮添加到弹出的对话框内。
	        
	     // 确定按钮监听器
	        okBut.addActionListener(new ActionListener() {

	            public void actionPerformed(ActionEvent e) {
	                d.setVisible(false);
	            }

	        });

	        // 对话框监听器
	        d.addWindowListener(new WindowAdapter() {
	            public void windowClosing(WindowEvent e) {

	                d.setVisible(false);//设置对话框不可见

	            }

	        });
	        d.setVisible(true);// 显示
	}

	/**
	 * 启动函数
	 * @param args
	 */
	public static void main(String[] args) {
		Pwdtool pwd=new Pwdtool();
		pwd.showFrame();

	}
	
	
	
	public void windowClosing(WindowEvent e){
        System.out.println("Closing");
        System.exit(0); //点击右上角关闭按钮
    }
    public void windowActivated(WindowEvent e1){
        System.out.println("Activated"); //每次窗口被激活
    }
    public void windowOpened(WindowEvent e2){
        System.out.println("Open"); //每次打开窗口,
    }
    
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowClosed(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowClosed(WindowEvent e) {
		System.out.println("Close"); //关闭后事件,不一定激活
		
	}
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowIconified(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowDeiconified(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}
	/* (non-Javadoc)
	 * @see java.awt.event.WindowListener#windowDeactivated(java.awt.event.WindowEvent)
	 */
	@Override
	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

}

编译启动后的界面如下:

JAVA-UI编程之-AWT界面编程_第2张图片

抛开其他框架的优越性不说。普通的UI工程还是能胜任的。导出的jar的体积也很小。

补充一

值得注意的是,AWT在Eclipse编译启动后,可能有乱码。中文字符显示一个方块。
JAVA-UI编程之-AWT界面编程_第3张图片

此时可以设置run configuration中的启动配置,Common中的编码类型

JAVA-UI编程之-AWT界面编程_第4张图片

或者在Argument中手动增加-Dfile.encoding=GBK. 后再启动。 这2个方法是一样的用途。
JAVA-UI编程之-AWT界面编程_第5张图片

 

补充二

通过elclipse的Export导出独立的jar包时,如果依赖第3方jar,可以修改jar包中的配置文件

在项目里保存了一个MANIFEST.MF.txt文件。内容如下

Manifest-Version: 1.0
Class-Path: ./ xxxx.jar
Main-Class: pwdtool2.Pwdtool

导出时选择本地MANIFEST文件。

 

你可能感兴趣的:(Java)