【发布时间】:2016-08-10 17:27:09
【问题描述】:
我正在尝试在 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 在 java.lang.AbstractStringBuilder.substring(未知来源) 在 java.lang.StringBuilder.substring(未知来源) 在 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.processMouseEvent(未知来源) 在 java.awt.Component.processEvent(未知来源) 在 java.awt.Container.processEvent(未知来源) 在 java.awt.Component.dispatchEventImpl(未知来源) 在 java.awt.Container.dispatchEventImpl(未知来源) 在 java.awt.Component.dispatchEvent(未知来源) 在 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.run(未知来源) 在 java.awt.EventQueue$3.run(未知来源) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(未知来源) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(未知来源) 在 java.awt.EventQueue$4.run(未知来源) 在 java.awt.EventQueue$4.run(未知来源) 在 java.security.AccessController.doPrivileged(本机方法) 在 java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(未知来源) 在 java.awt.EventQueue.dispatchEvent(未知来源) 在 java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) 在 java.awt.EventDispatchThread.pumpEventsForFilter(未知来源) 在 java.awt.EventDispatchThread.pumpEventsForHierarchy(未知来源) 在 java.awt.EventDispatchThread.pumpEvents(未知来源) 在 java.awt.EventDispatchThread.pumpEvents(未知来源) 在 java.awt.EventDispatchThread.run(未知来源)
请帮我解决问题。
【问题讨论】:
-
异常发生在
ParserTool,你还没有发布。 -
好吧..实际上我是这个网站的新手..还在学习如何提问..
-
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
请添加如何绑定并运行它,以及其他类的代码。如果我们不能尝试运行它,就很难找到发生了什么。然而,看看这行代码... opennlp.tools.cmdline.parser.ParserTool.parseLine(ParserTool.java:66)
-
为什么猜测是,无论
ParserTest在String中寻找什么,它都不存在
标签: java swing stringindexoutofbounds