description h3>
- Java USES the same event listener for the same type of component to simplify the program and avoid writing duplicate code.
- distinguishes different components by setting the name.
program code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextEve2 {
public static void main(String[] args) {
JFrame jf=new JFrame("test event");
//创建多个同类型的事件源
TextField tf1=new TextField(30);
tf1.setName("tf1");
TextField tf2=new TextField(30);
tf2.setName("tf2");
//注册事件监听器
tf1.addTextListener(new MyTextListener());
tf2.addTextListener(new MyTextListener());
//添加组件
Component strut = Box.createVerticalStrut(5);
Box vBox = Box.createVerticalBox();
vBox.add(tf1);
vBox.add(strut);
vBox.add(tf2);
JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT,5,10));
panel.add(vBox);
jf.add(panel);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
//自定义事件监听器
private static class MyTextListener implements TextListener{
@Override
public void textValueChanged(TextEvent e) {
TextField tf = (TextField) e.getSource();
//根据不同事件源,执行不同的逻辑
String name = tf.getName();
switch (name){
case "tf1":
System.out.println("文本1内容= "+tf.getText());
break;
case "tf2":
System.out.println("文本2内容= "+tf.getText());
break;
default:
break;
}
}
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class TextEve2 {
public static void main(String[] args) {
JFrame jf=new JFrame("test event");
//创建多个同类型的事件源
TextField tf1=new TextField(30);
tf1.setName("tf1");
TextField tf2=new TextField(30);
tf2.setName("tf2");
//注册事件监听器
tf1.addTextListener(new MyTextListener());
tf2.addTextListener(new MyTextListener());
//添加组件
Component strut = Box.createVerticalStrut(5);
Box vBox = Box.createVerticalBox();
vBox.add(tf1);
vBox.add(strut);
vBox.add(tf2);
JPanel panel=new JPanel(new FlowLayout(FlowLayout.LEFT,5,10));
panel.add(vBox);
jf.add(panel);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.pack();
jf.setVisible(true);
}
//自定义事件监听器
private static class MyTextListener implements TextListener{
@Override
public void textValueChanged(TextEvent e) {
TextField tf = (TextField) e.getSource();
//根据不同事件源,执行不同的逻辑
String name = tf.getName();
switch (name){
case "tf1":
System.out.println("文本1内容= "+tf.getText());
break;
case "tf2":
System.out.println("文本2内容= "+tf.getText());
break;
default:
break;
}
}
}
}
p>
div>
Read More:
- [Solved] VUE3.0 Warning: Added non-passive event listener to a scroll-blocking ‘mousewheel‘ event如何解决
- Listener refused the connection with the following error: ORA-12505, TNS:listener does not currently
- Failed to convert value of type ‘java.lang.String‘ to required type ‘java.util.Date‘;
- No converter found capable of converting from type [java.lang.String] to type [java.util.Map<java.la
- Event resolution of component {b3118b2-1f49-48e5-b6f5-bc21caec56fb}
- Error: (22,57) Java: type parameter… Mapper is not in the range of type variable M
- Unknown custom element: <component> – did you register the component correctly
- Idea start error: java.lang.IllegalStateException : failed to create a child event loop
- Resolve the problem of “event ID 4107” or “event ID 11” errors recorded in the application logs of windows and windows server
- Java gets the type t.class of generic t
- The Vue parent component uses ref to call the sub component method and reports an error
- Problem solving of failed to read candidate component class in Java
- Error type referred to is not an annotation type:RedisCache
- Cause: java.sql.SQLException: invalid column type: 1111 Error [How to Fix]
- Eclipse software Java solution: editor does not contain a main type error box
- Failed to set up listener: socketexception: address already in use
- Solution to Tomcat starting error listener start
- Error:Failed to notify project evaluation listener.
- Java long type error: error: integer number too large
- Oracle11gr2 database suddenly “TNS no listener” protocol adapter error