Show multiple details in console, but not in textarea. How to show specific lines in textarea

huangapple go评论94阅读模式
英文:

Show multiple details in console, but not in textarea. How to show specific lines in textarea

问题

class search extends JFrame {

    private Container container;
    private Font font, font2;
    private JLabel label;
    private JTextField textField;
    private JButton search;
    private JTextArea textarea;

    search() {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setBounds(100, 40, 600, 380);
        this.setTitle("SEARCH");

        container = this.getContentPane();
        container.setLayout(null);

        font = new Font("Courier New", Font.BOLD, 14);
        font2 = new Font("Courier New", Font.BOLD, 18);

        label = new JLabel("SEARCH : ");
        label.setBounds(40, 20, 150, 50);
        label.setFont(font);
        container.add(label);

        textField = new JTextField();
        textField.setBounds(140, 25, 200, 40);
        textField.setFont(font2);
        container.add(textField);

        search = new JButton("SEARCH");
        search.setBounds(370, 25, 110, 37);
        search.setFont(font);
        container.add(search);

        textarea = new JTextArea();
        textarea.setBounds(40, 85, 500, 200);
        textarea.setFont(font2);
        container.add(textarea);

        search.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                String x = textField.getText();
                try {
                    File file = new File("allinfo.txt");
                    BufferedReader reader = new BufferedReader(new FileReader(file));
                    String s = reader.readLine();
                    StringBuilder detailsToShow = new StringBuilder(); // Store details to display in textarea
                    while (s != null) {
                        String string[] = s.split(" ");
                        String a = string[0];
                        String b = string[1];
                        String c = string[2];
                        String d = string[3];

                        String f = a + " " + b + " " + c + " " + d;

                        if (x.equals(a)) {
                            detailsToShow.append(f).append("\n"); // Append the details to display
                        }
                        s = reader.readLine();
                    }
                    reader.close();
                    textarea.setText(detailsToShow.toString()); // Set all details in the textarea
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

    }

    public static void main(String[] args) {

        search frame = new search();
        frame.setVisible(true);

    }

}
英文:

class search extends JFrame{

private Container container;
private Font font,font2;
private JLabel label;
private JTextField textField;
private JButton search;
private JTextArea textarea;
search(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBounds(100,40,600,380);
this.setTitle("SEARCH");
container = this.getContentPane();
container.setLayout(null);
font = new Font("Courier New",Font.BOLD,14);
font2 = new Font("Courier New",Font.BOLD,18);
label = new JLabel("SEARCH : ");
label.setBounds(40,20,150,50);
label.setFont(font);
container.add(label);
textField = new JTextField();
textField.setBounds(140,25,200,40);
textField.setFont(font2);
container.add(textField);
search = new JButton("SEARCH");
search.setBounds(370,25,110,37);
search.setFont(font);
container.add(search);
textarea = new JTextArea();
textarea.setBounds(40,85,500,200);
textarea.setFont(font2);
container.add(textarea);
search.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
String x = textField.getText();
try{
File file = new File("allinfo.txt");
BufferedReader reader = new BufferedReader(new FileReader(file));
String s = reader.readLine();
while(s!=null){
String string[] = s.split(" ");
String a = string[0];
String b = string[1];
String c = string[2];
String d = string[3];
String f = a+" "+b+" "+c+" "+d;
if (x.equals(a)){
System.out.println(f);
textarea.setText(f);
}
s = reader.readLine();
}
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
});
}
public static void main(String[] args) {
search frame = new search();
frame.setVisible(true);
}
}

It is a search option where I can search for a person's name. If there are two people who have the same name, both the option should show in the text area, but the text area show only one person details. But the console is printing all personal details. I need help with how I can show all the details in textarea.

答案1

得分: 0

不是用

textarea.setText(f);

而是用

textarea.append(f + "\n");
英文:

Instead of

textarea.setText(f);

use

textarea.append(f + "\n");

huangapple
  • 本文由 发表于 2020年10月7日 20:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64244300.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定