Java网格布局:南JPanel覆盖中心JPanel



我目前正在开发我的第一个真正的blackJack程序。它快完成了,但我遇到了一个大问题。

当我运行代码时,它会按计划在中央JPanel中显示玩家卡。但一旦我完成并按下结束转弯,就会只显示经销商卡,而不是同时显示两者。在调整我的卡片图像的大小使其变小之前,它们会显示为又高又窄的条带(只显示卡片中间的一条带,而不是整个东西),从上到下覆盖整个屏幕。

我相信,一旦我结束转弯,卡片图像被添加到南JPanel,它就会最小化中心JPanel(假设是因为组件太高了),并最终占据所有空间。虽然我不确定是不是这样,但这只是猜测。

即使我把图像缩小了,尽管现在整张卡都出现了,但经销商的卡仍然在取代屏幕上的玩家卡。所以我假设我需要以某种方式调整为图像分配的实际空间的大小?

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

所以,是的,任何帮助或见解都将不胜感激!事先谢谢。

如果你想用卡片,我做了两个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;
        }

    }
}

最新更新