2

我创建了一个 JFrame 并放置了一个文本字段和按钮。在文本字段中我放置了一个从文本文件中读取的名称。知道我想单击按钮并打开一个我想在其中放置名称 + 其他信息的已知窗口来自同一个文件。

这是我的代码:这是我的主框架

   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();
                    }
        }
}
}

这是我的第二个 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();
}


}
4

1 回答 1

3
  1. JFrame如果您希望弹出窗口JDialog与框架的父级一起使用,请不要创建新的。
  2. 不要使用show();,因为这是长期贬值的方法,请删除它并取消注释 // setVisible(true);
  3. 为什么两个JButtons做同样的事情?
  4. 为什么使用FileIO将值或参数从一个Object传递到另一个?
  5. 我建议使用CardLayout而不是创建一个弹出窗口。
  6. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);fromclass ShowDetails将关闭当前的 JVM 实例。
  7. 不要扩展JFrameJDialogJWindow
于 2012-05-14T17:59:07.303 回答