猿问

JTextArea - 双击/三次单击 + 移动鼠标的选择行为

问题:当您双击 JTextArea 中的单词时,它会被标记,但是当您不释放鼠标按钮并尝试标记下一个单词时,它不会标记整个单词,而是标记单个字符。


移动鼠标(双击)时,它应该标记整个单词(而不是单个字符)。这实际上是我尝试过的所有程序的默认行为,例如:记事本、Firefox、Chrome、Word,甚至 Netbeans 等。


与三次单击相同(当按住并移动鼠标时应标记下一行,而不是字符)。


有任何想法吗?我很难用谷歌搜索这个问题,但由于这是一个很常见的事情,我相信一定有一个简单的选择,或者至少有人已经有了解决方案。


示例代码:


public class TestJTextArea

 {

    public static void main(final String[] args)

    {

        final JPanel panel = new JPanel(new BorderLayout());

        panel.setPreferredSize(new Dimension(500, 500));

        panel.add(new JTextArea(), BorderLayout.CENTER);


        final JFrame frame = new JFrame("Test");

        frame.getContentPane().add(panel, BorderLayout.CENTER);

        frame.pack();

        frame.setResizable(false);

        frame.setLocationRelativeTo(null);

        frame.setVisible(true);

    }

}


尚方宝剑之说
浏览 110回答 1
1回答

呼啦一阵风

也许您需要创建一个自定义的Caret,例如:import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.text.*;public class TestJTextArea2 {&nbsp; public Component makeUI() {&nbsp; &nbsp; String text = "The quick brown fox jumps over the lazy dog.";&nbsp; &nbsp; JTextArea textArea1 = new JTextArea("default\n" + text);&nbsp; &nbsp; JTextArea textArea2 = new JTextArea("setCaret\n" + text) {&nbsp; &nbsp; &nbsp; @Override public void updateUI() {&nbsp; &nbsp; &nbsp; &nbsp; setCaret(null);&nbsp; &nbsp; &nbsp; &nbsp; super.updateUI();&nbsp; &nbsp; &nbsp; &nbsp; Caret oldCaret = getCaret();&nbsp; &nbsp; &nbsp; &nbsp; int blinkRate = oldCaret.getBlinkRate();&nbsp; &nbsp; &nbsp; &nbsp; Caret caret = new SelectWordCaret();&nbsp; &nbsp; &nbsp; &nbsp; caret.setBlinkRate(blinkRate);&nbsp; &nbsp; &nbsp; &nbsp; setCaret(caret);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };&nbsp; &nbsp; JPanel p = new JPanel(new GridLayout(2, 1));&nbsp; &nbsp; p.add(new JScrollPane(textArea1));&nbsp; &nbsp; p.add(new JScrollPane(textArea2));&nbsp; &nbsp; return p;&nbsp; }&nbsp; public static void main(String[] args) {&nbsp; &nbsp; EventQueue.invokeLater(() -> {&nbsp; &nbsp; &nbsp; JFrame f = new JFrame();&nbsp; &nbsp; &nbsp; f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; f.getContentPane().add(new TestJTextArea2().makeUI());&nbsp; &nbsp; &nbsp; f.setSize(320, 240);&nbsp; &nbsp; &nbsp; f.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; f.setVisible(true);&nbsp; &nbsp; });&nbsp; }}class SelectWordCaret extends DefaultCaret {&nbsp; private boolean wordSelectingMode = false;&nbsp; private int p0; // = Math.min(getDot(), getMark());&nbsp; private int p1; // = Math.max(getDot(), getMark());&nbsp; @Override public void mousePressed(MouseEvent e) {&nbsp; &nbsp; super.mousePressed(e);&nbsp; &nbsp; int nclicks = e.getClickCount();&nbsp; &nbsp; if (SwingUtilities.isLeftMouseButton(e) && !e.isConsumed() && nclicks == 2) {&nbsp; &nbsp; &nbsp; p0 = Math.min(getDot(), getMark());&nbsp; &nbsp; &nbsp; p1 = Math.max(getDot(), getMark());&nbsp; &nbsp; &nbsp; wordSelectingMode = true;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; wordSelectingMode = false;&nbsp; &nbsp; }&nbsp; }&nbsp; @Override public void mouseDragged(MouseEvent e) {&nbsp; &nbsp; if (wordSelectingMode && !e.isConsumed() && SwingUtilities.isLeftMouseButton(e)) {&nbsp; &nbsp; &nbsp; continuouslySelectWords(e);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; super.mouseDragged(e);&nbsp; &nbsp; }&nbsp; }&nbsp; private void continuouslySelectWords(MouseEvent e) {&nbsp; &nbsp; Position.Bias[] biasRet = new Position.Bias[1];&nbsp; &nbsp; JTextComponent c = getComponent();&nbsp; &nbsp; int pos = c.getUI().viewToModel2D(c, e.getPoint(), biasRet);&nbsp; &nbsp; if(biasRet[0] == null) {&nbsp; &nbsp; &nbsp; biasRet[0] = Position.Bias.Forward;&nbsp; &nbsp; }&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; if (p0 <= pos && pos <= p1) {&nbsp; &nbsp; &nbsp; &nbsp; setDot(p0);&nbsp; &nbsp; &nbsp; &nbsp; moveDot(p1, biasRet[0]);&nbsp; &nbsp; &nbsp; } else if (p1 < pos) {&nbsp; &nbsp; &nbsp; &nbsp; setDot(p0);&nbsp; &nbsp; &nbsp; &nbsp; moveDot(Utilities.getWordEnd(c, pos - 1), biasRet[0]);&nbsp; &nbsp; &nbsp; } else if (p0 > pos) {&nbsp; &nbsp; &nbsp; &nbsp; setDot(p1);&nbsp; &nbsp; &nbsp; &nbsp; moveDot(Utilities.getWordStart(c, pos), biasRet[0]);&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } catch (BadLocationException bl) {&nbsp; &nbsp; &nbsp; UIManager.getLookAndFeel().provideErrorFeedback(c);&nbsp; &nbsp; }&nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答