Hey there,
So i need to create a matching pairs game. So far i've managed to create the table and all the cards. I though using a GridLayout would be suitable so it looks like the cards have been dealt across the table. The only problem is for some reason the cards are no longer the size i specified setSize(150, 200). So the button is no longer the same size as the image contained within in. I'm going to assume it's something to do with GridLayout? As it was working fine before this, but despite my best efforts I can't seem to find what i need to do to fix it. Any help would be much appretiated. Here's my code.
import javax.swing.*; import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ItemListener; import java.awt.event.ItemEvent; public class Cards implements ItemListener{ // Definition of global values and items that are part of the GUI. JToggleButton one, two, three, four, oneDup, twoDup, threeDup, fourDup; JPanel table; ImageIcon backImg = new ImageIcon("back.jpg"); ImageIcon oneImg = new ImageIcon("one.jpg"); ImageIcon twoImg = new ImageIcon("two.jpg"); ImageIcon threeImg = new ImageIcon("three.jpg"); ImageIcon fourImg = new ImageIcon("four.jpg"); public JPanel createContentPane (){ // JPanel to place everything on. table = new JPanel(); table.setBackground(Color.green); GridLayout dealer = new GridLayout(4, 2, 10, 10); table.setLayout(dealer); one = new JToggleButton(backImg); // Create cards (JToggleButtons) one.setSize(150,200); one.addItemListener(this); table.add(one); two = new JToggleButton(backImg); two.setSize(150,200); two.addItemListener(this); table.add(two); three = new JToggleButton(backImg); three.setSize(150,200); three.addItemListener(this); table.add(three); four = new JToggleButton(backImg); four.setSize(150,200); four.addItemListener(this); table.add(four); oneDup = new JToggleButton(backImg); oneDup.setSize(150,200); oneDup.addItemListener(this); table.add(oneDup); twoDup = new JToggleButton(backImg); twoDup.setSize(150,200); twoDup.addItemListener(this); table.add(twoDup); threeDup = new JToggleButton(backImg); threeDup.setSize(150,200); threeDup.addItemListener(this); table.add(threeDup); fourDup = new JToggleButton(backImg); fourDup.setSize(150,200); fourDup.addItemListener(this); table.add(fourDup); table.setOpaque(true); return table; } public void itemStateChanged(ItemEvent e) { // Flip card over when pressed if(e.getStateChange() == ItemEvent.SELECTED) { one.setSelectedIcon(oneImg); two.setSelectedIcon(twoImg); three.setSelectedIcon(threeImg); four.setSelectedIcon(fourImg); oneDup.setSelectedIcon(oneImg); twoDup.setSelectedIcon(twoImg); threeDup.setSelectedIcon(threeImg); fourDup.setSelectedIcon(fourImg); } else { one.setSelectedIcon(backImg); } } private static void createAndShowGUI() { // Set up JFrame , will be removed when other JPanels added JFrame.setDefaultLookAndFeelDecorated(true); JFrame frame = new JFrame("[=] JToggleButton [=]"); //Create and set up the content pane. Cards demo = new Cards(); frame.setContentPane(demo.createContentPane()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(1400, 700); frame.setVisible(true); } public static void main(String[] args) { //Schedule a job for the event-dispatching thread: //creating and showing this application's GUI. SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }