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);
}
}