【问题标题】:Inserting String in JTextArea at specific location在特定位置的 JTextArea 中插入字符串
【发布时间】:2012-09-08 14:21:06
【问题描述】:

Java 中有没有可以在指定的 JTextArea 列号处插入字符串的函数。

例如,

String str = "This is a sample text"

String => column Number

This => at 0

is => at 10

a => at 14

sample => at 20

text => at 25

【问题讨论】:

  • 嗯...我不明白。你的意思是某个字母?
  • @PicklishDoorknob jtextarea 列号
  • @Reimeus 你的答案是正确的!!!,有/没有替代品
  • @FirmView 肯定还有其他选择,发SSCCE
  • @FirmView :请看看这个example。这正是您所需要的,在我看来 :-) 您可以实现将 String 放在该位置而不是 Showing Up a Message 的逻辑

标签: java string swing document jtextarea


【解决方案1】:

请看一下这个例子,它使用了 JTextComponent 的 viewToModel() 方法。

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

public class TextAreaExample extends JFrame
{
    private JTextArea tarea =  new JTextArea(10, 10);
    private JTextField tfield = new JTextField(10);
    private Object[] possibleValues = { "First", "Second", "Third" };

    private void createAndDisplayGUI()
    {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tarea.setText("Hello there\n");
        tarea.append("Hello student://");
        JScrollPane scroll = new JScrollPane(tarea);

        tfield.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                tarea.append(tfield.getText() + "\n");
            }
        });

        tarea.addMouseListener(new MouseAdapter()
        {
            public void mouseClicked(MouseEvent me)
            {
                int x = me.getX();
                int y = me.getY();
                System.out.println("X : " + x);
                System.out.println("Y : " + y);
                int startOffset = tarea.viewToModel(new Point(x, y));
                System.out.println("Start Offset : " + startOffset);
                String text = tarea.getText();
                String firstPart = text.substring(0, startOffset);
                String secondPart = text.substring(startOffset, text.length());

                Object selectedValue = JOptionPane.showInputDialog(null,
                                                  "Choose one", "Input",
                                                  JOptionPane.INFORMATION_MESSAGE, null,
                                                  possibleValues, possibleValues[0]);

                if (selectedValue != null)  
                {
                    String newText = firstPart + " " 
                                               + (String) selectedValue
                                               + " "
                                               + secondPart;
                    tarea.setText(newText);                        
                }
            }
        });

        getContentPane().add(scroll, BorderLayout.CENTER);
        getContentPane().add(tfield, BorderLayout.PAGE_END);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextAreaExample().createAndDisplayGUI();
            }
        });
    }
}

【讨论】:

  • @mKorbel : 谢谢并保持微笑:-)
猜你喜欢
  • 2012-12-01
  • 2021-09-25
  • 2020-01-20
  • 2011-08-22
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多