0

我正在尝试在 java swing 中制作一个简单的 UI,它会要求用户输入文本。
这是我的程序:

package practice;

import java.awt.*;
import javax.swing.*;
public class gui_demo {

     JFrame frame;
     JLabel lblEnterText;
     static JTextField textField1;
     private JTextField textField2;
    // static String userText = textField.getText();
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    gui_demo window = new gui_demo();

                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public gui_demo() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        JLabel lblEnterText = new JLabel("ENTER TEXT");
        lblEnterText.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
        lblEnterText.setHorizontalAlignment(SwingConstants.CENTER);
        lblEnterText.setBounds(46, 70, 94, 25);
        frame.getContentPane().add(lblEnterText);

        textField1 = new JTextField();
        textField1.setBounds(214, 63, 181, 41);
        frame.getContentPane().add(textField1);
        textField1.setColumns(10);

        JButton btnNewButton = new JButton("Show Result");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ae) {
                ParserTest pt = new ParserTest();
            try {
                    pt.parserAction();
                    textField2.setText(pt.result);
            } catch (Exception e) {
                JOptionPane.showMessageDialog(null,"Oops!!Error occured!");// TODO Auto-generated catch block
                e.printStackTrace();
            }

            }
        });
        btnNewButton.setFont(new Font("Comic Sans MS", Font.BOLD, 12));
        btnNewButton.setBounds(157, 135, 131, 51);
        frame.getContentPane().add(btnNewButton);

        JLabel lblFindNounsVerbs = new JLabel("Find nouns, verbs, adjectives from a given text");
        lblFindNounsVerbs.setFont(new Font("Comic Sans MS", Font.BOLD | Font.ITALIC, 12));
        lblFindNounsVerbs.setBounds(10, 11, 371, 25);
        frame.getContentPane().add(lblFindNounsVerbs);

        textField2 = new JTextField();
        textField2.setBounds(30, 197, 387, 61);
        frame.getContentPane().add(textField2);
        textField2.setColumns(10);

    }
}

我有另一个名为 parserTest 的类,它在一个句子中找到名词/动词/形容词。这是 parserTest 类:

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import opennlp.tools.cmdline.parser.ParserTool;
import opennlp.tools.parser.Parse;
import opennlp.tools.parser.Parser;
import opennlp.tools.parser.ParserFactory;
import opennlp.tools.parser.ParserModel;

public class ParserTest {

	static Set<String> nounPhrases = new HashSet<>();
	static Set<String> adjectivePhrases = new HashSet<>();
	static Set<String> verbPhrases = new HashSet<>();
	String result = new String;
	gui_demo gd = new gui_demo();
	String line = practice.gui_demo.textField1.getText();
	public ParserTest(String line) {
		// TODO Auto-generated constructor stub
	}

	public ParserTest() {
		// TODO Auto-generated constructor stub
	}

	public void getNounPhrases(Parse p) {
	    if (p.getType().equals("NN") || p.getType().equals("NNS") || p.getType().equals("NNP") || p.getType().equals("NNPS")) {
	         nounPhrases.add(p.getCoveredText());        
	    }
	    
	    if (p.getType().equals("JJ") || p.getType().equals("JJR") || p.getType().equals("JJS")) {
	    	adjectivePhrases.add(p.getCoveredText());
	    }
	    
	    if (p.getType().equals("VB") || p.getType().equals("VBP") || p.getType().equals("VBG")|| p.getType().equals("VBD") || p.getType().equals("VBN")) {
	    	verbPhrases.add(p.getCoveredText());
        }
	    
	    for (Parse child : p.getChildren()) {
	         getNounPhrases(child);
	    }
	} 	
	
	public void parserAction() throws Exception {
		InputStream is = new FileInputStream("en-parser-chunking.bin");
	    ParserModel model = new ParserModel(is);
	    Parser parser = ParserFactory.create(model);
	    Parse topParses[] = ParserTool.parseLine(line, parser, 1);
	    for (Parse p : topParses){
	    	
	    	getNounPhrases(p);
	    }
	}
	public static void main(String[] args) throws Exception {
		
		new ParserTest().parserAction();
        result = "Nouns :"+ nounPhrases + "\n" + "Verbs:" + verbPhrases +"Adjectives:" + adjectivePhrases;  
		}  
}

这两个类单独工作都很好,但我不能将它们绑定在一起。
当我运行这个程序时,它会给出以下异常..

例外

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-1 at java.lang.AbstractStringBuilder.substring(Unknown Source) at java.lang.StringBuilder.substring(Unknown Source) at opennlp.tools.cmdline.parser.ParserTool.parseLine (ParserTool.java:66) 在 practice.ParserTest.parserAction(ParserTest.java:59) 在 practice.gui_demo$2.actionPerformed(gui_demo.java:82) 在 javax.swing.AbstractButton.fireActionPerformed(Unknown Source) 在 javax.swing .AbstractButton$Handler.actionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source) at javax.swing.DefaultButtonModel.setPressed(Unknown Source) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)在 javax.swing.JComponent 的 java.awt.Component.processMouseEvent(Unknown Source)。processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(未知源)在 java.awt.LightweightDispatcher.retargetMouseEvent(未知源)在 java.awt.LightweightDispatcher.processMouseEvent(未知源)在 java.awt.LightweightDispatcher.dispatchEvent(未知源)在 java.awt.Component.dispatchEvent(未知源) ) 在 java.awt.Container.dispatchEventImpl(Unknown Source) 在 java.awt.Window.dispatchEventImpl(Unknown Source) 在 java.awt.Component.dispatchEvent(Unknown Source) 在 java.awt.EventQueue.dispatchEventImpl(Unknown Source) 在java.awt.EventQueue.access$500(Unknown Source) at java.awt.EventQueue$3。run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain $JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security .ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt .EventDispatchThread。pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

请帮我解决问题。

4

1 回答 1

0

您要查看的异常中的两行是:

practice.ParserTest.parserAction(ParserTest.java:59) at
practice.gui_demo$2.actionPerformed(gui_demo.java:82) at

您需要在动作侦听器中的第 82 行之前检查字符串长度/索引,或者在第 59 行之前检查 ParserTest.parseAction 中的错误索引

于 2016-04-18T19:53:10.717 回答