我发现我的代码没有更新 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();
然后在我的 main() 中:
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);
这一切都有效。但是当我点击“投票”按钮时......除了 JTextArea 没有更新之外,一切正常。
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;
}
我担心它与线程有关,因为我不理解其他一些帖子中的描述......
以下是所有代码:
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;
}