当前位置: 技术问答>java相关
有两个JTextField,当在其中一个JTextField输入字符串时,另一个JTextField里也出现相同字符串,能给出代码吗,一定给分!!!
来源: 互联网 发布时间:2015-02-06
本文导语: 我用DocumentListener无法实现啊,报错: java.lang.IllegalStateException: Attempt to mutate in notification java.lang.Throwable(java.lang.String) java.lang.Exception(java.lang.String) java.lang.RuntimeException(java.lang.String) java.lang.IllegalStateException(java....
我用DocumentListener无法实现啊,报错:
java.lang.IllegalStateException: Attempt to mutate in notification
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
java.lang.RuntimeException(java.lang.String)
java.lang.IllegalStateException(java.lang.String)
void javax.swing.text.AbstractDocument.writeLock()
void javax.swing.text.AbstractDocument.remove(int, int)
void javax.swing.text.JTextComponent.setText(java.lang.String)
void com.eastpro.massmutual.FmBeneficiaryd.insertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.fireInsertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.insertString(int, java.lang.String, javax.swing.text.AttributeSet)
void javax.swing.text.JTextComponent.setText(java.lang.String)
void com.eastpro.massmutual.FmBeneficiaryd.insertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.fireInsertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.insertString(int, java.lang.String, javax.swing.text.AttributeSet)
void javax.swing.text.JTextComponent.replaceSelection(java.lang.String)
void javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(java.awt.event.ActionEvent)
void javax.swing.text.JTextComponent.processComponentKeyEvent(java.awt.event.KeyEvent)
void javax.swing.JComponent.processKeyEvent(java.awt.event.KeyEvent)
void javax.swing.text.JTextComponent.replaceInputMethodText(java.awt.event.InputMethodEvent)
void javax.swing.text.JTextComponent.processInputMethodEvent(java.awt.event.InputMethodEvent)
void java.awt.Component.processEvent(java.awt.AWTEvent)
void java.awt.Container.processEvent(java.awt.AWTEvent)
void java.awt.Component.dispatchEventImpl(java.awt.AWTEvent)
void java.awt.Container.dispatchEventImpl(java.awt.AWTEvent)
void java.awt.Component.dispatchEvent(java.awt.AWTEvent)
void java.awt.EventQueue.dispatchEvent(java.awt.AWTEvent)
boolean java.awt.EventDispatchThread.pumpOneEvent()
void java.awt.EventDispatchThread.pumpEvents(java.awt.Conditional)
void java.awt.EventDispatchThread.run()
java.lang.IllegalStateException: Attempt to mutate in notification
java.lang.Throwable(java.lang.String)
java.lang.Exception(java.lang.String)
java.lang.RuntimeException(java.lang.String)
java.lang.IllegalStateException(java.lang.String)
void javax.swing.text.AbstractDocument.writeLock()
void javax.swing.text.AbstractDocument.remove(int, int)
void javax.swing.text.JTextComponent.setText(java.lang.String)
void com.eastpro.massmutual.FmBeneficiaryd.insertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.fireInsertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.insertString(int, java.lang.String, javax.swing.text.AttributeSet)
void javax.swing.text.JTextComponent.setText(java.lang.String)
void com.eastpro.massmutual.FmBeneficiaryd.insertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.fireInsertUpdate(javax.swing.event.DocumentEvent)
void javax.swing.text.AbstractDocument.insertString(int, java.lang.String, javax.swing.text.AttributeSet)
void javax.swing.text.JTextComponent.replaceSelection(java.lang.String)
void javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(java.awt.event.ActionEvent)
void javax.swing.text.JTextComponent.processComponentKeyEvent(java.awt.event.KeyEvent)
void javax.swing.JComponent.processKeyEvent(java.awt.event.KeyEvent)
void javax.swing.text.JTextComponent.replaceInputMethodText(java.awt.event.InputMethodEvent)
void javax.swing.text.JTextComponent.processInputMethodEvent(java.awt.event.InputMethodEvent)
void java.awt.Component.processEvent(java.awt.AWTEvent)
void java.awt.Container.processEvent(java.awt.AWTEvent)
void java.awt.Component.dispatchEventImpl(java.awt.AWTEvent)
void java.awt.Container.dispatchEventImpl(java.awt.AWTEvent)
void java.awt.Component.dispatchEvent(java.awt.AWTEvent)
void java.awt.EventQueue.dispatchEvent(java.awt.AWTEvent)
boolean java.awt.EventDispatchThread.pumpOneEvent()
void java.awt.EventDispatchThread.pumpEvents(java.awt.Conditional)
void java.awt.EventDispatchThread.run()
|
这是你所需要的,看看吧
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//
public class TextTest extends JApplet implements DocumentListener
{
private JTextField leftText;
private JTextField rightText;
public void init()
{
leftText = new JTextField();
leftText.setBounds(70,50,100,20);
rightText = new JTextField(20);
rightText.setBounds(70,90,100,20);
leftText.getDocument().addDocumentListener(this);
getContentPane().setLayout(null);
getContentPane().add(leftText);
getContentPane().add(rightText);
}
public void changedUpdate(DocumentEvent event){}
public void insertUpdate(DocumentEvent event)
{
rightText.setText(leftText.getText());
}
public void removeUpdate(DocumentEvent event)
{
rightText.setText(leftText.getText());
}
}
产生这个类文件后,直接使用appletviewer TextTest.java
就可以直接看了,希望这符合你的要求
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
//
public class TextTest extends JApplet implements DocumentListener
{
private JTextField leftText;
private JTextField rightText;
public void init()
{
leftText = new JTextField();
leftText.setBounds(70,50,100,20);
rightText = new JTextField(20);
rightText.setBounds(70,90,100,20);
leftText.getDocument().addDocumentListener(this);
getContentPane().setLayout(null);
getContentPane().add(leftText);
getContentPane().add(rightText);
}
public void changedUpdate(DocumentEvent event){}
public void insertUpdate(DocumentEvent event)
{
rightText.setText(leftText.getText());
}
public void removeUpdate(DocumentEvent event)
{
rightText.setText(leftText.getText());
}
}
产生这个类文件后,直接使用appletviewer TextTest.java
就可以直接看了,希望这符合你的要求
|
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class Applet1 extends JApplet {
boolean isStandalone = false;
JTextField jTextField1 = new JTextField();
JTextField jTextField2 = new JTextField();
/**Get a parameter value*/
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
}
/**Construct the applet*/
public Applet1() {
}
/**Initialize the applet*/
public void init() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**Component initialization*/
private void jbInit() throws Exception {
jTextField1.setText("jTextField1");
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyTyped(KeyEvent e) {
jTextField1_keyTyped(e);
}
});
this.setSize(new Dimension(400,300));
jTextField2.setText("jTextField2");
this.getContentPane().add(jTextField1, BorderLayout.NORTH);
this.getContentPane().add(jTextField2, BorderLayout.SOUTH);
}
/**Get Applet information*/
public String getAppletInfo() {
return "Applet Information";
}
/**Get parameter info*/
public String[][] getParameterInfo() {
return null;
}
//static initializer for setting look & feel
static {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
//UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
}
catch(Exception e) {
}
}
void jTextField1_keyTyped(KeyEvent e) {
jTextField2.setText(jTextField1.getText());
}
}
|
/*
* tmp.java
*
* Created on 2001年9月26日, 上午10:39
*/
/**
*
* @author administrator
*/
public class tmp extends javax.swing.JFrame {
/** Creates new form tmp */
public tmp() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
getContentPane().setLayout(new java.awt.FlowLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jTextField1.setColumns(20);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTextField1KeyPressed(evt);
}
});
getContentPane().add(jTextField1);
jTextField2.setColumns(20);
getContentPane().add(jTextField2);
pack();
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
// Add your handling code here:
jTextField2.setText(jTextField1.getText());
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new tmp().show();
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
* tmp.java
*
* Created on 2001年9月26日, 上午10:39
*/
/**
*
* @author administrator
*/
public class tmp extends javax.swing.JFrame {
/** Creates new form tmp */
public tmp() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
jTextField1 = new javax.swing.JTextField();
jTextField2 = new javax.swing.JTextField();
getContentPane().setLayout(new java.awt.FlowLayout());
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
jTextField1.setColumns(20);
jTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
jTextField1.addKeyListener(new java.awt.event.KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent evt) {
jTextField1KeyPressed(evt);
}
});
getContentPane().add(jTextField1);
jTextField2.setColumns(20);
getContentPane().add(jTextField2);
pack();
}
private void jTextField1KeyPressed(java.awt.event.KeyEvent evt) {
// Add your handling code here:
jTextField2.setText(jTextField1.getText());
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here:
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new tmp().show();
}
// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
// End of variables declaration
}
|
监听keyEvent,每type一下就更改另一个textfield中内容。