Java JTextArea中的文本未更新

Java JTextArea中的文本未更新,java,swing,jtextarea,Java,Swing,Jtextarea,我发现我的代码没有更新JTextArea中的文本。我创建了一个框架并添加了几个面板。其中一个面板包含我的JTextArea。最初我设置为“”。然后,在稍后的一次按钮点击中,除了其他代码之外,我更新了JTextArea。但是,更新不会显示在屏幕上 以下是我的代码片段: static JFrame window = new JFrame("Valedictorian Voting System"); static JTextArea voteNotification = new JTextArea(

我发现我的代码没有更新JTextArea中的文本。我创建了一个框架并添加了几个面板。其中一个面板包含我的JTextArea。最初我设置为“”。然后,在稍后的一次按钮点击中,除了其他代码之外,我更新了JTextArea。但是,更新不会显示在屏幕上

以下是我的代码片段:

static JFrame window = new JFrame("Valedictorian Voting System");
static JTextArea voteNotification = new JTextArea(" ");

static JPanel options = new JPanel();
static JPanel options1 = new JPanel();
static JPanel options2 = new JPanel();
然后在我的主要发言中:

ImageIcon标题;
图像图标侧;
图像图标侧2;
Image Image=newimageicon(“src\\Header_MICHS.jpg”).getImage();
header=newimageicon(image.getScaledInstance(290,55,java.awt.image.SCALE_-SMOOTH));
Image image2=新的图像图标(“src\\lightningbolt2.png”).getImage();
side=newimageicon(image2.getScaledInstance(50235,java.awt.Image.SCALE_-SMOOTH));
Image image3=新的图像图标(“src\\lightningbolt.png”).getImage();
side2=新的ImageIcon(image3.getScaleInstance(50235,java.awt.Image.SCALE_-SMOOTH));
JLabel sideLabel2=新的JLabel(side2);
JLabel侧标签=新的JLabel(侧);
JLabel headerLabel=新的JLabel(收割台);
//底部栏添加-文本字段和Jbutton
JPanel studentInfo=新的JPanel();
设置布局(新的网格布局(1,2));
学生信息添加(学生编号);
studentInfo.add(studentNumButton);
//将val单选按钮添加到面板
JPanel options1=新的JPanel();
选项1.setLayout(新的GridLayout(val2.length,0));
对于(int i=0;i
其中一个面板包含我的JTextArea。最初我将其设置为“”

如果您最初使用一些文本设置文本区域,会发生什么情况?您看到文本了吗

我猜你看不到文本是因为你创建文本区域的方式:

static JTextArea voteNotification = new JTextArea(" ");
也许可以试试:

static JTextArea voteNotification = new JTextArea(2, 30);

因此,文本区域有一个合理的首选大小。

没有足够的代码可以理解。如果您想发布所有代码,那就更好了。为了更快地获得更好的帮助,请发布一个@tbodt,这样您就可以在600多个LOC中查找分布在六个类中的错误了?您将处于“选择组”中。我们大多数人都更喜欢SSCE.@AndrewThompson如果我在堆栈溢出问题中看到1000行代码,我会晕倒。我没想到会有太多代码。@tbodt当然我只是猜测LOC。它可能是1000行(所以不会接受),可能是600行(可以压缩),也可能是120行(完全可行).OTOH我很小心,从不暗示我们希望看到“所有”代码-只是一个SSCCE:)以下是所有代码:如果我在开始时将文本放入文本区域,它确实会显示出来。即使在单步执行代码时,它也不会更新。如果文本区域不更新,则表示以下两种情况之一:1)您正在阻止EDT,因此GUI无法重新绘制自身;2)您没有对文本区域的引用,这意味着您已经定义了两次。
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
public class StandaloneGUIVotingSystem/* extends JApplet*/{

  static JFrame window = new JFrame("Valedictorian Voting System");
  static HintTextField studentNumber = new HintTextField("Enter student #!");
  static JTextArea voteNotification = new JTextArea("This is where your confirmation message will show up.");
  static ButtonGroup studentGroup;
  static JRadioButton[] val2;
  static int[] votes;

  static JPanel options = new JPanel();
  static JPanel options1 = new JPanel();
  static JPanel options2 = new JPanel();

  static CreateFile creator = new CreateFile();
  static FileHandling fileH = new FileHandling();

  static int studentsVoting = 0;
  static int numVals = 0;
  static int studentsVoted = 0;
  static boolean stillVotable = false;
  static String filePath = "src//";

  static DynamicArrayOfString studentNum = new DynamicArrayOfString();
  static DynamicArrayOfString val = new DynamicArrayOfString();

  //Window window; applet stuff

  //checks the student number entered to confirm a valid vote from a student that has not voted


  public static boolean checkNum(){ 
    for(int i = 0; i<studentNum.size(); i++){
      if(studentNumber.getText().equals(studentNum.get(i))){
        return true;
      }
    }
    JOptionPane.showMessageDialog(null, "This is not a valid student number. Please enter a different one!");
    return false;
  }

  //checks to see if the user has selected a radio button


  public static boolean checkSelected(){
    stillVotable = checkNum();
    boolean selected = false;
    for(int i = 0; i<numVals; i++){
      if(val2[i].isSelected() && stillVotable==true){
        votes[i] +=1;

        voteNotification.setText("Student No.: " + studentNumber.getText() + " voted for: " + val.get(i));
        options2.repaint();



        System.out.println("Voted for " + val.get(i) + ":" + votes[i]);
        selected = true;
        checkWinner();//checks to see if someone won
        return true;
      }
    }
    if(selected==false && stillVotable==true)
      JOptionPane.showMessageDialog(null, "You have not selected a Valedictorian!");
    else
      selected=false;
    return false;
  }

  //resets the screen for the next user


  public static void clear(){
    studentNumber.setText("");
    studentGroup.clearSelection();
    studentNumber.showHint();
    voteNotification.setText("");
  }

  public static void write(){
    //write winner file
    CreateFile.openFile("Winner.txt");
    CreateFile.addToFile("The Results of the Voting Contest are:");
    for(int i = 0; i < val.size(); i++){
      CreateFile.addToFile(val.get(i) + " has " + votes[i] + " votes.");
    }
    CreateFile.closeFile();

    //write voter file
    CreateFile.openFile("Voters.txt");
    for(int i = 0; i < studentNum.size(); i++){
      CreateFile.addToFile(studentNum.get(i));
    }
    CreateFile.closeFile();
  }

  //removes the person who voted from the text file so they can only vote once
  public static void removeVoted(){
    for(int i = 0; i < val.size(); i++){
      if(studentNumber.getText().equals(studentNum.get(i))){
        studentNum.remove(i);
        studentsVoted++;
      }
    }
  }

  //checks if there is a winner and finishes program if there is
  public static void checkWinner(){

  if (studentsVoted == studentsVoting){
      int j=0;
      int largest = votes[0];  
      for(int i = 1; i < votes.length; i++){  
        if(votes[i] > largest){  
          largest = votes[i];  
          j = i;
        }  
      }
      CreateFile.openFile("Winner.txt");
      CreateFile.addToFile("The Results of the Voting Contest are:");
      for(int i = 0; i<val.size();i++){
        CreateFile.addToFile(val.get(i) + " has " + votes[i] + " votes.");
      }
      CreateFile.addToFile("\n");
      CreateFile.addToFile("Therefore, the winner is...");
      CreateFile.addToFile("");
      CreateFile.addToFile("");
      CreateFile.addToFile("");
      CreateFile.addToFile(val.get(j) + " wins!");
      CreateFile.closeFile();
      System.exit(0);
    }
  }

  //retrieves the initial information
  public static void retrieve(){
    //takes the student numbers and adds them to the dynamic array called studentNum
  fileH.openFile("Voters.txt");
    fileH.readFile();
    studentsVoting = fileH.length();
    for(int i = 0; i<studentsVoting;i++){
      studentNum.put(i, fileH.get(i));
    }
    fileH.closeFile();

    //gets the valedictorian names and puts them in the array val
    fileH.openFile("Valedictorians.txt");
    fileH.readFile();
    numVals = fileH.length();

    //for testing
    System.out.println(numVals);


    for(int i = 0; i < numVals;i++){
      val.put(i, fileH.get(i));

      //for testing
      System.out.println(val.get(i));
    }   
      val2 = new JRadioButton[numVals];
      votes = new int[numVals];

    fileH.closeFile();

    //takes the number of votes and places them in the votes array
    fileH.openFile("Winner.txt");
    fileH.readFile();
    int h = 0;
    for(int i = 9; i<fileH.length(); i += 4){

        System.out.println(fileH.get(i));

        votes[h]= Integer.parseInt(fileH.get(i));

      //for testing
      System.out.println("Votes for:" + val.get(h) + " are: "+ votes[h]);


      h++;
    }
    fileH.closeFile();

  }


  //if the user hits the vote button this method will run the steps needed to accomplish that goal
  public static void voted(){
    checkSelected();//checks student number, and adds a vote to the person selected
    removeVoted(); // removes the person who voted so that each person can only vote once



    clear();//clears the studentNumber and val selected
    write();// writes the updated info to the files Voters and Winner
    checkWinner(); //checks to see if the vote was the last

  }

  public static void main(String[] args){
    retrieve();


    studentGroup = new ButtonGroup();

    //initalizes the names of each val on to radio buttons
  //adds each radio button to the button group and sets the background to white for a clear gui look
    for(int i = 0; i<numVals;i++){
      val2[i] = new JRadioButton(val.get(i));
      studentGroup.add(val2[i]);
      val2[i].setBackground(Color.WHITE);
    }

    voteNotification.setEditable(false);


    //vote button - allows the user to let the program know that they are done entering their student number and selecting a person for their vote
    JButton studentNumButton = new JButton("Vote!");
    studentNumButton.addActionListener(new SelectionCounter());

//this code could be added to show a picture of each valedictorian *******EXTRA************
    // John.setRolloverEnabled(true);
    // John.setRolloverIcon(icon);

    //images
    //int NEW_WIDTH = 50;
    //int NEW_HEIGHT = 235;
    //int NEW_WIDTH2 = 290;
    //int NEW_HEIGHT2 = 55;


    ImageIcon header;
    ImageIcon side;
    ImageIcon side2;
    Image image = new ImageIcon("src\\Header_MICHS.jpg").getImage();    
    header = new ImageIcon(image.getScaledInstance(290, 55, java.awt.Image.SCALE_SMOOTH));
    Image image2 = new ImageIcon("src\\lightningbolt2.png").getImage() ;    
    side = new ImageIcon(image2.getScaledInstance(50, 235, java.awt.Image.SCALE_SMOOTH));
    Image image3 = new ImageIcon("src\\lightningbolt.png").getImage() ;  
    side2 = new ImageIcon(image3.getScaledInstance(50, 235, java.awt.Image.SCALE_SMOOTH));
    JLabel sideLabel2 = new JLabel(side2);
    JLabel sideLabel = new JLabel(side);
    JLabel headerLabel = new JLabel(header);

    //bottom bar add - the text field and Jbutton
    JPanel studentInfo = new JPanel();
    studentInfo.setLayout(new GridLayout(1,2));
    studentInfo.add(studentNumber);
    studentInfo.add(studentNumButton);



    //add the val radio buttons to a panel
    JPanel options1 = new JPanel();
    options1.setLayout(new GridLayout(val2.length,0));
    for(int i=0; i<val2.length;i++){
      options1.add(val2[i]);
    }

    //add the message box to a panel

    options2.setLayout(new GridLayout(1,0));
    options2.setBackground(Color.WHITE);
    options2.add(voteNotification);


    //add the panels to a panel

    options.setLayout(new GridLayout(1,2));
    options.add(options1);
    options.add(options2);

    //put the functional things into one panel
    JPanel setup = new JPanel();
    setup.setLayout(new BorderLayout());
    setup.add(options, BorderLayout.CENTER);
    setup.add(studentInfo, BorderLayout.SOUTH);

    //put the pictures around the functional things
    JPanel content = new JPanel();
    content.setLayout(new BorderLayout());
    content.add(setup, BorderLayout.CENTER);
    content.add(headerLabel, BorderLayout.NORTH);
    content.add(sideLabel, BorderLayout.EAST);
    content.add(sideLabel2, BorderLayout.WEST);


    window.setLayout(new BorderLayout());
    window.add(content, BorderLayout.CENTER);
    window.setContentPane(content);
    window.setSize(300,310);
    window.setLocation(550,250);
    window.setVisible(true);  
    window.setResizable(false);
  }
  private static class SelectionCounter implements ActionListener{
    public void actionPerformed(ActionEvent e){
      voted();
    }
  }//end of SelectionCounter class


  public static class FileHandling{ 
    private Scanner file;
    static DynamicArrayOfString temp;
    String name;

    public void openFile(String name){
      try{
        file = new Scanner(new File(filePath + name)); //gets name=file u want to read
      }
      catch(Exception e){
        System.out.println("Could not find file!");
      }
    }

    public void readFile(){ // retrieves one input from the file for each line
        temp = new DynamicArrayOfString();
      for(int i = 0; file.hasNext(); i++){
        temp.put(i,file.next());
       System.out.printf("%s%n", temp.get(i));
      }
    }

    public int length(){
      return temp.size();
    }

    public boolean hasNext(){
      return file.hasNext();
    }

    public String get(int position) {
      if (position >= temp.size())
        return "Nothing";
      else{
        String answer = temp.get(position);
        //System.out.println(temp.get(position));
        return answer;
      }
    }

    public void closeFile(){
      file.close();
    }
  }//end of FileHandling


  private static class CreateFile{
    private static Formatter file2;

    public static void openFile(String name){
      try{
        file2 = new Formatter(filePath + name);
      }
      catch(Exception e){
        System.out.println("you have an error");
      }
    }
    public static void addToFile(String number){
      file2.format("%s%n", number);
    }
    public static void closeFile(){
      file2.close();
    }
  }//end of CreateFile

  static class HintTextField extends JTextField implements FocusListener {

    private final String hint;
    private boolean showingHint;

    public HintTextField(final String hint) {
      super(hint);
      this.hint = hint;
      this.showingHint = true;
      super.addFocusListener(this);
    }

    public void focusGained(FocusEvent e) {
      if(this.getText().isEmpty()) {
        super.setText("");
        showingHint = false;
      }
    }

    public void focusLost(FocusEvent e) {
      if(this.getText().isEmpty()) {
        super.setText(hint);
        showingHint = true;
      }
    }

    public String getText() {
      return showingHint ? "" : super.getText();
    }
    public void showHint(){
      super.setText(hint);
    this.showingHint=true;
    }
static JTextArea voteNotification = new JTextArea(" ");
static JTextArea voteNotification = new JTextArea(2, 30);