JTextArea没有在gui中更新

时间:2011-12-03 17:48:48

标签: java swing user-interface jbutton jtextarea

我有一个Java程序,它读取名称的文本文件并将它们分组。通过JTextArea输入每组的数量,点击“提交”JButton,然后随机分配名称。

最后,这些组被放置在JTextArea中。一切正常,直到名称出现在JTextArea中。它们没有出现。这应该在actionPerformed()方法中完成。

我可以让它们显示的唯一方法是在String方法中加入JTextArea's append字面值。然后我必须第二次点击“提交”并且名称确实出现,但String文字(“Groups\n”)出现两次,一次出现前后一次。

为什么我需要文字,为什么不立即显示文字。

这是我的代码:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class GUIForGroups extends JFrame implements ActionListener
{
    JLabel numOfG = new JLabel("How many per group?  ");
    JTextField num = new JTextField(3);
    JButton sub = new JButton("Make Groups!");
    JPanel top = new JPanel();

    JLabel botTitle = new JLabel("The Groups are:");
    private ArrayList<String> roster;
    private ArrayList<String> team;
    //     ArrayList<JLabel> numbers = new ArrayList<JLabel>();
    //     ArrayList<JTextField> teams = new ArrayList<JTextField>();
    JPanel bottom = new JPanel();
    JTextArea area = new JTextArea(400,20);
    JScrollPane scrollPane = new JScrollPane(area); 
    ArrayList<String> groups;
    int n;
    public GUIForGroups()
    {
        setSize(800,400);
        setTitle("Group Picker!");
        Container c = getContentPane();
        c.setLayout( new BoxLayout(c, BoxLayout.Y_AXIS ) );
        top.add(numOfG);
        top.add(num);
        top.add(sub);
        sub.addActionListener(this);
        c.add(top);
        c.add(bottom);
        try {
            roster = getRoster();
        }
        catch (IOException ioe) {
            JOptionPane.showMessageDialog(null,"Error reading in data, exiting");
            System.exit(0);
        }
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent evt) 
    {
        String userIn ;
        userIn    = num.getText()  ;
        n  = Integer.parseInt( userIn ) ;
        team = sortInGroups(n);
        String s = setPrint();
        //         /bottom.add(botTitle);

        area.append("Groups\n"+setPrint());  // PROBLEM HERE, NOT WORKING
       //          System.out.print(s);  // was for testing 
        bottom.add(area);
        add(bottom);

        repaint();
    }

    public void paintComponent(Graphics g)
    {
        area.append("The groups are: \n"+setPrint());
        add(bottom);
    }

    public ArrayList<String> getRoster() throws IOException
    {
        roster = new ArrayList<String>();
        Scanner fr = new Scanner(new File("csroster.txt"));
        while (fr.hasNext())
        {
            String name = fr.next();
            roster.add(name);
        }
        return roster;
    }

    public ArrayList sortInGroups(int n)
    {
        int i = 0, j=0;
        String next="";
        ArrayList<String> groups = new ArrayList<String>();
        while (roster.size() > 0 )
        {
            while (i<n && roster.size()>0)
            {
                int num = (int)(Math.random()*roster.size());
                next +=  roster.remove(num) + "   ";
                i++;
            }
            i=0; 
            groups.add(next);
            next = "";
        }
        return groups;
    }

    public String setPrint()
    {
        int teamNum = 1;
        String groups="";
        for (String t: team)
        {
            groups += "Team # "+teamNum+++" is: "+t+"\n";

        }
        return groups;
    }


    public static void main (String[] args) throws IOException
    {
        GUIForGroups gui = new GUIForGroups();
    }
}

2 个答案:

答案 0 :(得分:2)

您必须致电revalidate()而不是repaint()

public void actionPerformed(ActionEvent evt) 
{
    ...

    revalidate();
}

或 不要在area方法中将bottom添加到actionPerformed - 在构造函数中执行此操作。那么就不需要revalidaterepaint

public GUIForGroups()
{
    ...
    c.add(bottom);
    bottom.add(area);  // missing a JScrollPane here
    try {
        ...
}


public void actionPerformed(ActionEvent evt) 
{
    String userIn ;
    userIn    = num.getText()  ;
    n  = Integer.parseInt( userIn ) ;
    team = sortInGroups(n);
    String s = setPrint();

    area.append("Groups\n"+setPrint());
}

答案 1 :(得分:1)

替换repaint();在执行的操作中使用revalidate()。您也可以删除paintComponent方法。