0

我有一个可能很简单的问题(尽管我已经完成了搜索)。我有一个数组列表,只需单击 JButton 即可更新。我有 toString() 这个数组列表,这样我就可以将它添加到 textarea.setText(xx.toString() 以便我可以在滚动窗格上显示。问题是每次添加一个元素时,textarea 都会显示数组,然后新的更新数组,这意味着文本每次都会翻倍。还有其他方法吗?谢谢,知道这是个愚蠢的问题

public  nameFrame()
{
    super("Name Generator");

    JPanel background = new JPanel();
    background.setLayout(new BoxLayout( background, BoxLayout.X_AXIS ) );
    name = new ArrayList<String>();

    getContentPane().add(background); 
    Box verticalBack = Box.createVerticalBox();
    Box horizbox1 = Box.createHorizontalBox();
    Box horizboxmid = Box.createHorizontalBox();
    Box horizbox2 = Box.createHorizontalBox();
    Box verticalbox1 =Box.createVerticalBox();
    Box verticalbox2 = Box.createVerticalBox();
    getContentPane(); 

    background.add(verticalBack);
    verticalBack.add(horizbox1);
    horizbox1.add(verticalbox1);
    horizbox1.add(verticalbox2);
    verticalBack.add(horizboxmid);
    verticalBack.add(horizbox2);

    JButton maleName = new JButton("MALE");
    JButton femaleName = new JButton("FEMALE");
    JButton clearLog = new JButton("CLEAR LIST");
    JButton closeApp = new JButton("CLOSE");

    verticalbox1.add(maleName);
    verticalbox1.add(femaleName);
    verticalbox2.add(clearLog);
    verticalbox2.add(closeApp);

    final JScrollPane listName = new JScrollPane();
    listName.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    horizbox2.add(listName);
    final JTextArea nameDisplayList = new JTextArea();
    nameDisplayList.setSize(300, 300);
    listName.add(nameDisplayList);
    listName.setViewportView(nameDisplayList);
    nameDisplayList.setEditable(false); 
    nameDisplayList.setVisible(true);

    stringLog = Arrays.toString(name.toArray()).replaceAll("\\[|\\]", "").replaceAll(", ","\n");
    final StringBuilder sb = new StringBuilder();
    for(String s : name)
    {
        sb.append(s);
        sb.append("\n");
    }

    nameDisplayList.setText(sb.toString());
    ///listName.repaint();
    ///listName.revalidate();


    nameDisplayList.setText(sb.toString());

    maleName.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {
            {

                try {
                    Scan = new Scanner(new File("MaleNames.txt"));
                    ArrayList<String> MaleFirstNames = new ArrayList<String>();
                    while(Scan.hasNext())
                    {
                        MaleFirstNames.add(Scan.next());
                    }
                    Scan.close();

                    int Mindex = new Random().nextInt(MaleFirstNames.size());
                    firstName = MaleFirstNames.get(Mindex); 

                    Scan = new Scanner(new File("Surnames.txt"));
                    ArrayList<String> SurnameList = new ArrayList<String>();
                    while(Scan.hasNext())
                    {
                        SurnameList.add(Scan.next());
                    }
                    Scan.close();

                    int Sindex = new Random().nextInt(SurnameList.size());
                    surName = SurnameList.get(Sindex);

                    fullName = String.format("%s\t%s%n", firstName, surName);

                    name.add(fullName);

                    //indexPoint++;

                    //System.out.println(name);

                    nameDisplayList.revalidate();
                    nameDisplayList.repaint();
                    for(String s : name)
                    {
                        sb.append(s);
                        sb.append("\n");
                    }
                    nameDisplayList.setText(sb.toString());

                    } 
                catch (FileNotFoundException e) 
                    {
                    e.printStackTrace();
                    }   
            }
        }           
    });

    femaleName.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {
            {

                try {
                    Scan = new Scanner(new File("FemaleNames.txt"));
                    ArrayList<String> FemaleFirstNames = new ArrayList<String>();
                    while(Scan.hasNext())
                    {
                        FemaleFirstNames.add(Scan.next());
                    }
                    Scan.close();

                    int Findex = new Random().nextInt(FemaleFirstNames.size());
                    firstName = FemaleFirstNames.get(Findex);

                    Scan = new Scanner(new File("Surnames.txt"));
                    ArrayList<String> SurnameList = new ArrayList<String>();
                    while(Scan.hasNext())
                    {
                        SurnameList.add(Scan.next());
                    }
                    Scan.close();

                    int Sindex = new Random().nextInt(SurnameList.size());
                    surName = SurnameList.get(Sindex);

                    fullName = String.format("%s\t%s%n", firstName, surName);

                    name.add(indexPoint, fullName);



                    } 
                catch (FileNotFoundException e) 
                    {
                    e.printStackTrace();
                    }
            }
        }


    });

    clearLog.addActionListener(new ActionListener()
    {

        public void actionPerformed(ActionEvent event)
        {                   
            {
                    do
                    {

                        name.add(name.size(), " ");

                        indexPoint--;

                    }while(indexPoint != 0);
            }
        }


    });     



}

}

4

1 回答 1

2

在您的“actionPerformed”中,您需要清理 StringBuilder 内容或使用新内容。你总是附加到同一个,所以内容被添加到前一个。

于 2013-06-08T00:57:59.210 回答