简单的Java Swing

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JProgressBar;
import javax.swing.JTextField;
import javax.swing.filechooser.FileNameExtensionFilter;

public class JavaSwing implements ActionListener
{
    
    JFrame frame = new JFrame("FRAME");
    JButton importButton;  //导入文件按钮
    JButton exportButton;  //导出文件按钮
    JButton runButton;  //程序运行按钮
    JButton openPathButton;  //打开文件目录按钮
    JButton closeButton;  //程序关闭按钮
    JTextField importField;
    JTextField exportField;
    JProgressBar bar;  //进度条
    
    public static String importFile = "";  //导入文件路径
    String importFileName = "";   //导入文件名称
    String exportPath = "";  //导出文件路径
    
    public void mainWindow()
    {
        frame.setLayout(null);  //自定义布局
        importButton = new JButton("导入");  //导入按钮
        JLabel importLabel = new JLabel("请选择需要解析的文件:");
        importField = new JTextField();
        importField.setEditable(false);
        exportButton = new JButton("导出");  //导出按钮
        JLabel exportLabel = new JLabel("请选择文件导出路径:");
        exportField = new JTextField();
        exportField.setEditable(false);
        runButton = new JButton("程序运行");
        openPathButton = new JButton("打开文件目录");
        closeButton = new JButton("关闭");
        JLabel progressLabel = new JLabel("进度信息");
        bar = new JProgressBar();  //进度条
        bar.setValue(0);  //设置进度条的初始值
        bar.setStringPainted(true);  //显示进度条中内容
        //设置窗体大小
        frame.setSize(450, 250);
        //设置各组件在窗体中的位置以及大小
        importLabel.setBounds(20, 20, 140, 20);
        importField.setBounds(170, 20, 160, 20);
        importButton.setBounds(340, 20, 80, 20);
        
        exportLabel.setBounds(20, 70, 140, 20);
        exportField.setBounds(170, 70, 160, 20);
        exportButton.setBounds(340, 70, 80, 20);
        
        runButton.setBounds(20, 120, 110, 20);
        openPathButton.setBounds(160, 120, 120, 20);
        closeButton.setBounds(310, 120, 110, 20);
        
        progressLabel.setBounds(20, 150, 410, 20);
        bar.setBounds(20, 170, 400, 20);
        //增加组件触发
        importButton.addActionListener(this);
        exportButton.addActionListener(this);
        runButton.addActionListener(this);
        openPathButton.addActionListener(this);
        closeButton.addActionListener(this);
        //添加组件
        frame.add(importLabel);
        frame.add(importButton);
        frame.add(importField);
        frame.add(exportLabel);
        frame.add(exportButton);
        frame.add(exportField);
        frame.add(runButton);
        frame.add(openPathButton);
        frame.add(closeButton);
        frame.add(progressLabel);
        frame.add(bar);
        
        frame.setResizable(false);  //设置窗体大小不可改变
        frame.setLocationRelativeTo(null);  //设置窗体放在屏幕中间
        //给窗体设置图标
        frame.setIconImage(new ImageIcon(System.getProperty("user.dir")+"\\picture\\Face_32.jpg").getImage());
        frame.setVisible(true);  //设置窗体可见
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //设置窗体在点击右上角"X"的时候,结束程序
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==importButton)  //当点击"导入"按钮
        {
            JFileChooser importChoose = new JFileChooser(".");  //文件选择对话框,设置默认路径为程序当前路径
            importChoose.setDialogTitle("请选择需要导入的文件"); //设置标题
            importChoose.setFileSelectionMode(JFileChooser.FILES_ONLY); //仅显示文件
            importChoose.setAcceptAllFileFilterUsed(false);  //过滤掉选择全部文件
            //设置指定的选择文件类型
            importChoose.addChoosableFileFilter(new FileNameExtensionFilter("2003 excel 类型","xls"));
            //显示打开文件对话框,如果isOpen为0,则为选择了路径并且保存,为1则表示选择了取消或者关闭
            int isOpen = importChoose.showOpenDialog(frame);  //设置为打开文件
            if(isOpen==0)
            {
                importFile = importChoose.getSelectedFile().toString();  //得到选择文件的路径
                int a = importFile.lastIndexOf(".");  
                importFileName = importFile.substring(0, a); //取0-a之间的字符,即为路径+文件名称
                importField.setText(importFile);
            }
            else
            {
                importFile = importField.getText();
            }
        }
        if(e.getSource()==exportButton)
        {
            //当触发了"导出"按钮,则调用exportFile()方法
            exportFile();
        }
        if(e.getSource()==runButton)
        {
            //判断导入文件和导出文件的文本框中都不为空的情况下,表明导入和导出文件都选择了
            if((null!=importFile.trim() && !"".equals(importFile.trim())) && 
               (null!=exportPath.trim() && !"".equals(exportPath.trim())))
            {
//                ResolveTMSSExport resolve = new ResolveTMSSExport(importFile,exportPath,frame,bar);
                File exportFile = new File(exportPath); //在程序中添加文件后缀
                if(exportFile.exists())  
                {
                    //如果导出的文件已经存在,弹出对话框,提示是否需要替换
                    int option = JOptionPane.showConfirmDialog(frame, "需要导出的文件已经存在,是否替换?","文件存在",JOptionPane.YES_OPTION);
                    if(JOptionPane.YES_OPTION==option)  
                    {
                        //如果需要替换,则不需要做任何操作
                    }
                    else
                    {
                        exportFile();  //如果不需要替换,就重新选择目录或者修改文件名称
                    }
                }
                else
                {
                    //如果文件不存在,则不需要做任何操作
                }
            }
            else
            {
                if(null==importFile.trim() || "".equals(importFile.trim()))
                {
                    JOptionPane.showMessageDialog(frame, "请选择需要解析的文件!");
                }
                else
                {
                    JOptionPane.showMessageDialog(frame, "请选择导出文件存放路径!");
                }
            }
        }
        if(e.getSource()==openPathButton)
        {
            if(null!=exportPath.trim() && !"".equals(exportPath.trim()))
            {
                int last = exportPath.lastIndexOf("\\");
                String openPathStr = exportPath.substring(0, last+1);
                try {
                    java.awt.Desktop.getDesktop().open(new File(openPathStr));  //打开文件存放的路径
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
            else
            {
                JOptionPane.showMessageDialog(frame, "请选择导出文件存放路径!");
            }
        }
        if(e.getSource()==closeButton) //点击"关闭"按钮
        {
            frame.setVisible(false);  //将窗体设置为不可见
            frame.dispose();  //销毁窗体
        }
    }
    /**
     * @author lKF57031
     * @描述 文件导出
     */
    private void exportFile()
    {
        SimpleDateFormat dateTime = new SimpleDateFormat("yyyyMMdd");  //将时间格式设置为"yyyyMMdd"
        JFileChooser exportChoose = new JFileChooser();
        exportChoose.addChoosableFileFilter(new FileNameExtensionFilter("2003 excel 类型","xls"));
        //设置默认的文件
        exportChoose.setSelectedFile(new File(importFileName+"_质量点灯_"+dateTime.format(new Date())));
        exportChoose.setFileSelectionMode(JFileChooser.FILES_ONLY); //仅选择路径
        //显示保存对话框,如果isOpen为0,则为选择了路径并且保存,为1则表示选择了取消或者关闭
        int isOpen = exportChoose.showSaveDialog(frame);
        if(isOpen==0){  //为0,则表示选择了"保存"
            exportPath = exportChoose.getSelectedFile().toString()+".xls";
            exportField.setText(exportPath);
        }
        else   //否则就选择了"取消"
        {
            exportPath = exportField.getText();
        }
    }
    
    public static void main(String[] args) {
        JavaSwing mainWindows = new JavaSwing();
        mainWindows.mainWindow();
    }
    

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值