【发布时间】:2014-07-04 04:23:17
【问题描述】:
我正在尝试制作一个 GUI 界面程序,它可以插入名字(Prenume)、姓氏(Nume)和生日(Zi de nastere)等人的数据。它有 3 个按钮:Save(Salvare) 将数据保存到文本文件中,Delete(Stergere) 删除数据(分别为每个放入的人,而不是文本文件中的所有数据)和最后一个按钮Search(Cautare) 会打开另一个 GUI 窗口,该窗口看起来与旧窗口相同,只是它只有一个按钮。当您按下该按钮时,程序将在文本文件中搜索您放入文本框中的字符串(如果您输入名称文本框,它将搜索 FirstName:Michael 并显示所有名字为 michael 的人由程序制作的列表也是 GUI。如果你输入生日,他将搜索所有具有该生日的人)同样在程序开始时,我希望程序显示一个列表,其中包含生日在下 10 位的人天。
现在我遇到的最大问题是搜索框......我似乎无法让它工作,我知道我需要搜索命令在开始时执行列表,并且还需要删除按钮......因为在删除按钮 我需要程序来搜索数据输入并将其从文本文件中删除。我知道这可能有点令人困惑,因为文本输出是我的主要语言,不是英语,但我为主要单词做了翻译,所以你可以理解。
这是我目前所做的:
JTextField camp,camp2,camp3,camp4,camp5;
String line;
Interfata()
{
setTitle("Zile De Nastere");
setSize(340, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel container = (JPanel) getContentPane();
container.setLayout(null);
JLabel intro = new JLabel();
intro.setText("Nume:");
intro.setBounds(67, 15, 100, 22);
container.add(intro);
camp = new JTextField("Numele");
camp.setBounds(110,15,220,22);
container.add(camp);
JLabel intro2 = new JLabel();
intro2.setText("Prenume:");
intro2.setBounds(48, 45, 100, 22);
container.add(intro2);
camp2 = new JTextField("Prenumele");
camp2.setBounds(110,45,220,22);
container.add(camp2);
JLabel intro3 = new JLabel();
intro3.setText("Data de nastere:");
intro3.setBounds(10, 75, 100, 22);
container.add(intro3);
camp3 = new JTextField("zi");
camp3.setHorizontalAlignment(JTextField.CENTER);
camp3.setBounds(110,75,20,22);
container.add(camp3);
JLabel cp2 = new JLabel();
cp2.setText("/");
cp2.setBounds(132, 75, 15, 22);
container.add(cp2);
camp4 = new JTextField("luna");
camp4.setBounds(137,75,30,22);
camp4.setHorizontalAlignment(JTextField.CENTER);
container.add(camp4);
JLabel cp3 = new JLabel();
cp3.setText("/");
cp3.setBounds(169, 75, 15, 22);
container.add(cp3);
camp5 = new JTextField("an");
camp5.setBounds(174,75,35,22);
camp5.setHorizontalAlignment(JTextField.CENTER);
container.add(camp5);
JButton go = new JButton("Salvare");
go.setBounds(10,105,100,22);
container.add(go);
go.addActionListener(this);
JButton go2 = new JButton("Stergere");
go2.setBounds(120,105,100,22);
container.add(go2);
go2.addActionListener(this);
JButton go3 = new JButton("Cautare");
go3.setBounds(230,105,100,22);
container.add(go3);
go3.addActionListener(this);
JLabel cp = new JLabel();
cp.setText("Copyright 2014 Tache Radu");
cp.setBounds(10, 140, 200, 22);
container.add(cp);
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="Salvare")
{
try {
File statText = new File("E:/Eclipse/Workspace/Proiect Tache Radu/Baza de Date.txt");
FileOutputStream is = new FileOutputStream(statText,true);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
String a=camp.getText();
String b=camp2.getText();
String c=camp3.getText();
String d=camp4.getText();
String f=camp5.getText();
int ok=1;
if(f.equalsIgnoreCase(""))
f="Nespecificat";
if(a.equalsIgnoreCase(""))
{
setTitle("Eroare");
JOptionPane.showMessageDialog(this,"Introduceti Numele", "Eroare", JOptionPane.ERROR_MESSAGE);
ok=0;
}
if(b.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(this,"Introduceti Prenumele", "Eroare", JOptionPane.ERROR_MESSAGE);
ok=0;
}
if(c.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(this,"Introduceti Ziua", "Eroare", JOptionPane.ERROR_MESSAGE);
ok=0;
}
if(d.equalsIgnoreCase(""))
{
JOptionPane.showMessageDialog(this,"Introduceti Luna", "Eroare", JOptionPane.ERROR_MESSAGE);
ok=0;
}
if(ok==0)
System.exit(0);
else
{
w.write("Nume: "+camp.getText()+"\r\nPrenume: "+camp2.getText()+"\r\nData Nasterii: "+camp3.getText()+"/"+camp4.getText()+"/"+f+"\r\n----------------------------------------\r\n\r\n");
}
w.close();
} catch (IOException g) {
System.err.println("Problem writing to the file Baza de Date.txt");
}
}
if(e.getActionCommand()=="Stergere")
{
String s1=camp.getText();
String s2=camp2.getText();
String s3=camp3.getText();
String s4=camp4.getText();
String s5=camp5.getText();
try {
File inFile = new File("Baza de Date.txt");
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing file");
return;
}
//Construct the new file that will later be renamed to the original filename.
File tempFile = new File(inFile.getAbsolutePath() + ".tmp");
BufferedReader br = new BufferedReader(new FileReader("temp.tmp"));
PrintWriter pw = new PrintWriter(new FileWriter(tempFile));
String line = null;
//Read from the original file and write to the new
//unless content matches data to be removed.
while ((line = br.readLine()) != null) {
if (!line.trim().equals(s1)) {
pw.println(line);
pw.flush();
}
if (!line.trim().equals(s2)) {
pw.println(line);
pw.flush();
}
if (!line.trim().equals(s3)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
//Delete the original file
if (!inFile.delete()) {
System.out.println("Could not delete file");
return;
}
//Rename the new file to the filename the original file had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex) {
ex.printStackTrace();
}
}
if(e.getActionCommand()=="Cautare")
{
JFrame Register = new JFrame("Cautare");
Register.setTitle("Cautare");
Register.setSize(340, 200);
Register.setLocationRelativeTo(null);
Register.setDefaultCloseOperation(EXIT_ON_CLOSE);
JPanel container = (JPanel) getContentPane();
container.setLayout(null);
JLabel intro7 = new JLabel();
intro7.setText("Nume:");
intro7.setBounds(67, 15, 100, 22);
container.add(intro7);
camp.setBounds(110,15,220,22);
container.add(camp);
JLabel intro2 = new JLabel();
intro2.setText("Prenume:");
intro2.setBounds(48, 45, 100, 22);
container.add(intro2);
camp2.setBounds(110,45,220,22);
container.add(camp2);
JLabel intro3 = new JLabel();
intro3.setText("Data de nastere:");
intro3.setBounds(10, 75, 100, 22);
container.add(intro3);
camp3.setHorizontalAlignment(JTextField.CENTER);
camp3.setBounds(110,75,20,22);
container.add(camp3);
JLabel cp2 = new JLabel();
cp2.setText("/");
cp2.setBounds(132, 75, 15, 22);
container.add(cp2);
camp4.setBounds(137,75,30,22);
camp4.setHorizontalAlignment(JTextField.CENTER);
container.add(camp4);
JLabel cp3 = new JLabel();
cp3.setText("/");
cp3.setBounds(169, 75, 15, 22);
container.add(cp3);
camp5.setBounds(174,75,35,22);
camp5.setHorizontalAlignment(JTextField.CENTER);
container.add(camp5);
JButton go4 = new JButton("Cautare");
go4.setBounds(120,105,100,22);
container.add(go4);
go4.addActionListener(this);
if(e.getActionCommand()=="Cautare")
{
String a=camp.getText();
String b=camp2.getText();
String c=camp3.getText();
String d=camp4.getText();
String f=camp5.getText();
double count = 0,countBuffer=0,countLine=0;
String lineNumber = "";
String filePath = "E://Eclipse/Workspace/Proiect Tache Radu/Baza de Date.txt";
BufferedReader br;
String inputSearch = a;
String inputSearch2 = b;
String inputSearch3 = c;
String inputSearch4 = d;
String inputSearch5 = f;
String line = "";
try {
br = new BufferedReader(new FileReader(filePath));
try {
while((line = br.readLine()) != null)
{
countLine++;
//System.out.println(line);
String[] words = line.split(" ");
for (String word : words) {
if (word.equals(inputSearch) || word.equals(inputSearch2) || word.equals(inputSearch3) || word.equals(inputSearch4) || word.equals(inputSearch5)) {
count++;
countBuffer++;
}
}
if(countBuffer > 0)
{
countBuffer = 0;
lineNumber += countLine + ",";
}
}
br.close();
} catch (IOException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
} catch (FileNotFoundException h) {
// TODO Auto-generated catch block
h.printStackTrace();
}
System.out.println("Times found at--"+count);
System.out.println("Word found at--"+lineNumber);
}
Register.setVisible(true);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Interfata interfata = new Interfata();
interfata.setVisible(true);
Interfata Stergere = new Interfata();
}
});
}
}
编辑:我已经成功地执行了 Delete(Stergere) 按钮,但不是只删除我想要的数据,而是删除了所有内容。
【问题讨论】:
-
如果这是 Visual Basic,它可能会追踪 IP 地址。
标签: java user-interface