Java GridLayout:south-JPanel 覆盖 center-JPanel?

Java GridLayout: south-JPanel covers center-JPanel?

我目前正在研究我的第一个真正的二十一点程序。快完成了,但我 运行 遇到了一个大问题。

当我 运行 代码时,它会按计划在中央 JPanel 中显示玩家卡片。但是一旦我完成并按下 结束回合,将只显示庄家牌,而不是两者。在调整我的 cardImages 的大小以使其变小之前,它们会显示为又高又窄的条带(只会显示卡中间的一条条带,而不是整个条带),从上到下覆盖整个屏幕。

我相信一旦我结束转弯并因此将 cardsImages 添加到南 JPanel,它会最小化中心 JPanel(大概是因为组件太高了)并最终占据所有 space .虽然我不确定情况是否如此,但这只是一个猜测。

即使我把图像变小了,虽然现在整张牌都出现了,但庄家牌仍然取代了屏幕上的玩家牌。所以我假设我需要以某种方式调整分配给图像的实际 space 大小?

通过 Whosebug 搜索让我相信我需要使用 gridbaglayout 并调整卡片实际 space 的大小,但我似乎找不到任何描述如何实际调整大小的线程,尽管我一整天和昨天都在为这个问题苦苦挣扎。

是的,任何帮助或见解将不胜感激!预先感谢。

如果你想使用卡片我制作了两个zip文件;一张是大卡片,一张是调整大小的卡片:

原尺寸(大): http://www.filedropper.com/cardsoriginalsizebigzip

调整大小: http://www.filedropper.com/cardsresizedsmallzip

我的代码: (请记住,它还没有完全完成,我仍然需要修复重启游戏按钮并添加一些信息、错误处理等,所以不要担心。这也是我的第一个学期,我的老师没有帮助我所以我的编程风格可能真的很糟糕,但请耐心等待我尽力而为。)

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.util.Random.*;

public class BlackJack
{ 
 public static void main(String[] args) 
 {
  Interface Win = new Interface ();
 }

 public static int value(int cardID)
 //stores values for each "card ID number" 
 {

  int [] cardValue = {2, 2, 2, 2, 
    3, 3, 3, 3, 4, 4, 4, 4, 
    5, 5, 5, 5, 6, 6, 6, 6, 
    7, 7, 7, 7, 8, 8, 8, 8, 
    9, 9, 9, 9, 10, 10, 10, 10, 
    10, 10, 10, 10, 10, 10, 10, 10, 
    10, 10, 10, 10, 11, 11, 11, 11};
  
  return cardValue[cardID];
 }
}

class Interface extends JFrame implements ActionListener
{
 
 int index;
 int IDIndex = 0;

 // Add all image components here:
 ImageIcon [] cardImage = new ImageIcon[52];
 {
 for (index = 0; index <= 51; index++)
  cardImage[index] = new ImageIcon(index + ".png");
 }
 ImageIcon Empty = new ImageIcon("blank");
 
 //identify variables
 int cardCounter = 0;
 int dealerCardCounter = 0;
 int cardID;
 boolean Continue;
 
 //keeps track of cards left in the deck. 
 boolean [] cardsLeft = new boolean [52];{
 for (index = 0; index < cardsLeft.length; index++)
  cardsLeft[index] = true;
 }
  
 // Interface components here:
 int plyrSum = 0;
 int dealerSum = 0;
 int aceCounter = 0;
 JLabel Info = new JLabel (""); //<== this will be (setText to) either "Bust!", "Black Jack!"/"21!" or "What would you like to do?"
 JLabel PlyrCardSum = new JLabel("Sum: " + plyrSum);
 JLabel DealerCardSum = new JLabel("Dealer sum: " + dealerSum);
 JLabel winLoseLabel = new JLabel("");
 
 JLabel [] cardLabel = new JLabel[20];
 {
  for (index = 0; index < 20; index ++)
   cardLabel[index] = new JLabel (Empty);
 }
 
 JButton hit = new JButton ("Hit!");
 JButton stay = new JButton ("Stay");
 JButton reset = new JButton ("New Round");
 
 //JPanels here:
 JPanel buttons = new JPanel(new GridLayout(1,2,5,15));
 JPanel plyrCards = new JPanel(new GridLayout(1,2,5,15));
 JPanel dealerCards = new JPanel (new GridLayout(1,2,5,15));
  
 public Interface ()
 {
  
  super ("BlackJack 21");
  setSize (1367,729);
  setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  setVisible (true);
    
  //GridBagConstraints card = new GridBagConstraints();
  
  Container contentArea = getContentPane();
  contentArea.setBackground(Color.white);
  
  //Event listeners
  hit.addActionListener (this);
  stay.addActionListener (this);
  reset.addActionListener (this); 
  
  //add panels to different compass points on the content area
  contentArea.add("North",buttons);
  contentArea.add("Center",plyrCards);
  contentArea.add("South",dealerCards);
  
  //Add components to panels
  buttons.add(hit);
  buttons.add(stay);
  buttons.add(reset);
  buttons.add(PlyrCardSum);
  
  for (index = 0; index < 10; index ++)
   plyrCards.add(cardLabel[index]);
  for (index = 10; index < 20; index ++)
   dealerCards.add(cardLabel[index]);
  
 }
 public void actionPerformed(ActionEvent event) 
 {
  
  if (event.getSource() == hit)
  {
   if (plyrSum < 21)
   {
    Continue = true;
    while (Continue == true) //Generates a new random number if that number has already been generated
    {
     cardID = (int) RandomNumber.GetRandomNumber(51); 
     if (cardsLeft[cardID] == true) //If a new card is drawn, continue normally
     {
      cardCounter++;
      cardsLeft[cardID] = false; //save that the card was drawn
      plyrSum += BlackJack.value(cardID); //add value to sum
      cardLabel[IDIndex].setIcon(cardImage[cardID]); //Display card
      PlyrCardSum.setText("Sum: " + plyrSum);
      IDIndex++;
      if(cardID >= 47)
       aceCounter++;
      Continue = false; //Stop the loop that draws a new card
      
     }
     
    }
    
   }
     
   if (plyrSum > 21 && aceCounter > 0) //If bust, reduce values of ace by 10 (from 11 to 1). 
   {

     plyrSum -= 10;
     aceCounter--;
   
   }
   
  }
  
  //Stay/Finish round button
  if (event.getSource() == stay)
  {
   
   aceCounter = 0;
   cardCounter = 10; //Starts at 10 so it will end up in the south JPanel
   while (dealerSum < 17)
   {
    cardID = (int) RandomNumber.GetRandomNumber(52);
    if (cardsLeft [cardID] == true)
    {
     dealerSum += BlackJack.value(cardID);
     cardsLeft [cardID] = false;
     cardLabel[cardCounter].setIcon(cardImage[cardID]); //Display card
     if(cardID >= 47)
      aceCounter++;
     cardCounter++;

     
     if (plyrSum > 21 && aceCounter > 0) //If bust, reduce values of ace by 10 (from 11 to 1). 
     {
     
      dealerSum -= 10; 
      aceCounter--;
      
     }
     
    }
          
   }
    
  }
  
  //Reset button
  if (event.getSource() == reset)
  {
   
   for (index = 0; index < 52; index++)
   {
    cardsLeft[index] = true;
   }
   
   cardCounter = 0;
   
  }
  
 }
 

给你,试试这个,我为网格布局中的图像修改了它

public class BlackJack
{   
    public static void main(String[] args) 
    {
        Interface Win = new Interface ();
    }

    public static int value(int cardID)
    //stores values for each "card ID number" 
    {

        int [] cardValue = {2, 2, 2, 2, 
                3, 3, 3, 3, 4, 4, 4, 4, 
                5, 5, 5, 5, 6, 6, 6, 6, 
                7, 7, 7, 7, 8, 8, 8, 8, 
                9, 9, 9, 9, 10, 10, 10, 10, 
                10, 10, 10, 10, 10, 10, 10, 10, 
                10, 10, 10, 10, 11, 11, 11, 11};

        return cardValue[cardID];
    }
}

class Interface extends JFrame implements ActionListener
{

    int index;
    int IDIndex = 0;

    // Add all image components here:
    ImageIcon [] cardImage = new ImageIcon[52];
    {
    for (index = 0; index <= 51; index++)
        cardImage[index] = new ImageIcon(index + ".png");

    }
    ImageIcon Empty = new ImageIcon("blank");

    //identify variables
    int cardCounter = 0;
    int dealerCardCounter = 0;
    int cardID;
    boolean Continue;

    //keeps track of cards left in the deck. 
    boolean [] cardsLeft = new boolean [52];{
    for (index = 0; index < cardsLeft.length; index++)
        cardsLeft[index] = true;
    }

    // Interface components here:
    int plyrSum = 0;
    int dealerSum = 0;
    int aceCounter = 0;
    JLabel Info = new JLabel (""); //<== this will be (setText to) either "Bust!", "Black Jack!"/"21!" or "What would you like to do?"
    JLabel PlyrCardSum = new JLabel("Sum: " + plyrSum);
    JLabel DealerCardSum = new JLabel("Dealer sum: " + dealerSum);
    JLabel winLoseLabel = new JLabel("");

    JLabel [] cardLabel = new JLabel[20];
    {
        for (index = 0; index < 20; index ++){
            cardLabel[index] = new JLabel (Empty);
        cardLabel[index].setSize(50, 50);
        }
    }

    JButton hit = new JButton ("Hit!");
    JButton stay = new JButton ("Stay");
    JButton reset = new JButton ("New Round");

    //JPanels here:
    JPanel buttons = new JPanel();
    JPanel plyrCards = new JPanel();
    JPanel dealerCards = new JPanel ();

    public Interface ()
    {

        super ("BlackJack 21");
        setSize (1000,729);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible (true);

        //GridBagConstraints card = new GridBagConstraints();

        Container contentArea = getContentPane();
        contentArea.setBackground(Color.white);

        //Event listeners
        hit.addActionListener (this);
        stay.addActionListener (this);
        reset.addActionListener (this); 

        //add panels to different compass points on the content area
        GridBagLayout grid = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        buttons.add(hit);
        buttons.add(stay);
        buttons.add(reset);
        buttons.add(PlyrCardSum);
        contentArea.setLayout(grid);
        c.fill = GridBagConstraints.HORIZONTAL;
        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 4;


        contentArea.add(buttons,c);
        GridBagLayout grid1 = new GridBagLayout();
        plyrCards.setLayout(grid1);
        for (index = 0; index < 10; index ++){
            c.fill = GridBagConstraints.HORIZONTAL;

            c.gridx = index;
            c.gridy = 1;
            c.gridwidth = 1;
            plyrCards.add(cardLabel[index],c);
        }


        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 1;
        c.gridx = 0;
        c.gridy = 1;
        c.gridwidth = 10;
        //plyrCards.setSize(new Dimension(50,50));
        contentArea.add(plyrCards,c);
        GridBagLayout grid2 = new GridBagLayout();
        dealerCards.setLayout(grid2);
        for (index = 10; index < 20; index ++){
            c.fill = GridBagConstraints.HORIZONTAL;

            c.gridx = index;
            c.gridy = 2;
            c.gridwidth = 1;
            dealerCards.add(cardLabel[index],c);
        }


        c.fill = GridBagConstraints.HORIZONTAL;
        c.ipady = 1;
        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 10;
        //plyrCards.setSize(new Dimension(50,50));
        contentArea.add(dealerCards,c);
        //contentArea.add(dealerCards,BorderLayout.SOUTH);

        //Add components to panels





    }
    public void actionPerformed(ActionEvent event) 
    {
        Random RandomNumber = new Random();
        if (event.getSource() == hit)
        {
            if (plyrSum < 21)
            {
                Continue = true;
                while (Continue == true) //Generates a new random number if that number has already been generated
                {
                    cardID = (int) RandomNumber.nextInt(51 + 1); 
                    if (cardsLeft[cardID] == true) //If a new card is drawn, continue normally
                    {
                        cardCounter++;
                        cardsLeft[cardID] = false; //save that the card was drawn
                        plyrSum += Testing.value(cardID); //add value to sum
                        Image resizedImage = cardImage[cardID].getImage();
                        resizedImage = resizedImage.getScaledInstance(100, 150, 1);
                    cardLabel[IDIndex].setIcon(new ImageIcon(resizedImage)); //Display card
                        //cardLabel[IDIndex].setIcon(cardImage[cardID]); //Display card
                        PlyrCardSum.setText("Sum: " + plyrSum);
                        IDIndex++;
                        if(cardID >= 47)
                            aceCounter++;
                        Continue = false; //Stop the loop that draws a new card

                    }

                }

            }

            if (plyrSum > 21 && aceCounter > 0) //If bust, reduce values of ace by 10 (from 11 to 1). 
            {

                    plyrSum -= 10;
                    aceCounter--;

            }

        }

        //Stay/Finish round button
        if (event.getSource() == stay)
        {

            aceCounter = 0;
            cardCounter = 10; //Starts at 10 so it will end up in the south JPanel
            while (dealerSum < 17)
            {
                cardID = (int) RandomNumber.nextInt(52);
                if (cardsLeft [cardID] == true)
                {
                    dealerSum += Testing.value(cardID);
                    cardsLeft [cardID] = false;
                    Image resizedImage = cardImage[cardID].getImage();
                        resizedImage = resizedImage.getScaledInstance(100, 150,1);
                    cardLabel[cardCounter].setIcon(new ImageIcon(resizedImage)); //Display card
                    if(cardID >= 47)
                        aceCounter++;
                    cardCounter++;


                    if (plyrSum > 21 && aceCounter > 0) //If bust, reduce values of ace by 10 (from 11 to 1). 
                    {

                        dealerSum -= 10;    
                        aceCounter--;

                    }

                }

            }

        }

        //Reset button
        if (event.getSource() == reset)
        {

            for (index = 0; index < 52; index++)
            {
                cardsLeft[index] = true;
            }

            cardCounter = 0;

        }


    }
}