0

我想更改文本字段中的输入,该输入将立即更新到显示,而不是按下ENTER按钮来更新它。

这是我的代码。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class MyProgram01 extends JFrame
{
    private JTextField text1;
    private JCheckBox check1;
    private JCheckBox check2;
    private String message;
    private JLabel label1;
    private JLabel label2;
    private Font font;

    public MyProgram01(String title)
    {
        super(title);
        check1 = new JCheckBox("Bold");
        check2 = new JCheckBox("Italics");

        label1 = new JLabel("Text : ");
        label2 = new JLabel("Style : ");
        message = "Good Morning...";
        text1 = new JTextField(message, 100);
        font = new Font("Times New Roman", Font.PLAIN, 36);
        setBounds(0, 0, 600, 300);

        JPanel panel = new JPanel();
        panel.setLayout(null);
        panel.setBounds(0, 0, 600, 120);
        panel.setBackground(Color.ORANGE);
        label1.setFont(new Font("Times New Roman", Font.PLAIN, 36));
        label1.setBounds(15, 15, 100, 36);
        panel.add(label1);
        text1.setBounds(120, 15, 400, 36);
        panel.add(text1);

        label2.setFont(new Font("Times New Roman", Font.PLAIN, 36));
        label2.setBounds(15, 65, 100, 36);
        panel.add(label2);
        check1.setBounds(120, 65, 100, 36);
        check2.setBounds(220, 65, 100, 36);
        panel.add(check1);
        panel.add(check2);

        check1.addActionListener(new CheckBoxListener());
        check2.addActionListener(new CheckBoxListener());
        text1.addActionListener(new TextFieldListener());

        setLayout(null);
        add(panel);
    }
    public void paint(Graphics g)
    {
        super.paint(g);

        g.setFont(font);
        g.drawString(message, 15, 255);

    }
    private class CheckBoxListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if(check1.isSelected() && check2.isSelected())
            {
                font = new Font("Times New Roman", Font.BOLD + Font.ITALIC, 36);    
                repaint();
            }
            else if(check1.isSelected())
            {
                font = new Font("Times New Roman", Font.BOLD, 36);
                repaint();
            }
            else if(check2.isSelected())
            {
                font = new Font("Times New Roman", Font.ITALIC, 36);
                repaint();
            }
            else
            {
                font = new Font("Times New Roman", Font.PLAIN, 36);
                repaint();
            }
        }
    }
    private class TextFieldListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            message = text1.getText();
            repaint();
        }
    }
    public static void main(String[] args)
    {
        JFrame frame = new MyProgram01("My Program 01");
        frame.setVisible(true);
        frame.setResizable(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}    

如何更改代码以立即更新到显示?

编辑 :

它与 keyListener 一起工作,但我的程序只会在按下第二个键后开始显示。

例如,我输入,当我按下 时abc,程序将开始显示,当我按下时,它会显示并且丢失,除非我按下。abcabcENTER

这是代码的一部分:

text1.addKeyListener(new KeyAdapter()
    {
            public void keyPressed(KeyEvent e)
            {
                message = text1.getText();
                repaint();
            }
    });
4

2 回答 2

3

将 a 添加KeyListener到您的文本字段。你可以这样做:

textField.addKeyListener(new KeyAdapter(){
    @Override
    public void keyPressed(KeyEvent e){
         message = textField.getText();
         repaint();
    }
});     

或者

将 a 添加DocumentListener到您的文本字段的Document. 你可以这样做:

private JFrame getFrame(){
    return this;
}
...

textField.getDocument().addDocumentListener(new DocumentListener(){

    @Override
    public void insertUpdate(DocumentEvent e) {
        message = textField.getText();
        getFrame().repaint();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        message = textField.getText();
        getFrame().repaint();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        // on change
    }

});
于 2012-05-09T03:53:02.297 回答
2

Instead of using ActionListener for the class TextFieldListener, use KeyListener interface and use the keyTyped(KeyEvent e) method. When ever the event arises you can use getText() of texfield and repaint it.

于 2012-05-09T04:06:09.863 回答