2013-09-22 65 views
1

其实,我已经在here中提出过这个问题。但是,我犯了错误。我还没有得到解决方案。在JTextArea中获取插入位置的XY位置

首先,在问题之前,我可以

Rectangle rectangle = textArea.modelToView(textArea.getCaretPostion()); 

得到矩形我也得到X和Y位置。

我正在创建一个可以添加新文本区域的编辑器,每按一次Enter键。上面代码的XY位置总是会在每个文本区域中返回相同的返回值。看我的代码。

import java.awt.Container; 
import java.awt.Font; 
import java.awt.Rectangle; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyEvent; 
import java.util.LinkedList; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.AbstractAction; 
import javax.swing.Action; 
import javax.swing.Box; 
import javax.swing.JFrame; 
import javax.swing.JTextArea; 
import javax.swing.KeyStroke; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.JTextComponent; 

public class forquestion extends JFrame { 

    Container textAreaBox; 
    LinkedList<JTextComponent> textArea; 
    int nameTA; 

    public forquestion() { 
      int nameTA = 0; 

      textArea = new LinkedList<>(); 

      textAreaBox = Box.createVerticalBox(); 
      textAreaBox.add(Box.createVerticalGlue()); 
      addLine(); 
      this.add(textAreaBox); 
      this.setVisible(true); 
    } 

    public static void main(String[] args) { 
      forquestion app = new forquestion(); 
      app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 

    public void addLine() { 

      JTextComponent temp_ta = createTextComponent(); 
      textArea.add(temp_ta); 
      textAreaBox.add(textArea.getLast()); 
      textAreaBox.add(Box.createVerticalGlue()); 
    } 

    protected JTextComponent createTextComponent() { 
      JTextArea ta = new JTextArea("test"); 

      /*if (count%2==0) 
          ta.setForeground(Color.red); 
      else 
          ta.setForeground(Color.GREEN);*/ 

      ta.setFont(new Font("Courier New",Font.PLAIN,16)); 
      ta.setLineWrap(true);                                                             
      ta.setWrapStyleWord(true); 
      ta.setName(Integer.toString(nameTA)); 
      nameTA+=1; 

      basicKey("ENTER", enter, ta); 

      ta.addMouseListener(new java.awt.event.MouseAdapter() { 
        public void mousePressed(java.awt.event.MouseEvent ev) { 
          try { 
            taMousePressed(ev); 
          } catch (BadLocationException ex) { 
            Logger.getLogger(forquestion.class.getName()).log(Level.SEVERE, null, ex); 
          } 
        } 
      }); 

      return ta; 
    } 

    public void basicKey(String s, Action a, JTextArea ta) { 

      ta.getInputMap().put(KeyStroke.getKeyStroke(s), s); 
      ta.getActionMap().put(s, a); 
    } 

    Action enter = new AbstractAction() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 

        addLine(); 
      } 
    }; 

    private void taMousePressed(java.awt.event.MouseEvent ev) throws BadLocationException { 
      int now_focus = Integer.parseInt(ev.getComponent().getName()); 
      int _caret; 
      _caret = textArea.get(now_focus).getCaretPosition(); 
      Rectangle rectangle = textArea.get(now_focus).modelToView(_caret); 
      double x = rectangle.getX(); 
      //int xc = textArea.get(now_focus).getLocation().x; 
      double y = rectangle.getY(); 
      //int yc = textArea.get(now_focus).getLocation().y; 
      //double h = rectangle.getHeight(); 
      //double w = rectangle.getWidth(); 
      System.out.println(x); 
      System.out.println(y); 
      //System.out.println(xc); 
      //System.out.println(yc); 
      //System.out.println(h); 
      //System.out.println(w); 
      System.out.println(""); 
    } 

} 

我的代码每次按文本区域时都会打印XY位置。但是,每个文本区域的显示总是相同的。 (尝试制作很多文本区域并给出一些文本)顺便说一句,它只是简单的代码。你需要改变窗口的框架大小来更新新的文本区域,然后按下输入key ..hahaha。

所以,我的问题是:如何在任何文本区域中获得插入符(文本光标)的XY位置。我想在那里显示JPopmenu。 :)

我希望这个问题明确给你。 Thx之前。

回答

3

回报的Rectangle是相对于文本区域,它的位置是0x0,位于组件的左上角。

如果你使用类似...

popup.show(textArea.get(now_focus), rectangle.x, rectangle.y + rectangle.height); 

popupJPopupMenu,它会进行必要的翻译到屏幕本身。

现在。话说回来。就个人而言,我宁愿使用Swing提供的弹出API支持。这将意味着需要创建一个从JTextArea扩展来实现它的自定义组件...

public class MyPopupTextArea extends JTextArea { 
    /*...*/ 
    public Point getPopupLocation(MouseEvent evt) { 
     Rectangle rectangle = textArea.get(now_focus).modelToView(_caret); 

     Point p = rectangle.getLoction(); 
     p.y += rectangle.height; 

     return p; 
    } 
} 

然后,根据您的需求,您可以使用setComponentPopup提供JPopupMenu或共享的情况下,如果需要为每个自定义编辑器创建一个自定义JPopupMenu,并使用setComponentPopup,因为您认为合适......没有搞乱鼠标监听器;)

+0

我明白了。 Thx for solution.Btw,你能给我你的问题的完整代码吗?至少,您的代码的构造函数将Jpopmenu添加到面板或框架。我仍然在学习JPopMenu。每次我将它添加到容器中时,它都会覆盖其他组件。 – TblsX

+0

您不要将JPopupMenu添加到组件(通常)。您应该使用setComponentPopup将弹出框与组件关联起来,或者,如果您有特殊需求,请使用JPopupMenu#show来显示弹出框。更像是一个特殊的窗口,然后是一个组件。你可以看看[三明治弹出菜单](http://docs.oracle.com/javase/tutorial/uiswing/components/menu.html#popup)了解更多详情... – MadProgrammer

+0

这对我来说很有意义。无论如何:D – TblsX