我现在正在使用swing创建一个java swing的全垒打迷你游戏。这是我第一次使用swing,我在使用包含玩家数据的ArrayList时遇到了麻烦。我试图将数组列表中的数据显示为JTextArea,但似乎无法弄清楚。
UI类:
package Main;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
public class UI extends JPanel {
DerbyManager dm;
JFrame window;
public JPanel bgPanel[] = new JPanel[50];
public JTextArea info = new JTextArea();
public JLabel bgLabel[] = new JLabel[50];
public ImageIcon bgIcon[] = new ImageIcon[50];
public UI(DerbyManager dm) {
this.dm = dm;
createMainField();
generateScene();
window.setVisible(true);
}
public void createMainField() {
window = new JFrame("Ichiro's Secret Slugfest");
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(Color.black);
window.setLayout(null);
}
public void createBackground(int bgNum, String bgFileName) {
bgPanel[bgNum] = new JPanel();
bgPanel[bgNum].setBounds(0, 0, 800, 600);
bgPanel[bgNum].setBackground(null);
bgPanel[bgNum].setLayout(null);
window.add(bgPanel[bgNum]);
bgLabel[bgNum] = new JLabel();
bgLabel[bgNum].setBounds(0, 0, 800, 600);
ImageIcon bgIcon = new ImageIcon(getClass().getClassLoader().getResource(bgFileName));
bgLabel[bgNum].setIcon(bgIcon);
}
public ArrayList<Player> createInfo(int bgNum, int x, int y, int width, int height, String name) {
ArrayList<Player> derby = new ArrayList<Player>();
JTextArea info = new JTextArea();
info.setBounds(x, y, width, height);
for(Player d : derby) {
if(name == d.getName()) {
info.append(d.getName());
}
else {
info.append("Player Not Found");
}
}
derby.add(new Player(0, "Ichiro Suzuki", "OF", "Seattle Mariners", 84, "C", "A", 60.0, 3089, .311, 117, .757,
"Ichiro is one of TWO players to have won the ROTY and the MVP award in the same season."));
derby.add(new Player(1, "Ty Cobb", "CF", "Detroit Tigers", 78, "D", "A", 151.5, 4189, .366, 117, .944,
"'The Georgia Peach' wasn't considered a power hitter, but said to be able to hit a home run on comand."));
derby.add(new Player(2, "Lou Brock", "LF", "St. Louis Cardinals", 82, "C", "A", 45.3, 3023, .293, 149, .753,
"'The Georgia Peach' wasn't considered a power hitter, but said to be able to hit a home run on comand."));
derby.add(new Player(3, "Jackie Robinson", "LF, IF", "Brooklyn Dodgers", 86, "C", "A", 45.3, 3023, .293, 149, .753,
"'The Georgia Peach' wasn't considered a power hitter, but said to be able to hit a home run on comand."));
derby.add(new Player(4, "Barry Bonds", "LF", "San Francisco Giants", 99, "A", "A", 162.8, 2935, .298, 762, 1.051,
"Barry Bonds is considered to be the most feared hitter of all-time, tallying 688 intention walks in his career."));
derby.add(new Player(5, "Bo Jackson", "CF", "Kansas City Royals", 91, "A", "C", 183.1, 2873, .342, 714, 1.164,
"'The Sultan of Swat', 'The Titan of Terror', 'The Clolussus of Clout', 'The King of Crash'... 'The Great Bambino'!"));
derby.add(new Player(6, "Ted Williams", "LF", "Boston Red Sox", 97, "A", "A", 122.0, 2654, .344, 521, 1.116,
"A coin flip or is Ted Williams getting on base? Teddy Ballgame recorded a career OBP of .482!"));
derby.add(new Player(7, "Babe Ruth", "OF, P", "New York Yankees", 99, "A", "A", 183.1, 2873, .342, 714, 1.164,
"'The Sultan of Swat', 'The Titan of Terror', 'The Clolussus of Clout', 'The King of Crash'... 'The Great Bambino'!"));
return derby;
}
public void createButton(int bgNum, int x, int y, int width, int height, String buttonFileName, String action) {
ImageIcon buttonIcon = new ImageIcon(getClass().getClassLoader().getResource(buttonFileName));
JButton button = new JButton();
button.setBounds(x, y, width, height);
button.setBackground(null);
button.setContentAreaFilled(false);
button.setFocusPainted(false);
button.setIcon(buttonIcon);
button.addActionListener(dm.aHandler);
button.setActionCommand(action);
button.setBorderPainted(false);
bgPanel[bgNum].add(button);
}
public void createObject(int bgNum, int objX, int objY, int objWidth, int objHeight, String objFileName) {
//CREATE OBJECTS
JLabel objectLabel = new JLabel();
// objectLabel.setBounds(25, 25, 332, 236);
objectLabel.setBounds(objX, objY, objWidth, objHeight);
ImageIcon objectIcon = new ImageIcon(getClass().getClassLoader().getResource(objFileName));
objectLabel.setIcon(objectIcon);
bgPanel[bgNum].add(objectLabel);
}
public void createCard(int bgNum, int x, int y, int width, int height, String cardFileName, String action) {
ImageIcon cardIcon = new ImageIcon(getClass().getClassLoader().getResource(cardFileName));
JButton card = new JButton();
card.setBounds(x, y, width, height);
card.setBackground(null);
card.setContentAreaFilled(false);
card.setFocusPainted(false);
card.setIcon(cardIcon);
card.addActionListener(dm.aHandler);
card.setActionCommand(action);
card.setBorderPainted(true);
bgPanel[bgNum].add(card);
}
public void generateScene() {
//MAIN MENU
createBackground(1, "Ichiro Gif.gif");
//BUTTONS
createButton(1, 282, 508, 236, 59, "Play Button 236px.png", "playerSelect");
//CONTAINER
createObject(1, 0, 480, 400, 120, "Player Container.png");
createObject(1, 400, 480, 400, 120, "Player Container.png");
bgPanel[1].add(bgLabel[1]);
//PLAYER INTRODUCTIONS
createBackground(2, "Menu Background.png");
//PLAYERS
createCard(2, 25, 91, 130, 183, "Ichiro Flopps Menu.png", "ichiroCard");
createCard(2, 180, 91, 130, 183, "Jimmie Fox Flopps Menu.png", "jimmieCard");
createCard(2, 335, 91, 130, 183, "Barry Bonds Flopps Menu.png", "barryCard");
createCard(2, 490, 91, 130, 183, "Ted Williams Flopps Menu.png", "tedCard");
createCard(2, 25, 296, 130, 183, "Babe Ruth Flopps Menu.png", "babeCard");
createCard(2, 180, 296, 130, 183, "Bo Jackson Flopps Menu.png", "boCard");
createCard(2, 335, 296, 130, 183, "Rogers Hornsby Flopps Menu.png", "rogersCard");
createCard(2, 490, 296, 130, 183, "Ty Cobb Flopps Menu.png", "tyCard");
//BUTTONS
createButton(2, 582, 525, 200, 50, "Play Button.png", "derby");
createButton(2, 25, 525, 200, 50, "Exit Button.png", "mainMenu");
//CARD CONTAINERS
createObject(2, 645, 91, 130, 183, "Player Container.png");
createObject(2, 645, 296, 130, 183, "Player Container.png");
//MENU CONTAINER
createObject(2, 0, 505, 400, 95, "Player Container.png");
createObject(2, 400, 505, 400, 95, "Player Container.png");
bgPanel[2].add(bgLabel[2]);
//ICHIRO
createBackground(3, "Menu Background.png");
//CARD
createObject(3, 25, 25, 236, 332, "Ichiro Flopps.png");
//STATS
createObject(3, 286, 25, 489, 332, "Player Container.png");
createInfo(3, 286, 25, 489, 332, "Ichiro Suzuki");
//DYK?
createObject(3, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(3, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(3, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[3].add(bgLabel[3]);
//JIMMIE FOX
createBackground(4, "Menu Background.png");
//CARD
createObject(4, 25, 25, 236, 332, "Jimmie Fox Flopps.png");
//STATS
createObject(4, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(4, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(4, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(4, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[4].add(bgLabel[4]);
//BARRY BONDS
createBackground(5, "Menu Background.png");
//CARD
createObject(5, 25, 25, 236, 332, "Barry Bonds Flopps.png");
//STATS
createObject(5, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(5, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(5, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(5, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[5].add(bgLabel[5]);
//TED WILLIAMS
createBackground(6, "Menu Background.png");
//CARD
createObject(6, 25, 25, 236, 332, "Ted Williams Flopps.png");
//STATS
createObject(6, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(6, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(6, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(6, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[6].add(bgLabel[6]);
//BABE RUTH
createBackground(7, "Menu Background.png");
//CARD
createObject(7, 25, 25, 236, 332, "Babe Ruth Flopps.png");
//STATS
createObject(7, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(7, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(7, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(7, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[7].add(bgLabel[7]);
//BO JACKSON
createBackground(8, "Menu Background.png");
//CARD
createObject(8, 25, 25, 236, 332, "Bo Jackson Flopps.png");
//STATS
createObject(8, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(8, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(8, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(8, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[8].add(bgLabel[8]);
//ROGERS HORNSBY
createBackground(9, "Menu Background.png");
//CARD
createObject(9, 25, 25, 236, 332, "Rogers Hornsby Flopps.png");
//STATS
createObject(9, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(9, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(9, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(9, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[9].add(bgLabel[9]);
//TY COBB
createBackground(10, "Menu Background.png");
//CARD
createObject(10, 25, 25, 236, 332, "Ty Cobb Flopps.png");
//STATS
createObject(10, 286, 25, 489, 332, "Player Container.png");
//DYK?
createObject(10, 286, 382, 489, 193, "Player Container.png");
//BACK BUTTON
createButton(10, 25, 382, 236, 59, "Back Button 236px.png", "playerSelect");
//PICK BUTTON
createButton(10, 25, 466, 236, 59, "Pick Button 236px.png", "playerSelect");
bgPanel[10].add(bgLabel[10]);
//DERBY
createBackground(11, "Derby Background.png");
//EXIT BUTTON
bgPanel[11].add(bgLabel[11]);
}
}
球员类:
package Main;
public final class Player {
//Player info
private int number;
private String name;
private String position;
private String team;
//Player ratings
private int OVR;
private String power;
private String contact;
//Player career statistics
private double WAR;
private int H;
private double BA;
private int HR;
private double OPS;
//Player fun fact
private String fact;
public Player(int number, String name, String position, String team, int OVR, String power, String contact,
double WAR, int H, double BA, int HR, double OPS, String fact) {
this.number = number;
this.name = name;
this.position = position;
this.team = team;
this.OVR = OVR;
this.power = power;
this.contact = contact;
this.WAR = WAR;
this.H = H;
this.BA = BA;
this.HR = HR;
this.OPS = OPS;
this.fact = fact;
}
//getters
public int getNumber() {
return number;
}
public String getName() {
return name;
}
public String getPosition() {
return position;
}
public String getTeam() {
return team;
}
public int getOVR() {
return OVR;
}
public String getPower() {
return power;
}
public String getContact() {
return contact;
}
public double getWAR() {
return WAR;
}
public int getH() {
return H;
}
public double getBA() {
return BA;
}
public int getHR() {
return HR;
}
public double getOPS() {
return OPS;
}
public String getFact() {
return fact;
}
//setters
public void setNumber(int number) {
this.number = number;
}
public void setName(String name) {
this.name = name;
}
public void setPosition(String position) {
this.position = position;
}
public void setTeam(String team) {
this.team = team;
}
public void setOVR(int OVR) {
this.OVR = OVR;
}
public void setPower(String power) {
this.power = power;
}
public void setContact(String contact) {
this.contact = contact;
}
public void setWAR(double WAR) {
this.WAR = WAR;
}
public void setH(int H) {
this.H = H;
}
public void setBA(double BA) {
this.BA = BA;
}
public void setHR(int HR) {
this.HR = HR;
}
public void setOPS(double OPS) {
this.OPS = OPS;
}
public void setFact(String fact) {
this.fact = fact;
}
@Override
public String toString() {
return String.format("n%d OVR | %d %s | %s | %snContact: %s | Power: %snCareer Stats: %.1f WAR | %d H | %.3f BA | %d HR | %.3f OPS" + "n%s"
, OVR, name, number, position, team, contact, power, WAR, H, BA, HR, OPS, fact);
}
}
DerbyManager:
package Main;
import java.net.URL;
import Event.Event01;
public class DerbyManager {
ActionHandler aHandler = new ActionHandler(this);
public UI ui = new UI(this);
public SceneChanger sChanger = new SceneChanger(this);
Music music = new Music();
SoundEffect sound = new SoundEffect();
public Event01 ev1 = new Event01(this);
//GAME SOUND
public URL sweetTea = getClass().getClassLoader().getResource("Music/Sweet Tea.wav");
public URL jamesFranco = getClass().getClassLoader().getResource("Music/James Franco.wav");
public URL snowblood = getClass().getClassLoader().getResource("Music/Snowblood.wav");
public URL powerPros = getClass().getClassLoader().getResource("Music/Power Pros.wav");
public URL play = getClass().getClassLoader().getResource("Sound Effect/Play Button.wav");
public URL currentMusic;
public static void main(String[] args) {
new DerbyManager();
}
public DerbyManager() {
currentMusic = jamesFranco;
playMusic(currentMusic);
sChanger.mainMenu();
}
public void playMusic(URL url) {
music.setFile(url);
music.play(url);
music.loop(url);
}
public void stopMusic(URL url) {
music.stop(url);
}
public void playSound(URL url) {
sound.play(url);
}
}
我想要能够显示玩家的统计数据和信息,当点击他们的卡片在JTextArea。如果您需要更多的信息,请告诉我。
虽然提供的代码是不可运行的(因为缺少很多),但有一些(好吧,几个)事情我想做一些不同的事情。这并不是说你应该这样做,但它可能会为你提供一些思考的想法。
我很可能使用List Interface而不是ArrayList使ArrayList<Player> derby = new ArrayList<Player>();
成为公共静态成员变量,例如:
public class UI extends JPanel {
public static List<Player> derby = new ArrayList<>();
// ..... The rest of class code .....
我很可能从数据库表或CSV样式的文本文件中填充这个列表,而不是像UI.createInfo()
方法中那样使用硬编码的数据。
这样做将允许列表中的数据可能在应用程序中的任何地方使用。目前,UI.createInfo()
方法返回一个ArrayList<Player>
,这个返回值没有在任何地方使用。相反,UI.createInfo()
接受的参数允许它显示一个特定的球员的信息,如果该球员是在数组列表中找到的。这是导致你整个情况的可能原因,稍后会详细介绍。这个方法似乎有两个目的,检索和显示特定球员的信息,并用球员数据填充ArrayList。在我看来……这不好。这个方法应该用于播放器信息显示到JTextArea或者命名为UI.getPlayerInfo()
,或者命名为UI.loadPlayersInfo()
来填充数组列表。
在同一个UI.createInfo()
方法中是一个主要问题。在for
循环中,if
语句条件使用==
比较两个字符串是否相等。这不会像你想的那样。
// If you want a precise name equality comparison.
if(name.equals(d.getName())) {
或
// If you want to be more forgiving with letter case.
if(name.equalsIgnoreCase(d.getName())) {