Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从arraylist持续更新滚动窗格,该列表已被转换为字符串';预计起飞时间_Java_Swing - Fatal编程技术网

Java 如何从arraylist持续更新滚动窗格,该列表已被转换为字符串';预计起飞时间

Java 如何从arraylist持续更新滚动窗格,该列表已被转换为字符串';预计起飞时间,java,swing,Java,Swing,我有一个问题可能很简单(尽管我已经完成了搜索)。我有一个arraylist,我可以在点击JButton时更新它。我已经在这个arraylist中添加了toString(),以便将它添加到textarea.setText(xx.toString())所以我可以在scrollpane上显示。问题是每次添加元素时,textarea都会显示数组,然后是新更新的数组,这意味着每次文本都会加倍。有没有其他方法可以解决这个问题?谢谢,我知道这是一个愚蠢的问题 public nameFrame() {

我有一个问题可能很简单(尽管我已经完成了搜索)。我有一个arraylist,我可以在点击JButton时更新它。我已经在这个arraylist中添加了toString(),以便将它添加到textarea.setText(xx.toString())所以我可以在scrollpane上显示。问题是每次添加元素时,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);
            }
        }


    });     



}
public nameFrame()
{
超级(“名称生成器”);
JPanel background=新的JPanel();
background.setLayout(新的BoxLayout(background,BoxLayout.X_轴));
name=新的ArrayList();
getContentPane().add(后台);
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();
背景。添加(垂直背面);
垂直后退。添加(水平框1);
水平框1.添加(垂直框1);
水平框1.添加(垂直框2);
垂直后退。添加(horizboxmid);
垂直后退。添加(水平框2);
JButton maleName=新JButton(“男性”);
JButton femaleName=新JButton(“女性”);
JButton clearLog=新JButton(“清除列表”);
JButton closeApp=新JButton(“关闭”);
verticalbox1.add(男性姓名);
verticalbox1.添加(femaleName);
verticalbox2.add(clearLog);
verticalbox2.add(closeApp);
final JScrollPane listName=new JScrollPane();
listName.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL\u SCROLLBAR\u ALWAYS);
horizbox2.add(列表名);
最终JTextArea name显示列表=新JTextArea();
nameDisplayList.setSize(300300);
添加(名称显示列表);
setViewportView(名称显示列表);
nameDisplayList.setEditable(假);
nameDisplayList.setVisible(true);
stringLog=Arrays.toString(name.toArray()).replaceAll(“\\[\124;\\\]”,“”)。replaceAll(“,”,“\n”);
最终StringBuilder sb=新StringBuilder();
for(字符串s:名称)
{
某人追加;
某人附加(“\n”);
}
nameDisplayList.setText(sb.toString());
///repaint();
///listName.revalidate();
nameDisplayList.setText(sb.toString());
maleName.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件)
{
{
试一试{
扫描=新扫描仪(新文件(“MaleNames.txt”);
ArrayList MaleFirstNames=新的ArrayList();
while(Scan.hasNext())
{
maleflastnames.add(Scan.next());
}
Scan.close();
int Mindex=new Random().nextInt(maleflastnames.size());
firstName=maleflastnames.get(Mindex);
Scan=新扫描仪(新文件(“names.txt”);
ArrayList姓氏列表=新的ArrayList();
while(Scan.hasNext())
{
姓氏列表.add(Scan.next());
}
Scan.close();
int Sindex=new Random().nextInt(姓氏列表.size());
姓氏=姓氏列表.get(Sindex);
fullName=String.format(“%s\t%s%n”,名字,姓氏);
name.add(全名);
//indexPoint++;
//System.out.println(名称);
nameDisplayList.revalidate();
nameDisplayList.repaint();
for(字符串s:名称)
{
某人追加;
某人附加(“\n”);
}
nameDisplayList.setText(sb.toString());
} 
catch(filenotfounde异常)
{
e、 printStackTrace();
}   
}
}           
});
femaleName.addActionListener(新ActionListener()
{
已执行的公共无效操作(操作事件)
{
{
试一试{
扫描=新扫描仪(新文件(“FemaleNames.txt”);
ArrayList FemaleFirstNames=新的ArrayList();
while(Scan.hasNext())
{
添加(Scan.next());
}
Scan.close();
int Findex=new Random().nextInt(FemaleFirstNames.size());
firstName=FemaleFirstNames.get(Findex);
Scan=新扫描仪(新文件(“names.txt”);
ArrayList姓氏列表=新的ArrayList();
while(Scan.hasNext())
{
姓氏列表.add(Scan.next());
}
Scan.close();
int Sindex=new Random().nextInt(姓氏列表.size());
姓氏=姓氏列表.get(Sindex);
fullName=String.format(“%s\t%s%n”,名字,姓氏);
name.add(indexPoint,fullName);
} 
catch(filenotfounde异常)
{
e、 printStackTrace();
}
}
}
});
addActionListener(新的ActionListener()
{
已执行的公共无效操作(操作事件)
{                   
{
做
{
name.add(name.size(),“”);
索引点--;
}while(indexPoint!=0);
}
}
});     
}
}

在你的“动作表演”中