现在还有人用asp做网站/色盲测试
我在使用JTextArea的选择功能时遇到麻烦.我是否使用System.out.print()测试了变量是否正确填充.一切似乎都不错,但是select函数根本无法工作.
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 combo = new JComboBox();
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. "
+ "
I'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);
}
}