我一直在编写一个程序,它可以搜索一个数字列表,以找到与某个其他数字相加的那些。那里没有问题,算法虽然可能不是很有效,但很实用。
现在必须从文本文件中获取数字列表,但我一直在尝试这样做,以便用户可以将列表复制粘贴到 TextArea 中,按 Enter,然后让程序将字符串发送回普通(非 GUI)线程。
为此,我遵循了这个例子(最佳答案)。我使用的是按键事件而不是按钮按下,以及字符串而不是链接列表,但除此之外,非常相似。
我创建和运行 TextDemo 的代码(是的,我改编了一个教程程序):
/*Copy paste text in window */
public static String copypaste() throws Exception{
String text = "";
final TextDemo demo = new TextDemo();
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
demo.createAndShowGUI();
}
});
synchronized(demo.text){
while(demo.text.equals("")){ //if the window is unused
demo.text.wait();
}
text = demo.text;
}
return text;
}
TextDemo 本身(减去免责声明,请不要提醒 Oracle :)):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextDemo extends JPanel implements KeyListener{
protected JTextArea textArea;
private final static String newline = "\n";
public String text = "";
boolean used = false;
public TextDemo() {
super(new GridBagLayout());
textArea = new JTextArea(100, 30);
textArea.addKeyListener(this);
textArea.setEditable(true);
JScrollPane scrollPane = new JScrollPane(textArea);
//Add Components to this panel.
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
c.weighty = 1.0;
add(scrollPane, c);
}
public void keyPressed(KeyEvent e) {
// Listen for the key pressed and check it against "Enter"
// Then read out of our textarea control and print to screen4
if (e.getKeyCode() == e.VK_ENTER) {
synchronized(text){
text = textArea.getText();
System.out.println("Text entered.");
text.notify();
}
}
}
public void keyReleased(KeyEvent e) {
// Listen for the key pressed and check it against "Enter"
// Then read out of our textarea control and print to screen4
if (e.getKeyCode() == e.VK_ENTER) {
//do nothing
}
}
public void keyTyped(KeyEvent e) {
// Listen for the key pressed and check it against "Enter"
// Then read out of our textarea control and print to screen4
if (e.getKeyCode() == e.VK_ENTER) {
//do nothing
}
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event dispatch thread.
*/
public static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("TextDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add contents to the window.
frame.add(new TextDemo());
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event dispatch thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
当我运行代码时,它似乎可以工作,直到我按下回车键并且我的程序崩溃了。错误代码(我只包括前 5 行,完整版在这里:http: //img.photobucket.com/albums/v242/ChaosGuide/illegalmonitorstateexception.png):
Exception in thread "AWT-EventQue-0" java.lang.IllegalMonitorStateException
at java.lang.Object.notify(Native Method)
at TextDemo.keyPressed(TextDemo.java:72)
at java.awt.Component.processKeyEvent(Component.java:6463)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6282)
这是我第一次做任何涉及线程的事情,所以我真的不明白我做错了什么。
任何帮助是极大的赞赏。