Java Swing编写小工具图形化界面

因为项目需要,需要写一个能够提高工作效率的小工具,业务部分主要是将一个excel的文件解析,根据规则生成对应的xml配置文件。业务代码写好后,作为一个小工具,最好是使用可视化的图形界面来操作,所以我用了Java Swing比较简单的做了个界面,主要功能有
1.可以通过文件选择对话框选择需要进行处理的文件/文件夹
2.处理信息的输出情况
3.图标的更改
工具虽然小,但是最基础的java swing操作基本都包含了,比如JFrame和JPanel的布局,JTextField、JTextArea(加入JScrollPane实现滚动条)、JLabel、JButton以及监听动作,使用Toolkit实现窗体居中显示,自定义左上角图标,showConfirmDialog确认和showMessageDialog提示框,以及JFileChooser文件选择控件。因为都比较细碎而且应用简单,所以不做详细的讲解了,下面只放出效果图和全部代码,里面都有详细注释,大家可以自己结合搜索引擎来查看。
这里写图片描述
这里写图片描述
这里写图片描述

下面放出主类的源码

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Layout;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;


public class MsgLoadFrame extends JFrame implements ActionListener {
    /**
     * @author Roy_70
     * @date 
     */
    private static final long serialVersionUID = -1189035634361220261L;
    private static Logger logger = Logger.getLogger(MsgLoadFrame.class);    
    JFrame mainframe;
    JPanel panel;
    //创建相关的Label标签
    JLabel infilepath_label = new JLabel("导入文件(Excel):");
    JLabel outfilepath_label = new JLabel("导出文件路径:");
    JLabel outlogpath_label = new JLabel("过程日志路径:");    
    //创建相关的文本域
    JTextField infilepath_textfield = new JTextField(20);
    JTextField outfilepath_textfield = new JTextField(20);
    JTextField outlogpath_textfield = new JTextField(20);
    //创建滚动条以及输出文本域
    JScrollPane jscrollPane;
    JTextArea outtext_textarea = new JTextArea();
    //创建按钮
    JButton infilepath_button = new JButton("...");
    JButton outfilepath_button = new JButton("...");
    JButton outlogpath_button = new JButton("..."); 
    JButton start_button = new JButton("开始");

    public void show(){
        mainframe = new JFrame("标题ver-1.0");
        // Setting the width and height of frame
        mainframe.setSize(575, 550);
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        mainframe.setResizable(false);//固定窗体大小

        Toolkit kit = Toolkit.getDefaultToolkit(); // 定义工具包 
        Dimension screenSize = kit.getScreenSize(); // 获取屏幕的尺寸 
        int screenWidth = screenSize.width/2; // 获取屏幕的宽
        int screenHeight = screenSize.height/2; // 获取屏幕的高
        int height = mainframe.getHeight(); //获取窗口高度
        int width = mainframe.getWidth(); //获取窗口宽度
        mainframe.setLocation(screenWidth-width/2, screenHeight-height/2);//将窗口设置到屏幕的中部             
        //窗体居中,c是Component类的父窗口
        //mainframe.setLocationRelativeTo(c);   
        Image myimage=kit.getImage("resourse/hxlogo.gif"); //由tool获取图像
        mainframe.setIconImage(myimage);
        initPanel();//初始化面板
        mainframe.add(panel);
        mainframe.setVisible(true);
    }
     /* 创建面板,这个类似于 HTML 的 div 标签
     * 我们可以创建多个面板并在 JFrame 中指定位置
     * 面板中我们可以添加文本字段,按钮及其他组件。
     */
    public void initPanel(){
        this.panel = new JPanel();
        panel.setLayout(null);
        //this.panel = new JPanel(new GridLayout(3,2)); //创建3行3列的容器     
        /* 这个方法定义了组件的位置。
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
         */
        infilepath_label.setBounds(10,20,120,25);
        infilepath_textfield.setBounds(120,20,400,25);
        infilepath_button.setBounds(520,20, 30, 25);
        this.panel.add(infilepath_label);
        this.panel.add(infilepath_textfield);
        this.panel.add(infilepath_button);

        outfilepath_label.setBounds(10,50,120,25);
        outfilepath_textfield.setBounds(120,50,400,25);
        outfilepath_button.setBounds(520,50, 30, 25);
        this.panel.add(outfilepath_label);
        this.panel.add(outfilepath_textfield);
        this.panel.add(outfilepath_button);

        outlogpath_label.setBounds(10,80,120,25);
        outlogpath_textfield.setBounds(120,80,400,25);
        outlogpath_button.setBounds(520,80, 30, 25);
        this.panel.add(outlogpath_label);
        this.panel.add(outlogpath_textfield);
        this.panel.add(outlogpath_button);

        start_button.setBounds(10,120,80,25);
        this.panel.add(start_button);

        outtext_textarea.setEditable(false);
        outtext_textarea.setFont(new Font("标楷体", Font.BOLD, 16));
        jscrollPane = new JScrollPane(outtext_textarea);
        jscrollPane.setBounds(10,170, 550, 330);
        this.panel.add(jscrollPane);
        //增加动作监听
        infilepath_button.addActionListener(this);
        outfilepath_button.addActionListener(this);
        outlogpath_button.addActionListener(this);
        start_button.addActionListener(this);
    }
    /**
     * 单击动作触发方法
     * @param event
     */
    @Override
    public void actionPerformed(ActionEvent event) {
        // TODO Auto-generated method stub
        System.out.println(event.getActionCommand());
        if(event.getSource() == start_button){
            //确认对话框弹出       
            int result = JOptionPane.showConfirmDialog(null, "是否开始转换?", "确认", 0);//YES_NO_OPTION
            if (result == 1) {//是:0,否:1,取消:2
                return;
            }
            System.out.println(infilepath_textfield.getText());
            if (infilepath_textfield.getText().equals("") || outfilepath_textfield.getText().equals("")
                    || outlogpath_textfield.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "路径不能为空", "提示", 2);//弹出提示对话框,warning
                return;
            }else{
                outtext_textarea.setText("");
                String infilepath = infilepath_textfield.getText();
                String outfilepath = outfilepath_textfield.getText();
                String outlogpath = outlogpath_textfield.getText();
                //设置log4j日志输出格式以及路径
                Layout layout = new PatternLayout("%-d{yyyy-MM-dd HH:mm:ss}  [ %C{1}--%M:%L行 ] - [ %p ]  %m%n");          
                Appender appender = null;
                try {
                    appender = new FileAppender(layout,outlogpath+"\\log.log");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }         
                logger.addAppender(appender); 
                System.out.println(outlogpath+"\\log.log");
                logger.debug("报文转换开始"); 
                /**
                 * 
                 * 
                 * 调用转换方法
                 *
                 *
                 */
                logger.debug("报文转换结束"); 
                outtext_textarea.setText("此处放输出信息(非日志)");
                result = JOptionPane.showConfirmDialog(null, "是否打开日志文件?", "确认", 0);//YES_NO_OPTION
                if (result == 0) {//是:0,否:1,取消:2
                    try {
                            @SuppressWarnings("unused")
                            Process process = Runtime.getRuntime().exec("cmd.exe  /c notepad "+outlogpath+"\\log.log");//调用cmd方法使用记事本打开文件
                        } catch (IOException e) {
                            e.printStackTrace();
                        }   
                }
            }       
        }else{
            //判断三个选择按钮并对应操作
            if(event.getSource() == infilepath_button) {
                File file = openChoseWindow(JFileChooser.FILES_ONLY);
                if(file == null)
                    return;
                infilepath_textfield.setText(file.getAbsolutePath());
                outfilepath_textfield.setText(file.getParent()+"\\out");
                outlogpath_textfield.setText(file.getParent()+"\\log");
            }else if(event.getSource() == outfilepath_button){
                File file = openChoseWindow(JFileChooser.DIRECTORIES_ONLY);
                if(file == null)
                    return;
                outfilepath_textfield.setText(file.getAbsolutePath()+"\\out");
            }else if(event.getSource() == outlogpath_button){
                File file = openChoseWindow(JFileChooser.DIRECTORIES_ONLY);
                if(file == null)
                    return;
                outlogpath_textfield.setText(file.getAbsolutePath()+"\\log");
            }           
        }
    }
    /**
     * 打开选择文件窗口并返回文件
     * @param type
     * @return
     */
    public File openChoseWindow(int type){
        JFileChooser jfc=new JFileChooser();  
        jfc.setFileSelectionMode(type);//选择的文件类型(文件夹or文件)  
        jfc.showDialog(new JLabel(), "选择");  
        File file=jfc.getSelectedFile();
        return file;
    }
    public void windowClosed(WindowEvent arg0) {         
        System.exit(0);
    } 
    public void windowClosing(WindowEvent arg0) { 
        System.exit(0);
    }
}

最后使用测试类测试

public class Msgloadtest {
    public static void main(String []args){
        MsgLoadFrame f = new MsgLoadFrame();
        f.show();
    }
}

好了这篇Swing的小例子就到这里了,谢谢大家观看。

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值