Java可视化开发-WindowBuilder

一.WindowBuilder介绍

WindowBuilder 由 SWT Designer 和 Swing Designer 组成,可以非常轻松地创建 Java GUI 应用程序,而无需花费大量时间编写代码。使用所见即所得的可视化设计器和布局工具,为复杂的窗口创建简单的表单;将为您生成 Java 代码。使用拖放轻松添加控件、向控件添加事件处理程序、使用属性编辑器更改控件的各种属性、国际化您的应用程序等等。

WindowBuilder 构建为 Eclipse 和各种基于 Eclipse 的 IDE(RAD、RSA、MyEclipse、JBuilder 等)的插件。该插件构建了一个抽象语法树 (AST) 来导航源代码,并使用 GEF 来显示和管理可视化呈现。

生成的代码不需要任何额外的自定义库来编译和运行:所有生成的代码都可以在没有安装 WindowBuilder Pro 的情况下使用。WindowBuilder Pro 几乎可以读写任何格式,并且可以对大多数手写的 Java GUI 代码进行逆向工程。它还支持自由格式的代码编辑(在任何地方进行更改……不仅仅是在特殊区域)和大多数用户重构(您可以毫无问题地移动、重命名和细分方法)。

二.下载安装

因为WindowBuilder是基于Eclipse的,所有要先安装Eclipse.

如果没有,可以从Eclipse Downloads | The Eclipse Foundation下载 Eclipse.

打开eclipse,如图所示,搜索WindowBuilder并安装

Java可视化开发-WindowBuilder_第1张图片

 Java可视化开发-WindowBuilder_第2张图片

同意安装协议,然后傻瓜式安装完毕 

 三.实战

1.新建一个JavaJUI项目

Java可视化开发-WindowBuilder_第3张图片

Java可视化开发-WindowBuilder_第4张图片

2.设计窗体

Java可视化开发-WindowBuilder_第5张图片

3.成品下载

filetools.jar

4.窗体代码展示

package ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.JButton;
import javax.swing.JFileChooser;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

public class FileWindow {

	private JFrame frmv;
	private JTextField srcPath;
	private JTextField copyPath;
	private JTextField zipPath;
	private JTextField unzipPath;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FileWindow window = new FileWindow();
					window.frmv.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public FileWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmv = new JFrame();
		frmv.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				closeWindow(e);
			}
			@Override
			public void windowClosed(WindowEvent e) {
				closeWindow(e);
			}
		});
		frmv.setTitle("文件操作工具V1.0");
		frmv.setBounds(100, 100, 535, 420);
		frmv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmv.getContentPane().setLayout(null);
		
		JLabel label1 = new JLabel("源文件目录");
		label1.setHorizontalAlignment(SwingConstants.CENTER);
		label1.setBounds(110, 25, 300, 25);
		frmv.getContentPane().add(label1);
		
		srcPath = new JTextField();
		srcPath.setFont(new Font("宋体", Font.PLAIN, 16));
		srcPath.setBounds(110, 60, 300, 25);
		frmv.getContentPane().add(srcPath);
		srcPath.setColumns(10);
		
		JLabel label2 = new JLabel("操作");
		label2.setHorizontalAlignment(SwingConstants.CENTER);
		label2.setBounds(435, 25, 60, 25);
		frmv.getContentPane().add(label2);
		
		JButton deleteButton = new JButton("删除");
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doDelete(e);
			}
		});
		deleteButton.setBounds(435, 60, 60, 25);
		frmv.getContentPane().add(deleteButton);
		
		JButton btnNewButton_1 = new JButton("选择");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectSrcPath(e);
			}
		});
		btnNewButton_1.setBounds(25, 60, 60, 25);
		frmv.getContentPane().add(btnNewButton_1);
		
		JLabel label1_1 = new JLabel("复制到此目录");
		label1_1.setHorizontalAlignment(SwingConstants.CENTER);
		label1_1.setBounds(110, 105, 300, 25);
		frmv.getContentPane().add(label1_1);
		
		JButton btnNewButton_1_1 = new JButton("选择");
		btnNewButton_1_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectCopyDestPath(e);
			}
		});
		btnNewButton_1_1.setBounds(25, 140, 60, 25);
		frmv.getContentPane().add(btnNewButton_1_1);
		
		copyPath = new JTextField();
		copyPath.setFont(new Font("宋体", Font.PLAIN, 16));
		copyPath.setColumns(10);
		copyPath.setBounds(110, 140, 300, 25);
		frmv.getContentPane().add(copyPath);
		
		JButton copyButton = new JButton("复制");
		copyButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doCopy(e);
			}
		});
		copyButton.setBounds(435, 140, 60, 25);
		frmv.getContentPane().add(copyButton);
		
		JLabel label1_3 = new JLabel("压缩到此目录");
		label1_3.setHorizontalAlignment(SwingConstants.CENTER);
		label1_3.setBounds(110, 185, 300, 25);
		frmv.getContentPane().add(label1_3);
		
		JButton btnNewButton_1_3 = new JButton("选择");
		btnNewButton_1_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectZipPath(e);
			}
		});
		btnNewButton_1_3.setBounds(25, 220, 60, 25);
		frmv.getContentPane().add(btnNewButton_1_3);
		
		zipPath = new JTextField();
		zipPath.setFont(new Font("宋体", Font.PLAIN, 16));
		zipPath.setColumns(10);
		zipPath.setBounds(110, 220, 300, 25);
		frmv.getContentPane().add(zipPath);
		
		JButton zipButton = new JButton("压缩");
		zipButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doZip(e);
			}
		});
		zipButton.setBounds(435, 220, 60, 25);
		frmv.getContentPane().add(zipButton);
		
		JLabel label1_4 = new JLabel("解压到此目录");
		label1_4.setHorizontalAlignment(SwingConstants.CENTER);
		label1_4.setBounds(110, 265, 300, 25);
		frmv.getContentPane().add(label1_4);
		
		JButton btnNewButton_1_4 = new JButton("选择");
		btnNewButton_1_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				selectUnzipPath(e);
			}
		});
		btnNewButton_1_4.setBounds(25, 300, 60, 25);
		frmv.getContentPane().add(btnNewButton_1_4);
		
		unzipPath = new JTextField();
		unzipPath.setFont(new Font("宋体", Font.PLAIN, 16));
		unzipPath.setColumns(10);
		unzipPath.setBounds(110, 300, 300, 25);
		frmv.getContentPane().add(unzipPath);
		
		JButton unzipButton = new JButton("解压");
		unzipButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				doUnzip(e);
			}
		});
		unzipButton.setBounds(435, 300, 60, 25);
		frmv.getContentPane().add(unzipButton);
	}

	protected void closeWindow(WindowEvent e) {
        frmv.dispose();
        System.exit(0);	
	}

	protected void doUnzip(ActionEvent e) {
		try {
			JavaUnZip.unZip(new File(this.srcPath.getText()), new File(this.unzipPath.getText()));
			JOptionPane.showMessageDialog(null, "解压缩成功", "提示", JOptionPane.DEFAULT_OPTION);
		} catch (IOException e1) {
			JOptionPane.showMessageDialog(null, "解压缩失败:"+e1.getMessage(), "提示", JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
		}
		
	}

	protected void doZip(ActionEvent e) {
		try {
			File src = new File(this.srcPath.getText());
			File zip = new File(this.zipPath.getText(),src.getName()+".zip");
			JavaZip.zip(src, zip);
			JOptionPane.showMessageDialog(null, "压缩成功", "提示", JOptionPane.DEFAULT_OPTION);
		} catch (IOException e1) {
			JOptionPane.showMessageDialog(null, "压缩失败:"+e1.getMessage(), "提示", JOptionPane.ERROR_MESSAGE);
			e1.printStackTrace();
		}
		
	}

	protected void doCopy(ActionEvent e) {
		try {
			File src = new File(this.srcPath.getText());
			File dest = new File(this.copyPath.getText());
			JavaFile.copy(src, dest);
			JOptionPane.showMessageDialog(null, "复制成功", "提示", JOptionPane.DEFAULT_OPTION);
		} catch (IOException e1) {
			e1.printStackTrace();
			JOptionPane.showMessageDialog(null, "复制失败:"+e1.getMessage(), "提示", JOptionPane.ERROR_MESSAGE);
		}
		
	}

	protected void doDelete(ActionEvent e) {
		JavaFile.delete(new File(this.srcPath.getText()));
		JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.DEFAULT_OPTION);
	}

	protected void selectUnzipPath(ActionEvent e) {
		this.unzipPath.setText(selectPath("选择解压缩到哪个目录"));		
	}

	protected void selectZipPath(ActionEvent e) {
		this.zipPath.setText(selectPath("选择压缩到哪个目录"));	
	}

	protected void selectCopyDestPath(ActionEvent e) {
		this.copyPath.setText(selectPath("选择复制到哪个目录"));
		
	}

	protected void selectSrcPath(ActionEvent e) {
		this.srcPath.setText(selectPath("选择源文件路径"));		
	}
	
    /**
     * 选择路径
     *
     * @param title
     */
    private String selectPath(String title) {
        JFileChooser jfc = new JFileChooser();
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        jfc.showDialog(new JLabel(), title);
        File file = jfc.getSelectedFile();
        if (file == null) {
            return null;
        }
        return file.getPath();
    }
}

你可能感兴趣的:(框架学习,java)