1

我想在我的JTextArea. 假设我已经在编辑中"aaa bbb"并且我想"bbb"用“house”覆盖,我该如何在 Java 中做到这一点?

4

2 回答 2

2

你可以使用replaceRange()

public void replaceRange(String str, int start, int end)

用指定的新文本替换从指示的开始到结束位置的文本。如果模型为空,则不执行任何操作。如果新字符串为空或为空,只需删除即可。

此方法是线程安全的,尽管大多数 Swing 方法不是。有关更多信息,请参阅线程和 Swing。

于 2012-04-08T07:30:14.543 回答
2

您需要查看三个方法setSelectionStart(...)setSelectionEnd(...)replaceSelection(...)

这是一个小示例程序来帮助您的事业:

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

public class TextAreaSelection
{
    private JTextField replaceTextField;
    private JTextField startIndexField;
    private JTextField endIndexField;

    private void createAndDisplayGUI()
    {
        final JFrame frame = new JFrame("JTextArea Selection");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setLocationByPlatform(true);

        JPanel contentPane = new JPanel();
        contentPane.setLayout(new BorderLayout(5, 5));
        contentPane.setOpaque(true);

        final JTextArea tarea = new JTextArea(10, 10);
        tarea.setText("aaa bbb");

        final JButton updateButton = new JButton("UPDATE TEXT");
        updateButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent ae)
            {
                //tarea.setSelectionStart(4);
                //tarea.setSelectionEnd(7);
                //tarea.replaceSelection("house");
                int selection = JOptionPane.showConfirmDialog(null, getPanel());
                if (selection == JOptionPane.OK_OPTION)
                {
                    if (replaceTextField.getDocument().getLength() > 0
                        && startIndexField.getDocument().getLength() > 0
                        && endIndexField.getDocument().getLength() > 0)
                    {   
                        String text = replaceTextField.getText().trim();
                        int start = Integer.parseInt(startIndexField.getText().trim());
                        int end = Integer.parseInt(endIndexField.getText().trim());
                        tarea.replaceRange(text, start, end);
                    }
                }
            }
        });

        contentPane.add(tarea, BorderLayout.CENTER);
        contentPane.add(updateButton, BorderLayout.PAGE_END);

        frame.getContentPane().add(contentPane);
        frame.pack();
        frame.setVisible(true);
    }

    private JPanel getPanel()
    {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(0, 2, 2, 2));

        JLabel replaceLabel = new JLabel("Enter new String : "
                                                , JLabel.CENTER);
        replaceTextField = new JTextField(10);

        JLabel startIndexLabel = new JLabel("Enter Start Index : "
                                                , JLabel.CENTER);
        startIndexField = new JTextField(10);   

        JLabel endIndexLabel = new JLabel("Enter End Index : ");
        endIndexField = new JTextField(10); 

        panel.add(replaceLabel);
        panel.add(replaceTextField);
        panel.add(startIndexLabel);
        panel.add(startIndexField);
        panel.add(endIndexLabel);
        panel.add(endIndexField);

        return panel;
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new TextAreaSelection().createAndDisplayGUI();
            }
        });
    }
}
于 2012-04-08T08:05:27.993 回答