我正在使用 WindowBuilder,我想问一下如何在文本文件中搜索特定单词,我JTextField
通过单击Search按钮输入该单词?我的程序如下所示:
图片 http://img252.imageshack.us/img252/1410/39931036.png
这是我的JTextField
和JButton
:
private void initialize() {
frmInformacijaApieMuzikos = new JFrame();
frmInformacijaApieMuzikos.setResizable(false);
frmInformacijaApieMuzikos.setIconImage(Toolkit.getDefaultToolkit()
.getImage(KDVizualizuotas.class.getResource(
"/lt/kvk/i3_2/kalasnikovas_stanislovas/resources/Sidebar-Music-Blue-icon.png")));
frmInformacijaApieMuzikos.setTitle("Muzikos stiliai");
frmInformacijaApieMuzikos.setBounds(100, 100, 262, 368);
frmInformacijaApieMuzikos.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
txtStilius = new JTextField();
txtStilius.setBounds(10, 34, 128, 20);
txtStilius.setColumns(10);
JButton btnIekoti = new JButton("Ie\u0161koti");
btnIekoti.setBounds(146, 36, 89, 19);
btnIekoti.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
}
我有代码“在文本文件中搜索”,它可以在控制台中使用,但我需要重新制作它:“输入单词到JTextField
(不在控制台中)并单击搜索按钮(不是输入按钮)来查找它”。我不知道怎么做。
我的代码(您可以跳过最后两个代码):
package lt.kvk.i3_2.kalasnikovas_stanislovas;
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Stilius {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try {
FileReader fr = new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Stiliai.txt");
BufferedReader br = new BufferedReader(fr);
String stiliuSarasas;
while((stiliuSarasas = br.readLine()) != null) {
System.out.println(stiliuSarasas);
}
fr.close();
/** String help = "";
System.out.println("Iveskite stiliaus pavadinima apie kuri ieskote informacijos (daugiau funkciju - Pagalba)");
if ("Pagalba".equalsIgnoreCase(help)) {
help = input.nextLine();
Pagalba pagalba = new Pagalba(help);
}**/
String kitasStilius = "Taip";
while (!"Ne".equalsIgnoreCase(kitasStilius)) {
if ("Taip".equalsIgnoreCase(kitasStilius)) {
System.out.println("Iveskite stiliaus pavadinima apie kuri ieskote informacijos");
String stilius = input.nextLine();
Veiksmai stiliausPaieska = new Veiksmai(stilius);
}
else if ("Ne".equalsIgnoreCase(kitasStilius)) {
}
else{
System.out.println("Neteisingai pasirinkta.");
}
System.out.println("Ar norite ieskoti kito stiliaus?");
System.out.println("Iveskite: Taip or Ne");
kitasStilius = input.nextLine();
}
}
catch (IOException e) {
System.out.println("Error:" + e.toString());
}
}
}
和其他类
package lt.kvk.i3_2.kalasnikovas_stanislovas;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class Veiksmai {
public Veiksmai(String stilius) throws IOException {
BufferedReader bf = new BufferedReader(new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Miestai.txt"));
int counter = 0;
String line;
System.out.println("Ieskoma informacija apie " + stilius);
ArrayList<String> miestuSarasas = new ArrayList<String>();
String miestas = null;
while (( line = bf.readLine()) != null){
if (line.trim().length() == 0) miestas = null;
else if (miestas == null) miestas = line;
int indexfound = line.indexOf(stilius);
if (indexfound > -1) {
counter++;
miestuSarasas.add(miestas);
}
}
if (counter > 0) {
System.out.println(stilius + " turi " + counter + " remejus: " + miestuSarasas);
try {
File file = new File("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Atsakymas.txt");
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(stilius + " turi " + counter + " remejus: " + miestuSarasas+"\n");
bw.close();
}
catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("Klaida, nerastas muzikos stilius");
}
bf.close();
}
public void stabdyti(){
}
public void sarasas() throws IOException{
FileReader fr = new FileReader("src/lt/kvk/i3_2/kalasnikovas_stanislovas/Stiliai.txt");
BufferedReader br = new BufferedReader(fr);
String stiliuSarasas;
while((stiliuSarasas = br.readLine()) != null) {
System.out.println(stiliuSarasas);
}
fr.close();
}
}
请问你能帮帮我吗?我还是Java的新手。我尝试了很多,但一切都行不通。