【java swing 编程】文件替换小秘书(二)

界面布局的代码如下:

package frame;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import panel.FTPServerPanel;
import panel.FilePathPanel;
import panel.LogPanel;

import util.CommonBean;
import util.CommonUtil;

public class JTabFrame extends JFrame{
	private static final long serialVersionUID = 1L;
	
	private static JTabFrame jTabFrame = null;
	private JTabbedPane tabbedPane = null;
	private FilePathPanel filePathPanel = null;
	private FTPServerPanel fptServerPanel = null;
	private LogPanel logPanel = null;
	
	public JTabFrame(String title) {
		super(title);
	}
	
	public static JTabFrame getIns(String title) {
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		if(jTabFrame == null) {
			jTabFrame = new JTabFrame(title);
		}
		
		return jTabFrame;
	}
	
	public void init() {
		//创建标签页
		if(tabbedPane == null) {
			tabbedPane = new JTabbedPane(JTabbedPane.LEFT , JTabbedPane.SCROLL_TAB_LAYOUT);
		}
		logPanel = new LogPanel();
		
		fptServerPanel = new FTPServerPanel();
		//查找服务器的配置信息缓存文件并将缓存文件中的信息设置到到文本框中
		CommonBean comBean = CommonUtil.getServerInfo();
		String[] hostNameArr = comBean.getHostName().split("\\.");
		fptServerPanel.getHostName1().setText(hostNameArr[0]);
		fptServerPanel.getHostName2().setText(hostNameArr[1]);
		fptServerPanel.getHostName3().setText(hostNameArr[2]);
		fptServerPanel.getHostName4().setText(hostNameArr[3]);
		fptServerPanel.getPort().setText(comBean.getPort());
		fptServerPanel.getUserName().setText(comBean.getUserName());
		fptServerPanel.getPassword().setText(comBean.getPassword());
		if(comBean.getMsgBuff().length() > 0) {
			logPanel.getLogArea().setText(logPanel.getLogArea().getText() + comBean.getMsgBuff().toString());
			comBean.getMsgBuff().setLength(0);
		}
		tabbedPane.addTab("服务器信息", CommonUtil.getImg("image/info.png"), fptServerPanel , "服务器基本连接信息");
		
		filePathPanel = new FilePathPanel();
		comBean = CommonUtil.getPathInfo();
		filePathPanel.addCom(comBean.getPathList());
		if(comBean.getMsgBuff().length() > 0) {
			logPanel.getLogArea().setText(logPanel.getLogArea().getText() + comBean.getMsgBuff().toString());
			comBean.getMsgBuff().setLength(0);
		}
		tabbedPane.addTab("路径配置   ", CommonUtil.getImg("image/conf.png"), filePathPanel , "本地与服务器文件路径的映射");
		tabbedPane.addTab("日志             ", CommonUtil.getImg("image/log.png"), logPanel , "操作日志信息");
		//为JTabbedPane添加事件监听器  
		tabbedPane.addChangeListener(new ChangeListener() 
		{  
			@Override
			public void stateChanged(ChangeEvent e) {
				//如果被选择的组件依然是空  
				if (tabbedPane.getSelectedComponent() == null)  
				{  
					//为指定标前页加载内容  
					tabbedPane.setSelectedIndex(tabbedPane.getSelectedIndex());  
				}
			}  
		});
		
		jTabFrame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		//增加操作的按钮
		JPanel buttonPanel = new JPanel();
		//配置信息的操作按钮
		JButton saveBtn = new JButton("保存配置信息",new ImageIcon("image/save.png"));
		saveBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StringBuffer msg = CommonUtil.validateInfo(fptServerPanel, filePathPanel);
				if(msg.length() > 0) {
					CommonUtil.showMsgWin("检查出存在问题的配置信息如下:\n"+msg.toString(), JOptionPane.ERROR_MESSAGE);
				} 
				//当路径映射配置没有问题的情况下保存配置信息至缓存文件中
				else {
					//服务器信息的保存
					msg = CommonUtil.saveServerInfo(fptServerPanel);
					if(msg.length() > 0) {
						logPanel.getLogArea().setText(logPanel.getLogArea().getText() + msg.toString());
						msg.setLength(0);
						
						CommonUtil.showMsgWin("服务器配置信息保存失败,请查看日志!", JOptionPane.ERROR_MESSAGE);
					} else {
						//路径的映射信息的保存
						msg = CommonUtil.savePathInfo(filePathPanel.getPathList());
						if(msg.length() > 0) {
							logPanel.getLogArea().setText(logPanel.getLogArea().getText() + msg.toString());
							msg.setLength(0);
							
							CommonUtil.showMsgWin("路径映射配置信息保存失败,请查看日志!", JOptionPane.ERROR_MESSAGE);
						} else {
							CommonUtil.showMsgWin("恭喜,配置信息保存成功,亲!", JOptionPane.INFORMATION_MESSAGE);
						}
					}
				}
			}
			
		});
		buttonPanel.add(saveBtn);
		
		//文件替换的操作按钮
		JButton uploadBtn = new JButton("替换本地文件至服务器",new ImageIcon("image/up.png"));
		uploadBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StringBuffer msg = CommonUtil.validateInfo(fptServerPanel, filePathPanel);
				if(msg.length() > 0) {
					CommonUtil.showMsgWin("检查出存在问题的配置信息如下:\n"+msg.toString(), JOptionPane.ERROR_MESSAGE);
				} 
				//当路径映射配置没有问题的情况下就根据配置的主机信息连接服务器进行操作
				else {
					//再连接服务器进行操作
					StringBuffer operBuff = new StringBuffer();
					boolean success = CommonUtil.upLoadFile(operBuff, fptServerPanel, filePathPanel.getPathList());
					logPanel.getLogArea().setText(logPanel.getLogArea().getText() + operBuff.toString());
					if(success) {
						CommonUtil.showMsgWin("恭喜,替换本地文件至服务器成功,亲!", JOptionPane.INFORMATION_MESSAGE);
					} else {
						CommonUtil.showMsgWin("替换本地文件至服务器过程失败,请查看日志!", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			
		});
		buttonPanel.add(uploadBtn);
		
		//文件替换的操作按钮
		JButton downLoadBtn = new JButton("下载服务器文件至本地",new ImageIcon("image/down.png"));
		downLoadBtn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				StringBuffer msg = CommonUtil.validateInfo(fptServerPanel, filePathPanel);
				if(msg.length() > 0) {
					CommonUtil.showMsgWin("检查出存在问题的配置信息如下:\n"+msg.toString(), JOptionPane.ERROR_MESSAGE);
				}//当路径映射配置没有问题的情况下就根据配置的主机信息连接服务器进行操作
				else {
					
					//再连接服务器进行操作
					StringBuffer operBuff = new StringBuffer();
					boolean success = CommonUtil.downLoadFile(operBuff, fptServerPanel, filePathPanel.getPathList());
					logPanel.getLogArea().setText(logPanel.getLogArea().getText() + operBuff.toString());
					if(success) {
						CommonUtil.showMsgWin("恭喜,下载服务器文件至本地成功,亲!", JOptionPane.INFORMATION_MESSAGE);
					} else {
						CommonUtil.showMsgWin("下载服务器文件至本地过程失败,请查看日志!", JOptionPane.ERROR_MESSAGE);
					}
				}
			}
			
		});
		buttonPanel.add(downLoadBtn);
		
		jTabFrame.add(buttonPanel, BorderLayout.SOUTH);
		
		jTabFrame.setSize(1200, 600);
		jTabFrame.setResizable(false);
		jTabFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		jTabFrame.setVisible(true);
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值