Java 焦点事件监听器
Java Focus Event Listener
我想知道如何添加获得焦点的事件侦听器。
目前我有一个鼠标事件正在添加到我的
JTextareas
//=======================================================
// mouse drag event
//=======================================================
public static class genDrag extends MouseMotionAdapter {
JTextArea textarea;
// receive textarea as argument
public genDrag(JTextArea argTextarea) {
textarea = argTextarea;
}
// add drag functionality to argument
public void mouseDragged(MouseEvent E) {
Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
}
}
然后我可以使用
调用它
//=======================================================
// apply mouse event
//=======================================================
JTextArea textarea = new JTextArea();
textarea.setBounds(50, 50, 100, 30);
textarea.addMouseMotionListener(new genDrag(textarea));
这很好用,但我一直无法重现
focusGained 事件的功能
//=======================================================
// mouse focus event
//=======================================================
public static class genFocus extends EventListener {
JTextArea textarea;
public genFocus() {
textarea = argTextarea;
}
public void focusGained(FocusEvent E) {
System.out.println("Focus Triggered");
}
}
以上好像一点都不开心
更新代码
static gui classGui;
public static void main(String[] args) {
classGui = new gui();
classGui.textarea.addMouseMotionListener(
new genDrag(classGui.textarea)
);
classGui.textarea.addFocusListener(
new genFocus(this)
);
classGui.frame.setVisible(true);
public static class gui {
JFrame frame;
JPanel panel;
JTextArea textarea;
public gui() {
frame = new JFrame();
// configure JFrame here
panel = new JPanel();
// configure JPanel here
textarea = new JTextArea();
textarea.setBounds(50, 50, 100, 30);
frame.add(textarea);
}
}
public static class genDrag extends MouseMotionAdapter {
JTextArea textarea;
public genDrag(JTextArea argTextarea) {
textarea = argTextarea;
}
public void mouseDragged(MouseEvent E) {
Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
}
}
public static class genFocus implements FocusListener {
JTextArea textarea;
public genFocus(JTextArea argTextarea) {
textarea = argTextarea;
}
public void focusGained(FocusEvent E) {
System.out.println("Focus gained");
}
public void focusLost(FocusEvent E) {
System.out.println("Focus lost");
}
}
}
我认为 this 正是您所需要的...
提示:您的 class genFocus(更喜欢遵循代码约定:GenFocus)应该实现 FocusListener。
你应该添加一个 event-listener
到控件 JTextArea
然后只有它能够处理任何 event
请求。
JTextField textarea= new JTextField("Value");
textarea.addFocusListener(new genFocus(textarea)); //this peice of code will add an listener to you textarea Object of JTextField.
您的鼠标侦听器将会工作,因为您已将鼠标事件侦听器添加到 JTextArea
。
textarea.addMouseMotionListener(new genDrag(textarea));//code to add MouseMotionListener.
但是没有 FocusEvent
注册到您的 JTextArea
。
谢谢。
要处理焦点事件,您的处理程序需要实现 FocusListener
接口而不是 EventListener
。
请注意,您需要通过 addFocusListener
添加此处理程序。我不认为你这样做了,因为如果你这样做了,你会得到一个编译器错误,指出哪里出了问题。
使用 @Override
注释有助于发现此类错误。把它放在你认为应该覆盖父方法的每个方法之上。如果这样的方法实际上没有覆盖另一个方法,编译器将抛出错误。这样您就可以获悉错误,而不是您的程序默默地失败。
我想知道如何添加获得焦点的事件侦听器。
目前我有一个鼠标事件正在添加到我的 JTextareas
//=======================================================
// mouse drag event
//=======================================================
public static class genDrag extends MouseMotionAdapter {
JTextArea textarea;
// receive textarea as argument
public genDrag(JTextArea argTextarea) {
textarea = argTextarea;
}
// add drag functionality to argument
public void mouseDragged(MouseEvent E) {
Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
}
}
然后我可以使用
调用它 //=======================================================
// apply mouse event
//=======================================================
JTextArea textarea = new JTextArea();
textarea.setBounds(50, 50, 100, 30);
textarea.addMouseMotionListener(new genDrag(textarea));
这很好用,但我一直无法重现 focusGained 事件的功能
//=======================================================
// mouse focus event
//=======================================================
public static class genFocus extends EventListener {
JTextArea textarea;
public genFocus() {
textarea = argTextarea;
}
public void focusGained(FocusEvent E) {
System.out.println("Focus Triggered");
}
}
以上好像一点都不开心
更新代码
static gui classGui;
public static void main(String[] args) {
classGui = new gui();
classGui.textarea.addMouseMotionListener(
new genDrag(classGui.textarea)
);
classGui.textarea.addFocusListener(
new genFocus(this)
);
classGui.frame.setVisible(true);
public static class gui {
JFrame frame;
JPanel panel;
JTextArea textarea;
public gui() {
frame = new JFrame();
// configure JFrame here
panel = new JPanel();
// configure JPanel here
textarea = new JTextArea();
textarea.setBounds(50, 50, 100, 30);
frame.add(textarea);
}
}
public static class genDrag extends MouseMotionAdapter {
JTextArea textarea;
public genDrag(JTextArea argTextarea) {
textarea = argTextarea;
}
public void mouseDragged(MouseEvent E) {
Point p = SwingUtilities.convertPoint(textarea, E.getPoint(), gc_gui.cv_content);
textarea.setBounds((p.x - 40), (p.y - 15), 100, 30);
}
}
public static class genFocus implements FocusListener {
JTextArea textarea;
public genFocus(JTextArea argTextarea) {
textarea = argTextarea;
}
public void focusGained(FocusEvent E) {
System.out.println("Focus gained");
}
public void focusLost(FocusEvent E) {
System.out.println("Focus lost");
}
}
}
我认为 this 正是您所需要的...
提示:您的 class genFocus(更喜欢遵循代码约定:GenFocus)应该实现 FocusListener。
你应该添加一个 event-listener
到控件 JTextArea
然后只有它能够处理任何 event
请求。
JTextField textarea= new JTextField("Value");
textarea.addFocusListener(new genFocus(textarea)); //this peice of code will add an listener to you textarea Object of JTextField.
您的鼠标侦听器将会工作,因为您已将鼠标事件侦听器添加到 JTextArea
。
textarea.addMouseMotionListener(new genDrag(textarea));//code to add MouseMotionListener.
但是没有 FocusEvent
注册到您的 JTextArea
。
谢谢。
要处理焦点事件,您的处理程序需要实现 FocusListener
接口而不是 EventListener
。
请注意,您需要通过 addFocusListener
添加此处理程序。我不认为你这样做了,因为如果你这样做了,你会得到一个编译器错误,指出哪里出了问题。
使用 @Override
注释有助于发现此类错误。把它放在你认为应该覆盖父方法的每个方法之上。如果这样的方法实际上没有覆盖另一个方法,编译器将抛出错误。这样您就可以获悉错误,而不是您的程序默默地失败。