ユーザーにテキストの入力を求める単純な UI を Java Swing で作成しようとしています。
これが私のプログラムです:
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 (不明なソース) で javax.swing .AbstractButton$Handler.actionPerformed (不明なソース) で javax.swing.DefaultButtonModel.fireActionPerformed (不明なソース) で javax.swing.DefaultButtonModel.setPressed (不明なソース) で javax.swing.plaf.basic.BasicButtonListener.mouseReleased (不明なソース) java.awt.Component.processMouseEvent (不明なソース) で javax.swing.JComponent で。java.awt.Component.processEvent(不明なソース) の java.awt.Component.processEvent(不明なソース) java.awt.Component.dispatchEventImpl(不明なソース) で java.awt.Container.dispatchEventImpl(ソース不明) java.awt.LightweightDispatcher.retargetMouseEvent(ソース不明) java.awt.LightweightDispatcher.processMouseEvent(ソース不明) java.awt.LightweightDispatcher.dispatchEvent(ソース不明) ) java.awt.Container.dispatchEventImpl(不明なソース) で java.awt.Window.dispatchEventImpl(不明なソース) で java.awt.Component.dispatchEvent(不明なソース) で java.awt.EventQueue.dispatchEventImpl(不明なソース) でjava.awt.EventQueue.access$500 (不明なソース) java.awt.EventQueue$3 で。java.awt.EventQueue$3 で (不明なソース) を実行します。 $JavaSecurityAccessImpl.doIntersectionPrivilege(不明なソース) java.awt.EventQueue$4.run(不明なソース) で java.awt.EventQueue$4.run(不明なソース) で java.security.AccessController.doPrivileged(Native Method) で java.security .ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(不明なソース)、java.awt.EventQueue.dispatchEvent(不明なソース)、java.awt.EventDispatchThread.pumpOneEventForFilters(不明なソース)、java.awt.EventDispatchThread.pumpEventsForFilter(不明なソース)、java.awt .EventDispatchThread.java.awt.EventDispatchThread.pumpEvents(不明なソース) の pumpEventsForHierarchy(不明なソース) java.awt.EventDispatchThread.pumpEvents(不明なソース) で java.awt.EventDispatchThread.run(不明なソース)
問題を解決するのを手伝ってください。