我试图使用arrayList填充jTable,但我无法。我有4节课。我认为我不能将SoccerViewPlayer类中的manchester arraylist等同于SoccerEnterPlayer类中用户输入的团队arraylist。
SoccerViewPlayer类
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.awt.event.ActionEvent;
import javax.swing.JPanel;
import javax.swing.border.SoftBevelBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.border.BevelBorder;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import java.awt.Insets;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import javax.swing.Box;
import javax.swing.JTable;
import java.awt.ScrollPane;
public class SoccerViewPlayers {
// Frame Classes - GUI
private JFrame frame;
private JTable table;
//private String[] columns = new String[] {"Name","Age", "Gender", "Team Level" };
String[] columns = {"Player Name","Gender", "Age", "Team Level","Position","Overall Shots","Shots On Target", "Goals", "Saves"};
private JScrollPane scrollPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SoccerViewPlayers window = new SoccerViewPlayers();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public SoccerViewPlayers() {
initialize();
}
private void initialize() {
// Deals with program frame.
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.getContentPane().setLayout(null);
String[][] werty = new String[][]{
{}
//{"Sandra","Female","21", "Varsity","Striker","17","8", "5", "0"},
};
table = new JTable(werty, columns);
scrollPane.setViewportView(table);
DefaultTableModel model = (DefaultTableModel) table.getModel();
ArrayList<SoccerPlayer> manchester = ArrayList();
Object rowData[] = new Object [9];
for(int i = 0; i < manchester.size(); i++)
{
rowData[0] = manchester.get(i).getPlayerName();
rowData[1] = manchester.get(i).getGender();
rowData[2] = manchester.get(i).getage();
rowData[3] = manchester.get(i).getTeamLevel();
rowData[4] = manchester.get(i).getPosition();
rowData[5] = manchester.get(i).getOverallShots();
rowData[6] = manchester.get(i).getShotsOnTarget();
rowData[7] = manchester.get(i).getGoals();
rowData[8] = manchester.get(i).getSaves();
model.addRow(rowData);
}
}
The Soccer Class
import java.util.ArrayList;
public class Soccer {
public ArrayList<SoccerPlayer> SoccerPlayers;
public void addPlayers(String PlayerName, String Gender, int age, String teamLevel, String position, int shotsOnTarget, int overallShots, int goals, int saves)
{
SoccerPlayers.add(new SoccerPlayer(PlayerName, Gender, age, teamLevel, position, shotsOnTarget, overallShots, goals, saves));
The SoccerPlayer class
public class SoccerPlayer {
private String PlayerName;
private String position;
private String Gender;
private int shotsOnTarget;
private int overallShots;
private int goals;
private int saves;
private String teamLevel;
private int age;
public SoccerPlayer(String PlayerName, String Gender, int age, String teamLevel, String position, int shotsOnTarget, int overallShots, int goals, int saves) {
this.PlayerName = PlayerName;
this.age = age;
this.Gender = Gender;
this.teamLevel = teamLevel;
this.position = position;
this.shotsOnTarget = shotsOnTarget;
this.overallShots = overallShots;
this.goals = goals;
this.saves = saves;
}
public String getDetails()
{
System.out.println("Team Level:" + teamLevel);
System.out.println("Player Name:" + PlayerName);
System.out.println("Gender:" + Gender);
System.out.println("Age:" + age);
System.out.println("Position:" + position);
System.out.println("Shots:" + overallShots);
System.out.println("Shots On Target:" + shotsOnTarget);
System.out.println("Goals:" + goals);
System.out.println("Saves:" + saves);
return null;
}
public String getPlayerName()
{
return PlayerName;
}
public String getGender()
{
return Gender;
}
public String getTeamLevel()
{
return teamLevel;
}
public String getPosition()
{
return position;
}
public int getShotsOnTarget()
{
return shotsOnTarget;
}
public int getOverallShots()
{
return overallShots;
}
public int getGoals()
{
return goals;
}
public int getSaves()
{
return saves;
}
public int getage()
{
return age;
}
}
The SoccerEnterPlayer class
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
import java.awt.Font;
import javax.swing.JButton;
public class SoccerEnterPlayer extends JFrame {
private JPanel contentPane;
private static SoccerEnterPlayer frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
frame = new SoccerEnterPlayer();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public SoccerEnterPlayer() {
String[] genders = { "Male", "Female" };
String[] teamLevel = {"Varsity", "Junior Varsity"};
String[] positions = {"Goalkeeper", "Defender", "Midfielder", "Striker"};
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JTextArea textArea = new JTextArea();
textArea.setBounds(105, 56, 84, 16);
contentPane.add(textArea);
JTextArea textArea_1 = new JTextArea();
textArea_1.setBounds(300, 56, 84, 16);
contentPane.add(textArea_1);
JTextArea textArea_5 = new JTextArea();
textArea_5.setBounds(300, 118, 84, 16);
contentPane.add(textArea_5);
JTextArea textArea_6 = new JTextArea();
textArea_6.setBounds(105, 177, 84, 16);
contentPane.add(textArea_6);
JTextArea textArea_7 = new JTextArea();
textArea_7.setBounds(204, 177, 84, 16);
contentPane.add(textArea_7);
JTextArea textArea_8 = new JTextArea();
textArea_8.setBounds(300, 177, 84, 16);
contentPane.add(textArea_8);
JLabel lblPlayerNamer = new JLabel("Player Name");
lblPlayerNamer.setHorizontalAlignment(SwingConstants.CENTER);
lblPlayerNamer.setBounds(105, 28, 84, 16);
contentPane.add(lblPlayerNamer);
JLabel lblGender = new JLabel("Gender");
lblGender.setHorizontalAlignment(SwingConstants.CENTER);
lblGender.setBounds(204, 28, 84, 16);
contentPane.add(lblGender);
JLabel lblAge = new JLabel("Age");
lblAge.setHorizontalAlignment(SwingConstants.CENTER);
lblAge.setBounds(300, 28, 84, 16);
contentPane.add(lblAge);
JLabel lblTeamLevel = new JLabel("Team Level");
lblTeamLevel.setHorizontalAlignment(SwingConstants.CENTER);
lblTeamLevel.setBounds(105, 90, 84, 16);
contentPane.add(lblTeamLevel);
JLabel lblPosition = new JLabel("Position");
lblPosition.setHorizontalAlignment(SwingConstants.CENTER);
lblPosition.setBounds(204, 90, 84, 16);
contentPane.add(lblPosition);
JLabel lblOverallShots = new JLabel("Overall Shots");
lblOverallShots.setHorizontalAlignment(SwingConstants.CENTER);
lblOverallShots.setBounds(300, 90, 84, 16);
contentPane.add(lblOverallShots);
JLabel lblShotsOnTarget = new JLabel("Shots on Target");
lblShotsOnTarget.setFont(new Font("Lucida Grande", Font.PLAIN, 13));
lblShotsOnTarget.setHorizontalAlignment(SwingConstants.CENTER);
lblShotsOnTarget.setBounds(91, 149, 114, 16);
contentPane.add(lblShotsOnTarget);
JLabel lblGoals = new JLabel("Goals");
lblGoals.setHorizontalAlignment(SwingConstants.CENTER);
lblGoals.setBounds(204, 149, 84, 16);
contentPane.add(lblGoals);
JLabel lblSaves = new JLabel("Saves");
lblSaves.setHorizontalAlignment(SwingConstants.CENTER);
lblSaves.setBounds(300, 149, 84, 16);
contentPane.add(lblSaves);
JComboBox comboBox = new JComboBox(genders);
comboBox.setToolTipText("");
comboBox.setMaximumRowCount(2);
comboBox.setSelectedIndex(1);
comboBox.setBounds(198, 52, 100, 27);
contentPane.add(comboBox);
JComboBox comboBox_1 = new JComboBox(teamLevel);
comboBox_1.setToolTipText("");
comboBox_1.setSelectedIndex(1);
comboBox_1.setMaximumRowCount(2);
comboBox_1.setBounds(105, 114, 100, 27);
contentPane.add(comboBox_1);
JComboBox comboBox_2 = new JComboBox(positions);
comboBox_2.setBounds(204, 114, 84, 27);
contentPane.add(comboBox_2);
JButton btnNewButton = new JButton("Submit");
btnNewButton.setBounds(300, 221, 117, 29);
contentPane.add(btnNewButton);
btnNewButton.addActionListener(e->{
Soccer team = new Soccer();
team.addPlayers(textArea.getText(), comboBox.getSelectedItem().toString(), Integer.parseInt(textArea_1.getText()), comboBox_1.getSelectedItem().toString(), comboBox_2.getSelectedItem().toString(), Integer.parseInt(textArea_5.getText()), Integer.parseInt(textArea_6.getText()), Integer.parseInt(textArea_7.getText()), Integer.parseInt(textArea_8.getText()));
new SoccerFrame();
frame.dispose();
});
}
}
答案 0 :(得分:0)
您正在迭代一个空列表:
ArrayList<SoccerPlayer> manchester = ArrayList();
Object rowData[] = new Object [9];
for(int i = 0; i < manchester.size(); i++)
{
...
}
manchester.size()
为0。
首先,您必须将数据添加到manchaster列表中。像:
ArrayList<SoccerPlayer> manchester = ArrayList();
manchaster.add(new SoccerPlayer("Tom", "male", ....));
Object rowData[] = new Object [9];
for(int i = 0; i < manchester.size(); i++)
{
...
}