Java JTextArea.select()不选择任何内容

Java JTextArea.select()不选择任何内容,java,select,user-interface,jtextarea,indexof,Java,Select,User Interface,Jtextarea,Indexof,我在使用JTextArea的选择功能时遇到问题。我使用System.out.print()测试变量是否正确填写。一切似乎都很好,但选择功能根本不起作用 public class exercises extends JFrame { JTextField tf_search; String searchstr; JTextArea textarea; String aktStr; int Index; public exercises(String

我在使用
JTextArea
的选择功能时遇到问题。我使用
System.out.print()
测试变量是否正确填写。一切似乎都很好,但选择功能根本不起作用

public class exercises extends JFrame {
    JTextField tf_search;
    String searchstr;
    JTextArea textarea;
    String aktStr;
    int Index;

    public exercises(String title) {
        super(title);
        setLayout(new FlowLayout());
        JComboBox<String> combo = new JComboBox<String>();
        combo.addItem("Here stands the whole Shit");
        String[] systemfonts = new String[200];
        systemfonts = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
        for (String s : systemfonts) {
            combo.addItem(s);
        }

        textarea = new JTextArea(10, 40);
        String examplestr = "Here I only test how often the word olo is in the text. "
                + "\nI'll add olo as often as I like. olo sounds like lol olo";
        textarea.setText(examplestr);
        JPanel p_search = new JPanel();

        tf_search = new JTextField();
        JButton b_search = new JButton("Search");
        //JButton b_weitersuchen = new JButton("weiter suchen"); // I also want to implement a     function to keep on searching for more appereances of the word that is searched
        p_search.add(b_search);
        //p_suchen.add(b_weitersuchen);
        p_search.add(tf_search);
        p_search.setLayout(new GridLayout(3, 1));
        //b_weitersuchen.addActionListener(new MeinActionLauscher());
        b_search.addActionListener(new MyActionListener());
        add(p_search);
        add(textarea);
        add(combo);
    }

    class MyActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String label;
            label = e.getActionCommand();
            if (label.equals("Search")) {
                search();
            }
    //  if(label.equals("weiter suchen"))
            //  weiterSuchen();
        }
    }

    void search() {
        searchstr = tf_search.getText();
        if (searchstr == null) {
            return;
        }

        aktStr = textarea.getText();
        Index = aktStr.indexOf(searchstr);

        if (Index == -1) {
            JOptionPane.showMessageDialog(null, "String not found", "Dialog", JOptionPane.INFORMATION_MESSAGE);
        } else {
            textarea.select(Index, Index + searchstr.length());
        }
    }

    public static void main(String[] args) {
        exercises bla = new exercises("ComboBox Test");
        bla.pack();
        bla.setVisible(true);
        bla.setSize(700, 700);
    }
}
公共课堂练习扩展JFrame{
jtextf_搜索;
字符串搜索str;
JTextArea textarea;
字符串aktStr;
整数指数;
公开练习(弦乐题){
超级(标题);
setLayout(新的FlowLayout());
JComboBox组合=新的JComboBox();
combo.addItem(“整个大便都站在这里”);
字符串[]系统字体=新字符串[200];
systemfonts=GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
用于(字符串s:systemfonts){
组合附加项;
}
textarea=新的JTextArea(10,40);
String examplestr=“在这里,我只测试文本中单词olo的频率。”
+“\n我可以随时添加olo。olo听起来像lol olo”;
textarea.setText(examplestr);
JPanel p_search=新的JPanel();
tf_search=new JTextField();
JButton b_search=新JButton(“search”);
//JButton b_weitersuchen=new JButton(“weiter suchen”);//我还想实现一个函数,以继续搜索所搜索单词的更多外观
p_search.add(b_search);
//p_suchen.add(b_weitersuchen);
p_search.add(tf_search);
p_search.setLayout(新网格布局(3,1));
//b_weitersuchen.addActionListener(新的MeinActionLauscher());
b_search.addActionListener(新的MyActionListener());
添加(p_搜索);
添加(文本区域);
添加(组合);
}
类MyActionListener实现ActionListener{
@凌驾
已执行的公共无效操作(操作事件e){
字符串标签;
label=e.getActionCommand();
if(label.equals(“搜索”)){
搜索();
}
//if(label.equals(“weiter suchen”))
//魏特苏晨();
}
}
无效搜索(){
searchstr=tf_search.getText();
if(searchstr==null){
返回;
}
aktStr=textarea.getText();
索引=aktStr.indexOf(searchstr);
如果(索引==-1){
showMessageDialog(null,“未找到字符串”,“对话框”,JOptionPane.INFORMATION\u消息);
}否则{
选择(Index,Index+searchstr.length());
}
}
公共静态void main(字符串[]args){
练习bla=新练习(“组合框测试”);
bla.pack();
bla.setVisible(真);
bla.setSize(700700);
}
}

我自己解决了。文本区域需要在选择单词之前获得正确的焦点

textarea.requestFocus();
以下是带有新代码行的搜索功能:

void search() {
    searchstr = tf_search.getText();
    if (searchstr == null) {
        return;
    }

    aktStr = textarea.getText();
    Index = aktStr.indexOf(searchstr);

    if (Index == -1) {
        JOptionPane.showMessageDialog(null, "String not found", "Dialog", JOptionPane.INFORMATION_MESSAGE);
    } else {
        textarea.requestFocus();
        textarea.select(Index, Index + searchstr.length());
    }
}

我测试了你的代码,它对我也不起作用。问题是JComponent需要焦点才能显示任何光标选择。所以如果你打电话

textarea.requestFocusInWindow();

就在“选择”方法之前,您应该是优秀的。如果你不想突出显示取决于焦点,我会使用我评论中提到的荧光灯。

文本区域没有焦点。添加
textarea.requestFocus()选择文本之前:

    } else {
        textarea.requestFocus();
        textarea.select(Index, Index + searchstr.length());
    }

如果您试图突出显示文本,此链接可能会有所帮助。选择文本和突出显示文本是否相同?谢谢你的链接。它们使用与我选择相同的基本结构。但是没有帮我找到代码中的错误。