- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package browse;
- import java.awt.Color;
- import java.io.*;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.ImageIcon;
- import javax.swing.*;
- import javax.swing.JOptionPane;
- import javax.swing.text.BadLocationException;
- /**
- *
- * @author user
- */
- public class JTextOpenSaveEx extends javax.swing.JFrame {
- /**
- * Creates new form JTextOpenSaveEx
- */
- public JTextOpenSaveEx() {
- initComponents();
- jSlider1.setValue(14);
- this.setTitle("文字編輯器");
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jMenu2 = new javax.swing.JMenu();
- jLabel1 = new javax.swing.JLabel();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTextArea1 = new javax.swing.JTextArea();
- jSlider1 = new javax.swing.JSlider();
- jMenuBar1 = new javax.swing.JMenuBar();
- jMenu1 = new javax.swing.JMenu();
- jMenuItem1 = new javax.swing.JMenuItem();
- jMenuItem2 = new javax.swing.JMenuItem();
- jMenuItem3 = new javax.swing.JMenuItem();
- EDIT = new javax.swing.JMenu();
- jMenu3 = new javax.swing.JMenu();
- jMenuItem4 = new javax.swing.JMenuItem();
- jMenuItem5 = new javax.swing.JMenuItem();
- jMenu2.setText("jMenu2");
- setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
- setMaximumSize(new java.awt.Dimension(698, 525));
- setMinimumSize(new java.awt.Dimension(698, 525));
- setPreferredSize(new java.awt.Dimension(698, 525));
- setResizable(false);
- addWindowListener(new java.awt.event.WindowAdapter() {
- public void windowClosing(java.awt.event.WindowEvent evt) {
- formWindowClosing(evt);
- }
- });
- jLabel1.setToolTipText("path");
- jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
- jTextArea1.setColumns(20);
- jTextArea1.setFont(new java.awt.Font("微軟正黑體 Light", 0, 14)); // NOI18N
- jTextArea1.setRows(5);
- jTextArea1.setTabSize(25);
- jTextArea1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jTextArea1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
- jScrollPane1.setViewportView(jTextArea1);
- jSlider1.setToolTipText("textSize");
- jSlider1.addChangeListener(new javax.swing.event.ChangeListener() {
- public void stateChanged(javax.swing.event.ChangeEvent evt) {
- jSlider1StateChanged(evt);
- }
- });
- jSlider1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
- public void mouseMoved(java.awt.event.MouseEvent evt) {
- jSlider1MouseMoved(evt);
- }
- });
- jMenuBar1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0), 2));
- jMenu1.setText("檔案");
- jMenuItem1.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.SHIFT_MASK));
- jMenuItem1.setText("開啟");
- jMenuItem1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
- jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem1ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem1);
- jMenuItem2.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK));
- jMenuItem2.setText("儲存");
- jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem2ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem2);
- jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
- jMenuItem3.setText("另存新檔");
- jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem3ActionPerformed(evt);
- }
- });
- jMenu1.add(jMenuItem3);
- jMenuBar1.add(jMenu1);
- EDIT.setText("Edit");
- EDIT.addMouseListener(new java.awt.event.MouseAdapter() {
- public void mouseClicked(java.awt.event.MouseEvent evt) {
- EDITMouseClicked(evt);
- }
- });
- jMenuBar1.add(EDIT);
- jMenu3.setText("檢視");
- jMenuItem4.setText("前景");
- jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem4ActionPerformed(evt);
- }
- });
- jMenu3.add(jMenuItem4);
- jMenuItem5.setText("背景");
- jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jMenuItem5ActionPerformed(evt);
- }
- });
- jMenu3.add(jMenuItem5);
- jMenuBar1.add(jMenu3);
- setJMenuBar(jMenuBar1);
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 690, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(3, 3, 3)
- .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 300, javax.swing.GroupLayout.PREFERRED_SIZE))
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 440, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(8, 8, 8)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGroup(layout.createSequentialGroup()
- .addGap(2, 2, 2)
- .addComponent(jSlider1, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE))))
- );
- pack();
- }// </editor-fold>
- private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser chooser2 = new JFileChooser(new File("C:\\"));
- chooser2.setDialogTitle("圖片開啟 : ");
- //
- chooser2.setFileFilter(new FileTypeFilter(".txt", "Text File"));//設置文件過濾器
- //
- int result = chooser2.showSaveDialog(null);
- if (result == JFileChooser.APPROVE_OPTION) {
- File file = chooser2.getSelectedFile();
- AbsolutePath = file.getAbsolutePath();
- jLabel1.setText(AbsolutePath);
- try {
- String txt = "";
- String txts = "";
- BufferedReader n = new BufferedReader(new InputStreamReader(new FileInputStream(file), "big5"));
- while ((txt = n.readLine()) != null) {
- txts += txt + "\n";
- }
- jTextArea1.setText(txts);
- n.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- String AbsolutePath;
- private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- BufferedWriter n = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(AbsolutePath), "big5"));
- String fileName = jTextArea1.getText();
- String[] fileNames = fileName.split("\n");
- for (String N : fileNames) {
- n.write(N + "\r\n");
- }
- n.close();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (NullPointerException e) {
- JOptionPane.showMessageDialog(null, "沒開啟文件");
- }
- }
- private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
- JFileChooser chooser2 = new JFileChooser(new File("C:\\"));
- chooser2.setDialogTitle("另存新檔 : ");
- //
- chooser2.setFileFilter(new FileTypeFilter(".txt", "Text File"));//設置文件過濾器
- //
- int result = chooser2.showSaveDialog(null);
- if (result == JFileChooser.APPROVE_OPTION) {
- File file = chooser2.getSelectedFile();
- try {
- BufferedWriter n = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file.getAbsolutePath()+".txt"), "big5"));
- String fileName = jTextArea1.getText();
- String[] fileNames = fileName.split("\n");
- for (String N : fileNames) {
- n.write(N + "\r\n");
- }
- n.close();
- } catch (FileNotFoundException e) {
- JOptionPane.showMessageDialog(null, "存取被拒", "ERROR", JOptionPane.ERROR_MESSAGE);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private void EDITMouseClicked(java.awt.event.MouseEvent evt) {
- // TODO add your handling code here:
- int is = JOptionPane.showOptionDialog(null, "確定離開 ?", "提醒", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
- if (is == JOptionPane.YES_OPTION) {
- System.exit(0);
- }
- }
- private void formWindowClosing(java.awt.event.WindowEvent evt) {
- int is = JOptionPane.showOptionDialog(null, "確定離開 ?", "提醒", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null);
- if (is == JOptionPane.YES_OPTION) {
- System.exit(0);
- }
- }
- private void jSlider1MouseMoved(java.awt.event.MouseEvent evt) {
- }
- private void jSlider1StateChanged(javax.swing.event.ChangeEvent evt) {
- size = jSlider1.getValue();
- jTextArea1.setFont(new java.awt.Font("微軟正黑體 Light", 0, size));
- }
- int size;
- private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {
- Color c = JColorChooser.showDialog(null, "請選擇顏色", Color.yellow);
- jTextArea1.setForeground(c);
- }
- private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {
- Color c = JColorChooser.showDialog(null, "請選擇顏色", Color.yellow);
- jTextArea1.setBackground(c);
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(JTextOpenSaveEx.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new JTextOpenSaveEx().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JMenu EDIT;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JMenu jMenu1;
- private javax.swing.JMenu jMenu2;
- private javax.swing.JMenu jMenu3;
- private javax.swing.JMenuBar jMenuBar1;
- private javax.swing.JMenuItem jMenuItem1;
- private javax.swing.JMenuItem jMenuItem2;
- private javax.swing.JMenuItem jMenuItem3;
- private javax.swing.JMenuItem jMenuItem4;
- private javax.swing.JMenuItem jMenuItem5;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JSlider jSlider1;
- private javax.swing.JTextArea jTextArea1;
- // End of variables declaration
- }
2017年11月22日 星期三
文字編輯器
訂閱:
張貼留言 (Atom)

沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。