JTextArea不会正确读入文本

时间:2015-10-08 21:32:06

标签: java swing jtextarea

我似乎无法让这个工作。我的任务只会让我们使用JTextAreas。 我的代码的问题是我无法在TextArea中读取它的文本。目标是在用户输入输入后键入ENTER后运行逻辑。

当我运行代码时,我只能输入一个字符..并且GUI会在零之后显示字符,原因我无法弄清楚。例如:[0b]将在TextArea中。请帮助我无法解决这个问题。

public class ArabicToRomanGUI extends JFrame
{
private static final long serialVersionUID = 1L;
private JTextArea enterRomanNumber = new JTextArea();
JLabel label = new JLabel();
JPanel panel = new JPanel();
JFrame frame = new JFrame();


//TestArea contructor adds jtextArea to jframe
public ArabicToRomanGUI()
{
    super("Convert a Roman Numeral");
    setLayout(new FlowLayout());

    //Text field to enter a roman numeral
    enterRomanNumber = new JTextArea(1,25);
    enterRomanNumber.setText("Delete this text and Enter a Roman Numerial Here!");
    //enterRomanNumber.setAlignmentX(0);
    //enterRomanNumber.setAlignmentY(0);

    add(enterRomanNumber);

    HandlerForTextArea handler = new HandlerForTextArea();
    enterRomanNumber.addKeyListener(handler);
}

private class HandlerForTextArea implements KeyListener
{
    //used to process text field events
    @Override
    public void keyTyped(KeyEvent e) 
    {
            String userInput = "";
            userInput = enterRomanNumber.getText();
            userInput = userInput.toUpperCase();

            ConversionLogic.ConvertFromRomanToArabic(userInput); //converts user string of Roman numerals to an int in arabic
            String arabicNumberAsString = ConversionLogic.getConvertedRomanNumeral();
            enterRomanNumber.setText(arabicNumberAsString);

            //user pressed enter in JTextField enterNumberField
            if(e.getKeyCode() == KeyEvent.VK_ENTER)
            {
                //enterRomanNumber.setText(arabicNumberAsString);
                if (ConversionLogic.getCheckFail() == true)
                {
                    JOptionPane.showMessageDialog(frame, "The Roman Numeral entered is Invalid", "Error", JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(frame, "The arabic equivalent is " + arabicNumberAsString + "." , "Conversion Successful", JOptionPane.PLAIN_MESSAGE);
                }
            }
    }

    @Override
    public void keyPressed(KeyEvent e) {
        //not used

    }

    @Override
    public void keyReleased(KeyEvent e) {
        //not used

    }

}//end inner class TextFieldHandler
}//end class ArabicToRomainGUI

1 个答案:

答案 0 :(得分:4)

当您在本网站上一次又一次地阅读时,请不要使用带有KeyListener等文本组件的JTextArea,因为这可能会影响功能文本组件。相反,当您希望在DocumentListener发生后检测JTextArea状态的更改时使用DocumentFilter,或者如果您希望检测(并可能更改)文本更改,请使用JTextArea组件发布到文本组件之前。

我发现您正在使用JTextArea(1,25)或单行JTextField,这让我问:为什么不使用ActionListener?如果您这样做并希望捕获 ENTER 键,那么您只需向JTextField添加from multiprocessing import Process, Queue from time import sleep def wrap_send_email(q, msg): try: sleep(30) q.put( [ msg, 'mail sent sucessfully'] ) except: q.put( [ msg, 'mail not sent successfully'] ) def test(): q = Queue() p = Process(target=wrap_send_email, args=(q, 'test message')) p.start() while p.is_alive(): print("Doing stuff while the other process is working.") sleep(5) print(q.get()) p.join()