⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 natureframe.java

📁 该软件实现了正向增字最大匹配和未登陆词的识别.程序用java语言编写,界面使用简单友好.
💻 JAVA
字号:
/*
 * NatureFrame.java
 *
 * Created on 2007年5月20日, 上午9:13
 */

package cnu.nlp;

import java.awt.*;
import java.io.*;
import java.util.Scanner;

import javax.swing.*;

import org.apache.commons.io.IOUtils;

/**
 *
 * @author  gnehzuil
 */
public class NatureFrame extends javax.swing.JFrame {
    
    /** Creates new form NatureFrame */
    public NatureFrame() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        initComponents();
        initPosiztion();
        initIconImage();
        process = Process.getInstance();
    }

    private void initIconImage() {
        setIconImage(Toolkit.getDefaultToolkit().createImage("./icon.jpg"));
    }

    private void initPosiztion() throws HeadlessException {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension dim = kit.getScreenSize();        
        setLocation(dim.width/2 - getWidth()/2, dim.height/2 - getHeight()/2);
    }
    
    /** 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.
     */
    // <editor-fold defaultstate="collapsed" desc=" 生成的代码 ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabelIn = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaIn = new javax.swing.JTextArea();
        jLabelOut = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextAreaOut = new javax.swing.JTextArea();
        jButtonReset = new javax.swing.JButton();
        jButtonExit = new javax.swing.JButton();
        jButtonOpenFile = new javax.swing.JButton();
        jButtonSaveFile = new javax.swing.JButton();
        jButtonAbout = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("\u81ea\u7136\u8bed\u8a00\u5904\u7406");
        setIconImage(getIconImage());
        setName("frame");
        setResizable(false);
        jLabelIn.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelIn.setText("\u8f93\u5165");
        jLabelIn.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);

        jTextAreaIn.setColumns(20);
        jTextAreaIn.setLineWrap(true);
        jTextAreaIn.setRows(5);
        jTextAreaIn.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                jTextAreaInCaretUpdate(evt);
            }
        });

        jScrollPane1.setViewportView(jTextAreaIn);

        jLabelOut.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelOut.setText("\u8f93\u51fa");

        jTextAreaOut.setColumns(20);
        jTextAreaOut.setEditable(false);
        jTextAreaOut.setLineWrap(true);
        jTextAreaOut.setRows(5);
        jScrollPane2.setViewportView(jTextAreaOut);

        jButtonReset.setText("\u91cd\u7f6e");
        jButtonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonResetActionPerformed(evt);
            }
        });

        jButtonExit.setText("\u9000\u51fa");
        jButtonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonExitActionPerformed(evt);
            }
        });

        jButtonOpenFile.setText("\u6253\u5f00\u6587\u4ef6");
        jButtonOpenFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOpenFileActionPerformed(evt);
            }
        });

        jButtonSaveFile.setText("\u4fdd\u5b58\u6587\u4ef6");
        jButtonSaveFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSaveFileActionPerformed(evt);
            }
        });

        jButtonAbout.setText("\u5173\u4e8e");
        jButtonAbout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAboutActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelIn, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabelOut, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 324, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jButtonExit, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonOpenFile, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonSaveFile, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonAbout, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                .addGap(45, 45, 45))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(320, 320, 320)
                .addComponent(jButtonReset, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                .addGap(48, 48, 48))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addComponent(jLabelIn, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                .addGap(19, 19, 19)
                .addComponent(jButtonReset)
                .addGap(17, 17, 17)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jLabelOut, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonExit)
                    .addComponent(jButtonOpenFile)
                    .addComponent(jButtonSaveFile)
                    .addComponent(jButtonAbout))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextAreaInCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_jTextAreaInCaretUpdate
// TODO 将在此处添加您的处理代码:
        wordSegment(jTextAreaIn.getText());
    }//GEN-LAST:event_jTextAreaInCaretUpdate

    private void jButtonAboutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAboutActionPerformed
// TODO 将在此处添加您的处理代码:
        AboutDialog dialog = new AboutDialog(this, true);
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension dim = kit.getScreenSize();
        dialog.setLocation(dim.width/2 - dialog.getWidth()/2, 
                dim.height/2 - dialog.getHeight()/2);
        dialog.pack();
        dialog.setVisible(true);
    }//GEN-LAST:event_jButtonAboutActionPerformed

    private void jButtonSaveFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSaveFileActionPerformed
// TODO 将在此处添加您的处理代码:
        JFileChooser jFileChooser = new JFileChooser();
        jFileChooser.showSaveDialog(this);
        File file = jFileChooser.getSelectedFile();
        FileWriter output = null;
        try {
            output = new FileWriter(file);
            output.write(process.getResult());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            IOUtils.closeQuietly(output);
        }
    }//GEN-LAST:event_jButtonSaveFileActionPerformed

    private void jButtonOpenFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonOpenFileActionPerformed
        String inputString = readFromFile();
        if (!inputString.equals("")) {
            updateTextIn(inputString);
            wordSegment(inputString);
        }
    }//GEN-LAST:event_jButtonOpenFileActionPerformed

    private void updateTextIn(final String inputString) {
        jTextAreaIn.setText(inputString);
        jTextAreaIn.updateUI();
    }

    private String readFromFile() {
// TODO 将在此处添加您的处理代码:
        JFileChooser jFileChooser = new JFileChooser();
        jFileChooser.showOpenDialog(this);
        File file = jFileChooser.getSelectedFile();
        Scanner scanner = null;
        String inputString = "";
        try {
            scanner = new Scanner(file);
            while (scanner.hasNextLine())
                inputString += scanner.nextLine();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } finally {
            if (scanner != null)
                scanner.close();
            return inputString;
        }
    }

    private void jButtonExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonExitActionPerformed
// TODO 将在此处添加您的处理代码:
        System.exit(0);
    }//GEN-LAST:event_jButtonExitActionPerformed

    private void wordSegment(String sentence) {
// TODO 将在此处添加您的处理代码:
        process.setInputString(sentence);
        process.doProcess();
        jTextAreaOut.setText(process.getResult());
        jTextAreaOut.updateUI();
    }

    private void jButtonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonResetActionPerformed
// TODO 将在此处添加您的处理代码:
        jTextAreaIn.setText("");
    }//GEN-LAST:event_jButtonResetActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NatureFrame().setVisible(true);
            }
        });
    }
    
    private Process process;
    
    // 变量声明 - 不进行修改//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAbout;
    private javax.swing.JButton jButtonExit;
    private javax.swing.JButton jButtonOpenFile;
    private javax.swing.JButton jButtonReset;
    private javax.swing.JButton jButtonSaveFile;
    private javax.swing.JLabel jLabelIn;
    private javax.swing.JLabel jLabelOut;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextAreaIn;
    private javax.swing.JTextArea jTextAreaOut;
    // 变量声明结束//GEN-END:variables
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -