有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing Java,在窗格中选择文本

我用jEditorPane来扮演某个编辑的角色,那个向我要这个的人,也需要一个查找-替换搜索。。。所以我想让他在查找中进行选择,然后根据需要进行替换

因此,我提供以下简单代码:

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    int start = (jEditorPane1.getSelectionStart()>jEditorPane1.getSelectionEnd())?jEditorPane1.getSelectionEnd():jEditorPane1.getSelectionStart();

    int max = (start>jEditorPane1.getSelectionStart())?start:jEditorPane1.getSelectionStart();

    String searchWord = jTextField3.getText();
    int searchIndex = jEditorPane1.getText().indexOf(searchWord, max);
    if(searchIndex != -1){
        jEditorPane1.select(searchIndex, searchIndex+searchWord.length());
    }
    else{
        jEditorPane1.setSelectionStart(-1);
        jEditorPane1.setSelectionEnd(-1);
    }
}       

幸运的是,应用程序返回了良好的索引,但不幸的是,在视图窗口上,我看不到任何选择

我也是java新手,请帮忙


按钮操作由netbeans本身提供


你要的样品

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * test.java
 *
 * Created on Jun 12, 2013, 8:23:19 PM
 */
package wordchecker;

/**
 *
 * @author Hassan
 */
public class test extends javax.swing.JFrame {

    /** Creates new form test */
    public test() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jScrollPane1.setViewportView(jTextPane1);

        jTextField1.setText("jTextField1");

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 366, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 234, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        int startFrom = jTextPane1.getSelectionStart();
        if(jTextPane1.getSelectionStart() == jTextPane1.getSelectionEnd()){
            startFrom = -1;
        }

        String searchWord = jTextField1.getText();
        int searchIndex = jTextPane1.getText().indexOf(searchWord, startFrom);
        if(searchIndex != -1){
            jTextPane1.select(searchIndex, searchIndex+searchWord.length());
        }
        else{
            jTextPane1.setSelectionStart(0);
            jTextPane1.setSelectionEnd(0);
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new test().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration
}

共 (1) 个答案

  1. # 1 楼答案

    仅当文本组件具有焦点时,才会显示文本组件的选择。当您单击按钮时,它会获得焦点,因此您不会看到文本选择。您可以使按钮不可聚焦,也可以将以下内容添加到actionPerformed()方法的底部:

    jTextPane1.requestFocusInWindow();
    

    另外,不要使用getText()方法。这将导致偏移问题

    int searchIndex = jTextPane1.getText().indexOf(searchWord, startFrom);
    

    有关更多信息和解决方案,请参见Text and New Lines。此链接的基本功能是使用:

    int length = textPane.getDocument().getLength();
    String text = textPane.getDocument().getText(0, length);
    

    以上内容将仅返回“\n”作为下线字符串,以便在执行搜索并选择文本时,偏移量将匹配

    编辑:

    我通常使用如下代码:

    int startFrom = jTextPane1.getCaretPosition();