检测悬停在JTextArea上

时间:2014-11-03 04:36:14

标签: java swing jtextarea

我想在用户将鼠标悬停在JTextArea上时为其添加边框。这一切都很好,我是这样做的:

public void mouseMoved(MouseEvent e) {
    Border border = BorderFactory.createLineBorder(Color.black);
    JTextArea component = (JTextArea) e.getComponent();
    component.setBorder(border);
}

我的问题是:当用户没有悬停在JTextArea上时,我想删除边框。无论如何我能做到吗?我是否必须在JPanel中执行此操作,还是可以在侦听器类中执行此操作?你可以在这里看到,当我在JPanel上移动鼠标(但不在JTextArea上)时,边框会停留:

enter image description here

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.event.MouseInputAdapter;

public class Editor {

    public static void main(String[] args) {
        JFrame frame = new Window();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(30, 30, 1000, 700);
        frame.getContentPane().setBackground(Color.white);
        frame.setVisible(true);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

class Window extends JFrame {
    MyPanel myPanel = new MyPanel();

    private static final long serialVersionUID = 1L;

    public Window() {
        addMenus();
    }

    public void addMenus() {

        getContentPane().add(myPanel);

        setSize(300, 200);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
}

class MyPanel extends JPanel {
    private static final long serialVersionUID = 1L;
    DragListener drag;

    public MyPanel() {
        drag = new DragListener();
        addBox();
    }

    public void addBox() {
        setLayout(new DragLayout());
        drag = new DragListener();
        JTextArea textArea2 = new JTextArea("Text Box Added");
        textArea2.setLineWrap(true);
        textArea2.setWrapStyleWord(true);
        add(textArea2);
        textArea2.addMouseListener(drag);
        textArea2.addMouseMotionListener(drag);
        repaint();
        revalidate();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    }

    public class DragListener extends MouseInputAdapter {
        Point location;
        MouseEvent pressed;

        public void mousePressed(MouseEvent me) {
            pressed = me;
        }

        public void mouseDragged(MouseEvent me) {
            Component component = me.getComponent();
            location = component.getLocation(location);
            int x = location.x - pressed.getX() + me.getX();
            int y = location.y - pressed.getY() + me.getY();
            component.setLocation(x, y);
        }

        public void mouseMoved(MouseEvent e) {
            Border border = BorderFactory.createLineBorder(Color.black);
            JTextArea component = (JTextArea) e.getComponent();
            component.setBorder(border);
        }
    }

}

2 个答案:

答案 0 :(得分:1)

使用MouseEntered的{​​{1}}和MouseExited事件,这些事件会在您输入或存在组件时通知您

有关详细信息,请参阅How to Write a Mouse Listener

答案 1 :(得分:1)

实际上,在鼠标移动时,您可以获得文本位置(使用文本区域的viewToModel)并检测文本位置的视图边界。调用modelToView获取偏移量,并且您有插入矩形边界。您可以将鼠标y与矩形的yy+height进行比较,以检测鼠标是否垂直放置在文本上。

然后使用javax.swing.text.Utilities获取行的开始和结束位置。获取开始和结束的插入符矩形,并使用它们的x坐标来检查水平边界中的鼠标。

您还可以使用http://java-sl.com/JEditorPaneStructureTool.html示例的源代码来计算文本视图边界。