我的项目是读取文件,显示其详细信息,并将这些细节压缩到每个团队的排名系统中。我目前只是将这些细节输出到我创建的JTextArea中。
import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
public class CollegeFootballPlayoffManager
{
private ArrayList<Team> rank;
private File newFile;
private JFileChooser fileChooser;
private Scanner input;
StringBuilder sb;
public CollegeFootballPlayoffManager()
{
rank = new ArrayList<>();
}
public void openFile() throws Exception
{
fileChooser = new JFileChooser();
try
{
if(fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
newFile = fileChooser.getSelectedFile();
input = new Scanner(newFile);
}
else
{
JOptionPane.showMessageDialog(null, "No file was selected");
}
}
catch(Exception e)
{
e.printStackTrace();
}
input.close();
}
public void readFile()
{
sb = new StringBuilder();
while(input.hasNext())
{
sb.append(input.nextLine());
sb.append("\n");
}
input.close();
}
}
这是我在文件中读取的内容,然后将文件内容输出到JTextArea。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class UI extends JFrame
{
private static final long serialVersionUID = 1L;
private final int width = 650;
private final int height = 400;
private JButton rVotes, rTeams, sDetails, exit;
private JPanel topPanel;
private JTextArea text;
public UI()
{
JOptionPane.showMessageDialog(null, "Select 'Read Votes' to begin... ");
setTitle("College Football Playoff Selection");
setSize(width, height);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildTop();
add(topPanel, BorderLayout.NORTH);
text = new JTextArea();
text.setFont(new Font("Monospaced", Font.PLAIN,10));
text.setBackground(Color.LIGHT_GRAY);
text.setForeground(Color.BLACK);
text.setBounds(150,100,150,10);
text.setEditable(false);
add(text);
}
private void buildTop()
{
topPanel = new JPanel(new FlowLayout());
topPanel.setBackground(Color.LIGHT_GRAY);
rVotes = new JButton("Read Votes");
rVotes.setActionCommand("V");
rVotes.addActionListener(new ButtonListener());
rTeams = new JButton("Rank Teams");
rTeams.setActionCommand("R");
rTeams.addActionListener(new ButtonListener());
sDetails = new JButton("Show Details");
sDetails.setActionCommand("S");
sDetails.addActionListener(new ButtonListener());
exit = new JButton("Exit");
exit.setActionCommand("E");
exit.addActionListener(new ButtonListener());
topPanel.add(rVotes);
topPanel.add(rTeams);
topPanel.add(sDetails);
topPanel.add(exit);
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
switch(e.getActionCommand())
{
/** Only occurs when "V" button is clicked */
case "V":
CollegeFootballPlayoffManager read = new CollegeFootballPlayoffManager();
try {
read.openFile();
} catch (Exception e1)
{
// TODO Auto-generated catch block
e1.printStackTrace();
}
break;
/** Only occurs when "R" button is clicked */
case "R":
break;
/** Only occurs when "S" button is clicked */
case "S":
CollegeFootballPlayoffManager show = new CollegeFootballPlayoffManager();
text.setText(show.sb.toString());
break;
/** Only occurs when "E" button is clicked */
case "E":
System.exit(0);
break;
}
}
}
public static void main(String[] args)
{
UI frame = new UI(); /** declare and instantiate a new UI object */
frame.setVisible(true); /** Makes window visible */
}
}
这是我的GUI窗口,我收到了这段代码的错误。
text.setText(show.sb.toString());