swing 中的jTextArea 能不能加监听事件? jTextField.addActionListener(new action());可以这样加监听事件请问各位大侠swing 中的jTextArea 能不能加监听事件? 要怎么写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 jTextField.addActionListener(new action());class action implements ActionListener{ public void actionPerformed(ActionEvent arg0) { 内容... } }jTextArea 我加了jTextArea1.getDocument().addDocumentListener(new Area_OnValueChanged());jTextArea1.addKeyListener(new areakey());但是还确实一个类似上面jTextField.addActionListener(new action());监听应该怎么写,最好是做过测试的代码。 监听我输入到jTextArea 的每个字 javax.swing.JTextArea jTextArea = new javax.swing.JTextArea(); jTextArea.addAncestorListener(new AncestorListener(){ public void ancestorAdded(AncestorEvent evt) { //添加处理内容 } public void ancestorMoved(AncestorEvent arg0) { } public void ancestorRemoved(AncestorEvent arg0) { } }); 我按你的方法jTextArea1.addAncestorListener(new AncestorListener(){ public void ancestorAdded(AncestorEvent evt) { System.out.println(jTextArea1.getText()); } public void ancestorMoved(AncestorEvent arg0) { } public void ancestorRemoved(AncestorEvent arg0) { } });没监听到 只用这个 jTextArea1.getDocument().addDocumentListener()就足够了 your_jtextfield.addKeyListener(new KeyListener() { //method}); import java.awt.BorderLayout;import java.awt.Dimension;import java.awt.EventQueue;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JLabel;import javax.swing.text.Document;import javax.swing.text.BadLocationException;import javax.swing.event.DocumentEvent;import javax.swing.event.DocumentListener;public class Example { public static void main(final String[] args) { EventQueue.invokeLater(new Runnable(){ public void run(){ final JFrame frame = new JFrame("Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setPreferredSize(new Dimension(800,600)); frame.setLocationRelativeTo(null); final JTextArea textinput = new JTextArea(); textinput.setLineWrap(true); final JScrollPane scroller = new JScrollPane(textinput); final JLabel status = new JLabel("Welcome!"); textinput.getDocument().addDocumentListener(new DocumentListener(){ @Override public void insertUpdate(final DocumentEvent e) { try{status.setText(e.getDocument().getText(e.getOffset(),e.getLength()));} catch(BadLocationException xe){} } @Override public void removeUpdate(final DocumentEvent e) { try{status.setText(e.getDocument().getText(e.getOffset(),e.getLength()));} catch(BadLocationException xe){} } @Override public void changedUpdate(final DocumentEvent e) { try{status.setText(e.getDocument().getText(e.getOffset(),e.getLength()));} catch(BadLocationException xe){} } }); frame.getContentPane().add(scroller); frame.getContentPane().add(status,BorderLayout.PAGE_END); frame.pack(); frame.setVisible(true); } }); }} 请教各位大侠,JTextArea都可以加哪些监听,分别是什么?TextArea监听都可以用吗? 求“01-09年中国移动短信平台业务量增长率走势”免费LINK 方法重写 请教如何通过swing写的窗口界面打开一个IE页面 简单问题,求助,大家务必进来帮个忙 对core java中的一些英文术语的不理解 实现tag接口来开发自定义标签运行不成功! 如何实现在服务器中定时操作?问题2???? IE 不能正常浏览applet? 一个有趣的问题,请大家看一个小程序,来者有分。 scjp 高手请进!!!!! 如何监听JComboBox收起时的事件 编码转换
jTextField.addActionListener(new action());
class action implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
内容...
}
}jTextArea 我加了
jTextArea1.getDocument().addDocumentListener(new Area_OnValueChanged());
jTextArea1.addKeyListener(new areakey());
但是还确实一个类似上面jTextField.addActionListener(new action());监听
应该怎么写,最好是做过测试的代码。
javax.swing.JTextArea jTextArea = new javax.swing.JTextArea();
jTextArea.addAncestorListener(new AncestorListener(){
public void ancestorAdded(AncestorEvent evt) {
//添加处理内容
} public void ancestorMoved(AncestorEvent arg0) {
} public void ancestorRemoved(AncestorEvent arg0) {
}
});
public void ancestorAdded(AncestorEvent evt) {
System.out.println(jTextArea1.getText());
} public void ancestorMoved(AncestorEvent arg0) {
} public void ancestorRemoved(AncestorEvent arg0) {
}
});没监听到
jTextArea1.getDocument().addDocumentListener()
就足够了
//method
});
import java.awt.Dimension;
import java.awt.EventQueue;import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JLabel;import javax.swing.text.Document;
import javax.swing.text.BadLocationException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;public class Example {
public static void main(final String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run(){
final JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(800,600));
frame.setLocationRelativeTo(null); final JTextArea textinput = new JTextArea();
textinput.setLineWrap(true);
final JScrollPane scroller = new JScrollPane(textinput);
final JLabel status = new JLabel("Welcome!"); textinput.getDocument().addDocumentListener(new DocumentListener(){
@Override public void insertUpdate(final DocumentEvent e) {
try{status.setText(e.getDocument().getText(e.getOffset(),e.getLength()));}
catch(BadLocationException xe){}
}
@Override public void removeUpdate(final DocumentEvent e) {
try{status.setText(e.getDocument().getText(e.getOffset(),e.getLength()));}
catch(BadLocationException xe){}
}
@Override public void changedUpdate(final DocumentEvent e) {
try{status.setText(e.getDocument().getText(e.getOffset(),e.getLength()));}
catch(BadLocationException xe){}
}
});
frame.getContentPane().add(scroller);
frame.getContentPane().add(status,BorderLayout.PAGE_END); frame.pack();
frame.setVisible(true);
}
});
}
}