package com.nwx.txtEditor.ui; import java.awt.FlowLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class MainWindow extends JFrame { private JButton autoSaveBtn, saveBtn, cancelBtn, exitBtn, autoNextBtn; private JTextField saveDirTextField, fileNameTextField; private JTextArea fileContentArea; public JTextField getSaveDirTextField() { return saveDirTextField; } public void setSaveDirTextField(JTextField saveDirTextField) { this.saveDirTextField = saveDirTextField; } public JTextField getFileNameTextField() { return fileNameTextField; } public void setFileNameTextField(JTextField fileNameTextField) { this.fileNameTextField = fileNameTextField; } public JTextArea getFileContentArea() { return fileContentArea; } public void setFileContentArea(JTextArea fileContentArea) { this.fileContentArea = fileContentArea; } public MainWindow() { FlowLayout flowLayoutLeft = new FlowLayout(FlowLayout.LEFT, 10, 10); //FlowLayout flowLayoutCenter = new FlowLayout(FlowLayout.LEFT, 10, 10); //GridLayout gridLayout = new GridLayout(16, 1); this.setLayout(flowLayoutLeft); // button panel JPanel buttonPanel = new JPanel(flowLayoutLeft); autoSaveBtn = new JButton("autoSave"); saveBtn = new JButton("save"); cancelBtn = new JButton("cancel"); exitBtn = new JButton("exit"); autoNextBtn = new JButton("autoNext"); autoSaveBtn.addActionListener(new TxtEditorActionListener(this)); saveBtn.addActionListener(new TxtEditorActionListener(this)); cancelBtn.addActionListener(new TxtEditorActionListener(this)); exitBtn.addActionListener(new TxtEditorActionListener(this)); autoNextBtn.addActionListener(new TxtEditorActionListener(this)); buttonPanel.add(autoSaveBtn); buttonPanel.add(saveBtn); buttonPanel.add(cancelBtn); buttonPanel.add(exitBtn); buttonPanel.add(autoNextBtn); this.add(buttonPanel); // save dir panel JPanel saveDirPanel = new JPanel(flowLayoutLeft); JLabel saveDirLabel = new JLabel("Save Dir: "); saveDirTextField = new JTextField("C:/study/textEditor/textFiles/", 60); saveDirPanel.add(saveDirLabel); saveDirPanel.add(saveDirTextField); this.add(saveDirPanel); // file name panel JPanel fileNamePanel = new JPanel(flowLayoutLeft); JLabel fileNameLabel = new JLabel("FileName: "); fileNameTextField = new JTextField(60); fileNamePanel.add(fileNameLabel); fileNamePanel.add(fileNameTextField); this.add(fileNamePanel); // file content text area fileContentArea = new JTextArea(30, 65); this.add(fileContentArea); // EXIT_ON_CLOSE,关闭程序。(所有窗口和进程都会关闭) // DISPOSE_ON_CLOSE,只关闭本窗口。 // HIDE_ON_CLOSE,只隐藏本窗口,不关闭。 // DO_NOTHING_ON_CLOSE,不做任何事,点击关闭无效。 this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setSize(800, 700); this.setResizable(false); this.setVisible(true); } }
package com.nwx.txtEditor.ui; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.IOException; import java.util.Date; import javax.swing.JOptionPane; import org.apache.commons.lang3.StringUtils; import com.nwx.study.io.ReadWriteWebFile; public class TxtEditorActionListener implements ActionListener { private MainWindow window; public TxtEditorActionListener(MainWindow window) { this.window = window; } @Override public void actionPerformed(ActionEvent e) { String cmd = e.getActionCommand(); String fileNameStr = window.getFileNameTextField().getText().trim(); String saveDirStr = window.getSaveDirTextField().getText().trim(); String fileContentStr = window.getFileContentArea().getText().trim(); if ("autoSave".equals(cmd)) { if (StringUtils.isEmpty(fileNameStr) && StringUtils.isNotEmpty(fileContentStr)) { try { if (fileContentStr.indexOf("\n") > 0) { fileNameStr = fileContentStr.substring(0, fileContentStr.indexOf("\n")).trim() + ".txt"; if (fileNameStr.length() > 30) { fileNameStr = ""; } } if (StringUtils.isEmpty(fileNameStr)) { fileNameStr = (new Date()).getTime() + ".txt"; } window.getFileNameTextField().setText(fileNameStr); ReadWriteWebFile.writeFile(fileContentStr, saveDirStr + "/" + fileNameStr); JOptionPane.showMessageDialog(window, "SAVED SUCCESS", "INFO", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e1) { JOptionPane.showMessageDialog(window, "SAVED FAILED", "WARNING", JOptionPane.WARNING_MESSAGE); // e1.printStackTrace(); } } else if (StringUtils.isNotEmpty(fileNameStr) && StringUtils.isNotEmpty(fileContentStr)) { try { ReadWriteWebFile.writeFile(fileContentStr, saveDirStr + "/" + fileNameStr); JOptionPane.showMessageDialog(window, "SAVED SUCCESS", "INFO", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e1) { JOptionPane.showMessageDialog(window, "SAVED FAILED", "WARNING", JOptionPane.WARNING_MESSAGE); // e1.printStackTrace(); } } else if (StringUtils.isEmpty(fileContentStr)) { JOptionPane.showMessageDialog(window, "The fileName or fileContent is empty...", "WARNING", JOptionPane.WARNING_MESSAGE); } } else if ("save".equals(cmd)) { if (StringUtils.isEmpty(fileNameStr) || StringUtils.isEmpty(fileContentStr)) { JOptionPane.showMessageDialog(window, "The fileName or fileContent is empty...", "WARNING", JOptionPane.WARNING_MESSAGE); } else { try { ReadWriteWebFile.writeFile(fileContentStr, saveDirStr + "/" + fileNameStr); JOptionPane.showMessageDialog(window, "SAVED SUCCESS", "INFO", JOptionPane.INFORMATION_MESSAGE); } catch (IOException e1) { JOptionPane.showMessageDialog(window, "SAVED FAILED", "WARNING", JOptionPane.WARNING_MESSAGE); // e1.printStackTrace(); } } } else if("cancel".equals(cmd)){ window.getFileNameTextField().setText(""); window.getFileContentArea().setText(""); } else if ("exit".equals(cmd)) { int sure = JOptionPane.showConfirmDialog(window, "Are you sure to exit?", "WARNING", JOptionPane.YES_NO_OPTION); // System.out.println(sure); if (0 == sure) { System.exit(0); } } else if ("autoGetNext".equals(cmd)) { } } }
package com.nwx.txtEditor.ui; public class launcher { public static void main(String[] args) { new MainWindow(); } }