如何使GUI界面工作?

时间:2014-07-03 15:54:19

标签: java user-interface

我正在尝试制作一个GUI界面程序,用于插入人名数据,如名字(Prenume),姓氏(Nume)和生日(Zi de nastere)。它有3个按钮:保存(Salvare)将数据保存到文本文件中,删除(Stergere)删除数据(分别为每个人放入,而不是文本文件中的所有数据)和最后一个按钮搜索(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();



            }
        });
    }
}

编辑:我已设法执行删除(Stergere)按钮,但不是仅删除我想要的数据,而是删除所有内容。

1 个答案:

答案 0 :(得分:0)

像这样的问题的通常解决方案是创建一个实现UI操作(编辑,删除,搜索)的辅助类。用户界面然后与这个班级进行对话。

在内部,该类使用文本文件来实际保存数据。但UI永远不会知道。它只是使用一些参数调用add()

下一步是你应该定义一个包含你想要处理的数据的数据类(即关于一个人的所有内容:姓名,日期等)。

最后,您需要编写在文本文件和数据类之间进行转换的转换方法。