press a button and open a file in a new window in

2020-04-17 03:39发布

问题:

I have created a JFrame and a put a textfield and the button.in the textfield i have put a name read from a text file.know i want click the button and aopen a knew window in which i want to put the name + other information from the same file.

here is my code: This is my main frame

   package Frontend;

   import Business.ShowDetails;
   import model.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.io.*;
   import java.util.*;
   import javax.swing.*;

 public class Gui {
  //String file=" ";
  private JFrame frame;
  private JPanel panel1 ;
  private JPanel panel2;
  private JPanel panel;
  private JTextField nume1;
  private JTextField nume2;
  private JTextField nume3;
  private JTextField nume4;
  private JTextField nume5;
  private JButton edit1;
  private JButton edit2;
  private JButton edit3;
  private JButton edit4;
  private JButton edit5;
  private final String file = "Persoane.txt";


public Gui(){
   fereastra();
   readFile(file);
}

public void fereastra(){

    frame = new JFrame();
    panel = new JPanel(new FlowLayout());
    panel1 = new JPanel(new GridLayout(5,2,1,7));
    panel2 = new JPanel(new GridLayout(5,2,1,1));

    nume1 = new JTextField(15);
    nume2 = new JTextField(15);
    nume3 = new JTextField(15);
    nume4 = new JTextField(15);
    nume5 = new JTextField(15);

    edit1 = new JButton("Edit");
    edit2 = new JButton("Edit");
    edit3 = new JButton("Edit");
    edit4 = new JButton("Edit");
    edit5 = new JButton("Edit");

    panel1.add(nume1);
    panel2.add(edit1);
    panel1.add(nume2);
    panel2.add(edit2);
    panel1.add(nume3);
    panel2.add(edit3);
    panel1.add(nume4);
    panel2.add(edit4);
    panel1.add(nume5);
    panel2.add(edit5);

    ButtonHandler handler = new ButtonHandler();    
    edit1.addActionListener(handler);                             
    edit2.addActionListener(handler);
    panel.add(panel1);
    panel.add(panel2);
    frame.add(panel);
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    frame.setSize(300, 200);
    frame.setVisible(true);
}
public String readFile(String filename){
    try{
         String line="";
         Persoana p ;
         BufferedWriter bw = null;
         ArrayList<Persoana> listOfPersons;

          //creez o lista de persoane
         listOfPersons = new ArrayList<Persoana>();
         //citesc fisierul Persoane.txt
        FileReader  file1 =new FileReader(filename);
         BufferedReader br1 = new BufferedReader(file1);

        while((line = br1.readLine())!= null){
          //elimin prima linie din fisier

         if(!line.trim().contains("ID")){
         String[] attributes = line.split(";");// separ fiecare linie in tokenuri

            p = new Persoana(); // make a new person
            p.setId(Integer.parseInt(attributes[0]));
            p.setNume(attributes[1]);
            p.setPrenume(attributes[2]);
            p.setDataNasterii(attributes[3]);
            p.setProfesie(attributes[4]);

            listOfPersons.add(p);
         }

      }
         int i = 0;
        while (i < listOfPersons.size()){
            if(i == 0){
                 p = listOfPersons.get(i);
                 nume1.setText(p.getNume() + " " +p.getPrenume());
                 nume1.getText();
            }
            if(i == 1){
                 p = listOfPersons.get(i);
                 nume2.setText(p.getNume() + " " +p.getPrenume());
                 nume2.getText();
            }
             if(i == 2){
                 p = listOfPersons.get(i);
                 nume3.setText(p.getNume() + " " +p.getPrenume());
                 nume3.getText();
            }
             if(i == 3){
                 p = listOfPersons.get(i);
                 nume4.setText(p.getNume() + " " +p.getPrenume());
                 nume4.getText();
            }
             if(i == 4){
                 p = listOfPersons.get(i);
                 nume5.setText(p.getNume() + " " +p.getPrenume());
                 nume5.getText();
            }

             i++;
         }
         br1.close();
    }
    catch(IOException ex){
        System.out.println("Error opening file.");
        System.exit(1);
    }
   return file;
}

  // inner class for button event handling
    private class ButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == edit1) {
                            new ShowDetails();

                    }
                    if (e.getSource() == edit2) {
                            new ShowDetails();
                    }
        }
}
}

this is my second cclass:

  package Business;

  import Frontend.*;
   import javax.swing.*;
  import java.awt.*;
  import java.awt.event.*;




  public class ShowDetails extends JFrame{

  Gui gui;


  public ShowDetails(){

    this.gui = gui;
    fereastra();
}

public void fereastra(){
    JPanel panel = new JPanel(new GridLayout(2, 2));
    JTextArea text = new JTextArea();
    text.setEditable(true);
    text.setText(gui.readFile("Persoane.txt"));
    text.getText();
    panel.add(text);

    getContentPane().add(panel);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //setLocation(50,50);
setSize(300,200);
setResizable(false);
   // setVisible(true);
    show();
}


}

回答1:

  1. Don't to create a new JFrame, if you want the popup window use JDialog with parent of the frame.
  2. Don't use show();, because this is long time depreciated method, remove that and un-commented // setVisible(true);
  3. Why two JButtons make the same things?
  4. Why using FileIO for passing value or argument from one Object to the other?
  5. I'd suggest to use CardLayout instead of to create a popup window.
  6. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); from class ShowDetails will close current JVM instance.
  7. Don't extend JFrame or JDialog or JWindow