java问题,JTextField,在输入的过程中(没有打回车),要求在另一个JLabel上同步出现输入的文字。
2个回答
展开全部
Document dt = textField.getDocument();
dt.addDocumentListener(new Swing_OnValueChanged());
class Swing_OnValueChanged implements DocumentListener { // 输出变化及结果
public void changedUpdate(DocumentEvent e) {
System.out.println("Attribute Changed" + textField.getText());
label.setText(textField.getText());
}
public void insertUpdate(DocumentEvent e) { // 输出变化及结果
System.out.println("Text Inserted:" + textField.getText());
}
public void removeUpdate(DocumentEvent e) { // 输出变化及结果
System.out.println("Text Removed:" + textField.getText());
}
}
dt.addDocumentListener(new Swing_OnValueChanged());
class Swing_OnValueChanged implements DocumentListener { // 输出变化及结果
public void changedUpdate(DocumentEvent e) {
System.out.println("Attribute Changed" + textField.getText());
label.setText(textField.getText());
}
public void insertUpdate(DocumentEvent e) { // 输出变化及结果
System.out.println("Text Inserted:" + textField.getText());
}
public void removeUpdate(DocumentEvent e) { // 输出变化及结果
System.out.println("Text Removed:" + textField.getText());
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Update extends JFrame implements KeyListener{
JTextField text = null;
JLabel label = null;
public Update(){
this.setLayout(new FlowLayout());
text = new JTextField(15);
text.addKeyListener(this);
label = new JLabel("实时更新内容的JLabel!");
this.add(text);
this.add(label);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(200,100);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Update();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
label.setText(text.getText());
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Update extends JFrame implements KeyListener{
JTextField text = null;
JLabel label = null;
public Update(){
this.setLayout(new FlowLayout());
text = new JTextField(15);
text.addKeyListener(this);
label = new JLabel("实时更新内容的JLabel!");
this.add(text);
this.add(label);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(200,100);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
new Update();
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
label.setText(text.getText());
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询