2016-07-09 10 views
0

Ich habe JTextField und ich habe es mousemotionListener hinzugefügt. Ich möchte den Brief bekommen, wenn ich mit der Maus auf diesen Brief stoße. Aber ich weiß nicht, wie ich das tun kann und weiß nicht, wie ich den Brief bekommen kann.Buchstaben zeigen, wenn Maus auf Buchstaben

Hier ist der meinen Code

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 
import javax.swing.border.*; 
public class SetText extends JPanel implements MouseMotionListener 
{ 

    JTextField text; 

    public SetText() 
    { 
    text = new JTextField(10); 
    text.addMouseMotionListener(this); 
    add(text); 
    } 

    public void mouseMoved(MouseEvent e) 
    { 
    int x = e.getX(); 
    int y = e.getY(); 

    String str = text.getText(x,y); 
    } 

    public void mouseDragged(MouseEvent e) 
    { 

    } 

Thank you !!!

+0

[zum Beispiel] (http://stackoverflow.com/a/5957405/714968) – mKorbel

Antwort

4

Verwenden Sie die Methode viewToModel(Point p) von JTextComponent, um die Position im Dokument des Texts abzurufen. Zum Beispiel ...

import javax.swing.*; 
import java.awt.event.*; 
import java.awt.*; 

public class SetText extends JPanel implements MouseMotionListener { 

    private static final float POINTS = 40f; 
    JTextField textField; 
    private JLabel displayLabel = new JLabel("   ", SwingConstants.CENTER); 

    public SetText() { 
     textField = new JTextField("Hello world! How's it going?", 20); 
     textField.addMouseMotionListener(this); 

     // make the text bigger so easier to test 
     textField.setFont(textField.getFont().deriveFont(Font.BOLD, POINTS)); 

     JLabel lbl = new JLabel("Text:"); 
     lbl.setFont(lbl.getFont().deriveFont(Font.BOLD, POINTS)); 
     displayLabel.setFont(displayLabel.getFont().deriveFont(Font.BOLD, POINTS)); 

     JPanel centerPanel = new JPanel(); 
     centerPanel.add(lbl); 
     centerPanel.add(displayLabel); 

     setLayout(new BorderLayout()); 
     add(textField, BorderLayout.PAGE_START); 
     add(centerPanel, BorderLayout.CENTER); 
    } 

    public void mouseMoved(MouseEvent e) { 
     int location = textField.viewToModel(e.getPoint()); 

     String text = textField.getText(); 
     if (text.isEmpty()) { 
      return; 
     } 
     // if (location > 0 && location < text.length()) { 
     if (location >= 0 && location < text.length()) { 
      char c = textField.getText().charAt(location); 
      displayLabel.setText(String.valueOf(c)); 
     } else if (location >= text.length()) { 
      displayLabel.setText(text.substring(text.length() - 1)); 
     } 
    } 

    public void mouseDragged(MouseEvent e) { 

    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(() -> { 
      JFrame frame = new JFrame("Set Text"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.add(new SetText()); 
      frame.pack(); 
      frame.setLocationRelativeTo(null); 
      frame.setVisible(true); 
     }); 
    } 
} 
+0

Vielen Dank, alles ist in Ordnung, und es funktioniert. – GhostDede

Verwandte Themen