我正在开发一个从uno卡的集合(套牌)中抽取卡片的程序。它应该在启动时显示第一张卡,然后每次显示"下一张卡"按下按钮它会绘制另一张卡片。然而,我对GUI很新,并且无法弄清楚我做错了什么。我得到了#34; Uno"在应显示卡片文本表示的框中。
package Uno;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GameFrame extends JFrame
{
private PlayingCardPanel panel;
private JPanel field;
private JPanel menu;
public GameFrame(){
this.setLayout(new FlowLayout());
this.setSize( 300, 150);
this.setTitle("UNO");
this.createFieldPanel();
this.createMenuPanel();
this.add(field);
this.add(menu);
}
private void createMenuPanel(){
menu = new JPanel();
menu.setBounds(0,300,250,100);
JButton drawButton = new JButton("Next Card");
drawButton.setSize(300,200);
drawButton.addActionListener(new drawListener());
menu.add(drawButton);
}
private void createFieldPanel()
{
field = new JPanel();
field.setBounds(0,0,250,200);
panel = new PlayingCardPanel();
field.add(panel);
}
private class drawListener implements ActionListener{
public void actionPerformed(ActionEvent e){
panel.drawCard();
}
}
}
package Uno;
import java.awt.*;
import javax.swing.*;
public class PlayingCardPanel extends JPanel{
private StandardDeck playingdeck;
private PlayingCard card;
public PlayingCardPanel(){
this.setPreferredSize(new Dimension(100,100));
playingdeck = new StandardDeck();
playingdeck.shuffle();
playingdeck.drawTopCard();
}
public void drawCard(){
playingdeck.drawTopCard();
}
public void paintComponent(Graphics g){
super.paintComponent(g);
int panelWidth = this.getWidth();
int panelHeight = this.getHeight();
g.drawRoundRect(0, 0, panelWidth-1, panelHeight-1, 10, 10);
card = playingdeck.drawTopCard();
g.drawString(card.toString(), 75, 75);
}
}
package Uno;
import Uno.GameFrame;
public class GameApp{
/**
* @param args
*/
public static void main(String[] args)
{
GameFrame aBoard = new GameFrame();
aBoard.setVisible(true);
}
}
package Uno;
public class PlayingCard {
private int number;
private String color;
public int getNumber() {
return number;
}
public String getColor(){
return color;
}
public PlayingCard(PlayingCard card){
number = card.getNumber();
color = new String(card.getColor());
}
public PlayingCard(int cardNumber, String cardColor){
number = cardNumber;
color = cardColor;
}
}
package Uno;
public class PlayingCard {
private int number;
private String color;
public int getNumber() {
return number;
}
public String getColor(){
return color;
}
public PlayingCard(PlayingCard card){
number = card.getNumber();
color = new String(card.getColor());
}
public PlayingCard(int cardNumber, String cardColor){
number = cardNumber;
color = cardColor;
}
}
package Uno;
import java.util.Collections;
import java.util.Vector;
public class StandardDeck {
protected Vector<PlayingCard> cardsVector;
public StandardDeck(){
cardsVector = new Vector<PlayingCard>();
String[] colors = {"blue","red","yellow","green"};
for(String color : colors){
for(int i=1;i<=9;i++){
cardsVector.add(new PlayingCard(i,color));
cardsVector.add(new PlayingCard(i,color));
}
cardsVector.add(new PlayingCard(0,color));
for(int i=-3;i<0;i++){
cardsVector.add(new PlayingCard(i,color));
cardsVector.add(new PlayingCard(i,color));
}
}
for(int i=0;i<4;i++){
cardsVector.add(new PlayingCard(-4,"black"));
cardsVector.add(new PlayingCard(-5,"black"));
}
}
public int getSize(){
return cardsVector.size();
}
public StandardDeck reset(){
StandardDeck resetDeck = new StandardDeck();
return resetDeck;
}
public void shuffle(){
Collections.shuffle(cardsVector);
}
public PlayingCard drawTopCard(){
return new PlayingCard(cardsVector.remove(cardsVector.size()-1));
}
}
package Uno;
import Uno.IPlayingCard;
public interface IDeck
{
public void reset();
public void shuffle();
public IPlayingCard drawTopCard(PlayingCard card);
}
package Uno;
import Uno.PlayingCard;
public interface IPlayingCard extends Comparable<PlayingCard>{
public String getColor();
public String getNumber();
}
感谢您的帮助!
答案 0 :(得分:0)
它实际上是打印Uno.PlayingCard@56609959
之类的东西 - 虽然它的最后一部分被截断了,所以你只能在面板的边缘看到“Uno”。
您在PlayingCard课程中缺少toString()
实施。
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int panelWidth = this.getWidth();
int panelHeight = this.getHeight();
g.drawRoundRect(0, 0, panelWidth - 1, panelHeight - 1, 10, 10);
card = playingdeck.drawTopCard();
g.drawString(card.toString(), 75, 75); // you're calling toString() here...
}
由于您没有实现它,因此它调用默认的Object.toString()方法。你可能想在你的PlayingCard课程中添加这样的东西吗?
public String toString() {
return ""+number;
}