JTextArea 未在 gui 中更新

发布于 2024-12-19 04:00:43 字数 3793 浏览 0 评论 0原文

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

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

让它们显示的唯一方法是在 JTextArea 的 append 方法中包含一个 String 文字。然后我必须第二次单击“提交”,名称确实出现,但 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();
    }
}

I have a Java program that reads a text file of names and sorts them into groups. The number per group is entered via a JTextArea, the "submit" JButton is clicked, then the names are assigned randomly.

Finally the groups are placed in a JTextArea. Everything works fine up until the names are supposed to appear in the JTextArea. They don't show up. This is supposed to be done in the actionPerformed() method.

The only way I can get them to show up is by including a String literal in the JTextArea's append method. Then I have to click "submit" a 2nd time and the names do appear but the String literal ("Groups\n") appears twice, once before and once after.

Why do I need the literal and why doesn't the text appear immediately.

Here's my code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2024-12-26 04:00:43

您必须调用 revalidate() 而不是 repaint()

public void actionPerformed(ActionEvent evt) 
{
    ...

    revalidate();
}

或者
不要在 actionPerformed 方法中将 area 添加到 bottom - 在构造函数中执行此操作。那么就不需要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());
}

You must call revalidate() instead of repaint().

public void actionPerformed(ActionEvent evt) 
{
    ...

    revalidate();
}

or
don't add the area to bottom in the actionPerformed method - do it in the constructor. Then no need for revalidate or repaint

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());
}
左耳近心 2024-12-26 04:00:43

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

Replace repaint(); with revalidate() in your action performed. Also you can remove paintComponent method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文