package comparator.ui;
import java.awt.Container;
import java.util.List;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import comparator.util.FileReaderUtil;
public class YFileChooser implements ActionListener{
/** Excel文件目录 */
public File excelDir = null;
/** Txt文件目录 */
public File txtDir = null;
JFrame frame=new JFrame("IMIX消息比对器");
JTabbedPane tabPane=new JTabbedPane();//选项卡布局
Container con=new Container();//布局1
/** 处理过程中产生的提示信息显示面板 */
JScrollPane executeInfoPane = new JScrollPane();
/** 消息对比结果信息 */
JScrollPane compareResultPane = new JScrollPane();
//Container con1=new Container();//布局2
JLabel label1=new JLabel("Excel目录:");
JLabel label2=new JLabel("Txt目录:");
JTextField text1=new JTextField();
JTextField text2=new JTextField();
JButton button1=new JButton("选择..");
JButton button2=new JButton("选择..");
JButton doCompareBtn = new JButton("比对");
JFileChooser jfc=new JFileChooser();//文件选择器
YFileChooser(){
jfc.setCurrentDirectory(new File("d:\\"));//文件选择器的初始目录定为d盘
//下面两行是取得屏幕的高度和宽度
double lx=Toolkit.getDefaultToolkit().getScreenSize().getWidth();
double ly=Toolkit.getDefaultToolkit().getScreenSize().getHeight();
frame.setLocation(new Point((int)(lx/2)-150,(int)(ly/2)-150));//设定窗口出现位置
frame.setSize(400,250);//设定窗口大小
frame.setContentPane(tabPane);//设置布局
//下面设定标签等的出现位置和高宽
label1.setBounds(10,20,70,20);
label2.setBounds(10,50,100,20);
text1.setBounds(80,20,120,20);
text2.setBounds(80,50,120,20);
button1.setBounds(210,20,70,20);
button2.setBounds(210,50,70,20);
doCompareBtn.setBounds(10, 100, 70, 20);
button1.addActionListener(this);//添加事件处理
button2.addActionListener(this);//添加事件处理
doCompareBtn.addActionListener(this);
con.add(label1);
con.add(label2);
con.add(text1);
con.add(text2);
con.add(button1);
con.add(button2);
con.add(doCompareBtn);
con.add(jfc);
tabPane.add("目录选择",con);//目录选择面板
tabPane.add("提示信息", executeInfoPane );//提示信息面板
tabPane.add("比对结果", compareResultPane);//比较结果面板
frame.setVisible(true);//窗口可见
//frame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//使能关闭窗口,结束程序
}
public void actionPerformed(ActionEvent e){//事件处理
if(e.getSource().equals(button1)){//判断触发方法的按钮是哪个
jfc.setFileSelectionMode(1);//设定只能选择到文件夹
int state=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
excelDir=jfc.getSelectedFile();//选择的Excel目录
text1.setText(excelDir.getAbsolutePath());
}
}
if(e.getSource().equals(button2)){
//jfc.setFileSelectionMode(0);//设定只能选择到文件
jfc.setFileSelectionMode(1);//设定只能选择到文件夹
int state=jfc.showOpenDialog(null);//此句是打开文件选择器界面的触发语句
if(state==1){
return;//撤销则返回
}
else{
txtDir=jfc.getSelectedFile();//选择的txt目录
text2.setText(txtDir.getAbsolutePath());
}
}
if(e.getSource().equals(doCompareBtn)){
/*if(excelDir == null){
JOptionPane.showMessageDialog(null, "Excel目录不能为空");
return;
}else if(txtDir == null){
JOptionPane.showMessageDialog(null, "Txt目录不能为空");
return;
}*/
executeInfoPane.setViewportView(getInfoList());
}
}
private JList getInfoList(){
JList infoList = new JList();
DefaultListModel listModel = new DefaultListModel();
/*File infoFile = getInfoFile();
List<String> infoLines = FileReaderUtil.getFileTxtLines(infoFile);
for(String il:infoLines){
listModel.addElement(il);
}*/
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
listModel.addElement("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
infoList.setModel(listModel);
return infoList;
}
private File getInfoFile(){
File f = new File(("file\\AllInfo.txt"));
return f;
}
public static void main(String[] args) {
new YFileChooser();
}
}