JTextArea 未在 gui 中更新
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须调用
revalidate()
而不是repaint()
。或者
不要在
actionPerformed
方法中将area
添加到bottom
- 在构造函数中执行此操作。那么就不需要revalidate
或repaint
You must call
revalidate()
instead ofrepaint()
.or
don't add the
area
tobottom
in theactionPerformed
method - do it in the constructor. Then no need forrevalidate
orrepaint
替换重绘();在执行的操作中使用 revalidate() 。您也可以删除paintComponent方法。
Replace repaint(); with revalidate() in your action performed. Also you can remove paintComponent method.