0

Hello all I am trying to get a JButton to toggle the setVisible on a JPanel label with an image on it. Below is my code. Thx in advance!

import javax.swing.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ButtonExample_Extended implements ActionListener {

	JPanel titlePanel, buttonPanel, dealerPanel, playerPanel;
	JLabel dealerLabel, playerLabel, imglabel1, imglabel2, pimglabel1,
			pimglabel, pimglabel3;
	JButton hitButton, standButton, betButton;

	BlackJackv3 data = new BlackJackv3();

	public JPanel createContentPane() {

		data.dealCards();

		// We create a bottom JPanel to place everything on.
		JPanel totalGUI = new JPanel();
		totalGUI.setLayout(null);
		totalGUI.validate();

		// Creation of a Panel to contain the title labels
		titlePanel = new JPanel();
		titlePanel.setLayout(null);
		titlePanel.setLocation(10, 0);
		titlePanel.setSize(500, 500);
		totalGUI.add(titlePanel);

		dealerLabel = new JLabel("Dealer");
		dealerLabel.setLocation(45, 0);
		dealerLabel.setSize(120, 30);
		dealerLabel.setHorizontalAlignment(0);

		titlePanel.add(dealerLabel);

		playerLabel = new JLabel("Player Score: " + data.playerScore());
		playerLabel.setLocation(350, 0);
		playerLabel.setSize(120, 30);
		playerLabel.setHorizontalAlignment(0);
		titlePanel.add(playerLabel);

		// Dealer cards panel
		JPanel dealerPanel = new JPanel();
		dealerPanel.setLayout(null);
		dealerPanel.setLocation(20, 40);
		dealerPanel.setSize(250, 350);
		titlePanel.add(dealerPanel);

		// Top dealer card
		ImageIcon icon1 = new ImageIcon("images/cardback.gif");
		JLabel imglabel1 = new JLabel();
		imglabel1.setIcon(icon1);
		imglabel1.setLocation(40, 40);
		imglabel1.setSize(200, 300);
		dealerPanel.add(imglabel1);

		// Bottom dealer card
		ImageIcon icon2 = new ImageIcon("images/" + data.dImage() + ".gif");
		JLabel imglabel2 = new JLabel();
		imglabel2.setIcon(icon2);
		imglabel2.setLocation(0, 0);
		imglabel2.setSize(200, 300);
		dealerPanel.add(imglabel2);

		// Player cards panel
		JPanel playerPanel = new JPanel();
		playerPanel.setLayout(null);
		playerPanel.setLocation(330, 40);
		playerPanel.setSize(250, 350);
		titlePanel.add(playerPanel);
		
		//panels with card images
		ImageIcon picon3 = new ImageIcon("images/" + data.pImage1() + ".gif");
		JLabel pimglabel3 = new JLabel();
		pimglabel3.setIcon(picon3);
		pimglabel3.setLocation(80, 80);
		pimglabel3.setSize(200, 300);
		pimglabel3.setVisible(false);
		playerPanel.add(pimglabel3);

		ImageIcon picon1 = new ImageIcon("images/" + data.pImage1() + ".gif");
		JLabel pimglabel1 = new JLabel();
		pimglabel1.setIcon(picon1);
		pimglabel1.setLocation(40, 40);
		pimglabel1.setSize(200, 300);
		playerPanel.add(pimglabel1);

		ImageIcon picon2 = new ImageIcon("images/" + data.pImage2() + ".gif");
		JLabel pimglabel2 = new JLabel();
		pimglabel2.setIcon(picon2);
		pimglabel2.setLocation(0, 0);
		pimglabel2.setSize(200, 300);
		playerPanel.add(pimglabel2);

		// Creation of a Panel to contain buttons.
		buttonPanel = new JPanel();
		buttonPanel.setLayout(null);
		buttonPanel.setLocation(150, 420);
		buttonPanel.setSize(260, 70);
		totalGUI.add(buttonPanel);
	

		//Buttons

		hitButton = new JButton("Hit");
		hitButton.setLocation(0, 0);
		hitButton.setSize(120, 30);
		hitButton.addActionListener(this);
		buttonPanel.add(hitButton);

		standButton = new JButton("Stand");
		standButton.setLocation(130, 0);
		standButton.setSize(120, 30);
		standButton.addActionListener(this);
		buttonPanel.add(standButton);

		betButton = new JButton("Bet");
		betButton.setLocation(0, 40);
		betButton.setSize(250, 30);
		betButton.addActionListener(this);
		buttonPanel.add(betButton);

		totalGUI.setOpaque(true);
		return totalGUI;
	}

//Action
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == hitButton) {

			data.playerHit();
			playerLabel.setText("Player Score: " + data.playerScore());
		
			playerPanel.setVisible(false);
			
		} else if (e.getSource() == standButton) {

		} else if (e.getSource() == betButton) {

		}
	}
	public void setPlayer(){
		playerPanel.setVisible(false);
	}
	private static void createAndShowGUI() {

		JFrame.setDefaultLookAndFeelDecorated(true);
		JFrame frame = new JFrame("$$ BlackJackv1.2 $$");

		// Create and set up the content pane.
		ButtonExample_Extended test = new ButtonExample_Extended();
		frame.setContentPane(test.createContentPane());

		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600, 550);
		frame.setVisible(true);
	}

	public static void main(String[] args) {

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				createAndShowGUI();
			}
		});
	}
}

Edited by tipalm: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by quuba
1
Line   8: JPanel titlePanel, buttonPanel, dealerPanel, playerPanel;
Line  68: JPanel playerPanel = new JPanel();

Two declaration of playerPanel? First of them is null!

Line 136: 	playerPanel.setVisible(false);

Edited by quuba: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.