0

JButtons had the default size and I can't change it. I was try with setSize and it don't do anything. When I click on some of JButtons picture will be set and the JButtons will get size of the picture. I want to set size of JButton to be the same like the size of JButton when I click on it(JButton with picture)

    btn=new JButton[9];
    j=0;

    for (i = 0; i <btn.length; i++) {
        btn[i] = new JButton("");
        btn[i].addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(j%2==0){
                    ((JButton) e.getSource()).setIcon(new ImageIcon("resources/X.png"));
                }else{
                    ((JButton) e.getSource()).setIcon(new ImageIcon("resources/O.png"));
                }
                ((JButton) e.getSource()).setEnabled(false);
                j++;
            }
        });

    }

GridBagConstraints gbc=new GridBagConstraints();

gbc.gridx=0;
gbc.gridy=0;
p2.add(btn[0],gbc);

gbc.gridx=1;
gbc.gridy=0;
p2.add(btn[1],gbc);

gbc.gridx=2;
gbc.gridy=0;
p2.add(btn[2],gbc);

 .........

enter image description here enter image description here

GlacialMan
  • 579
  • 2
  • 5
  • 20
  • 1
    You could: Use a blank image; use `fill` and `weightx/y` constraints and/or `ipadx/y` constraints; use a `GridLayout` – MadProgrammer Apr 16 '16 at 11:57
  • setPreferredSize work! – GlacialMan Apr 16 '16 at 12:32
  • @GlacialMan `setPreferredSize` is a bad idea and represents a habit which will cause you no end of issues, learn to live without it. [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/questions/7229226/should-i-avoid-the-use-of-setpreferredmaximumminimumsize-methods-in-java-swi) – MadProgrammer Apr 16 '16 at 21:24

1 Answers1

3

Probably the simplest and most reliable solution is to use a blank image which is the same size as the others as the initial image for the button

Very few layout managers allow you to directly suggest the size of a given component, in fact, the intention is to allow the component to tell the layout manager what it wants and then let the layout manager figure out if it can accomidate it.

For example...

GridBagLayout

public class TestPane extends JPanel {

    public TestPane() {
        setLayout(new GridBagLayout());
        BufferedImage img = new BufferedImage(32, 32, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setBackground(new Color(255, 255, 255, 0));
        g2d.clearRect(0, 0, 32, 32);
        g2d.dispose();
        GridBagConstraints gbc = new GridBagConstraints();
        for (int row = 0; row < 3; row++) {
            gbc.gridy = row;
            for (int col = 0; col < 3; col++) {
                gbc.gridx = col;
                add(new JButton(new ImageIcon(img)), gbc);
            }
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

}

In this example I've created my own blank image, you could do the same, but it's just as easy to load blank image, the concept is the same

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yeah i thought about that but later i will check does the picture are the same so i will get always answer that the picture are the same – GlacialMan Apr 16 '16 at 12:14
  • @GlacialMan `setPreferredSize` is one of those insidious functions that "look" like they solve your issue, but which simple continue to make your life more difficult in the long run, learn to live without. Instead of checking the image state, you should have a virtual model of the game which contains the actual game state and which the buttons are just a visual representation of – MadProgrammer Apr 16 '16 at 21:23