I've been trying to resize my jbuttons to the size of my pictures, but the jbuttons keep on occupying the whole frame.
The size of the photos are 150x267.

import javax.swing.*;
import java.awt.event.KeyListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
public class StartGame extends JFrame implements KeyListener {

    Timer timer;
    JPanel startpane, oakpane, choosepane;
    JButton a,b,c;
    int enter = 0;
    public StartGame()
    {
        super("Animon");
        this.setBounds(100,100,1000,1000);
        startpane = new JPanel();
        oakpane = new JPanel();
        choosepane= new JPanel();
        //start
        JLabel pic = new JLabel(new ImageIcon("animon2.png"));
        pic.setSize(400, 500);
        startpane.addKeyListener(this);
        startpane.setFocusable(true);
        startpane.add(pic);
        add(startpane);
        //oakpane
        addoakpane();

        //choosepane
        this.addKeyListener(this);
        this.setFocusable(true);
        this.setVisible(true);
    }

public void keyPressed(KeyEvent k){
    int code;
    code = k.getKeyCode();
    if(k.getSource() == this)
    {if(code == KeyEvent.VK_ENTER && startpane.isVisible()== true)
    {
        enter += 1;
        startpane.setVisible(false);        
        remove(startpane);
        repaint();
        add(oakpane);
        //new GameInterface(30,40);
    }
    else if(code == KeyEvent.VK_ESCAPE && startpane.isVisible() == false)
    {
        enter -=1;
        startpane.setVisible(true);
        add(startpane);
        repaint();
    }
    }
    }
public void keyReleased(KeyEvent k){

}
public void keyTyped(KeyEvent k){

}


public void addoakpane(){
    oakpane.setLayout(new GridLayout(1,3));
    //oakpane.setBounds(0, 0, 450, 267);
    a = new JButton(new ImageIcon("charopt.png"));
    a.setSize(150, 267);
    oakpane.add(a);
    b = new JButton(new ImageIcon("leaf.png"));
    b.setSize(150, 267);
    oakpane.add(b);
    c = new JButton(new ImageIcon("squiropt.png"));
    c.setSize(150, 267);
    oakpane.add(c);

}
}

Dani AI

Generated

Your buttons are being stretched by GridLayout. In Swing, a component’s setSize(...) is ignored when a layout manager is in control. A JButton created with an ImageIcon already reports a preferred size equal to the icon plus its insets; layouts like FlowLayout or BoxLayout will respect that, while GridLayout forces all children to fill equal cells.

Example: switch the container that holds your three image buttons to a layout that honors preferred sizes, and let the frame compute its own size with pack().

JPanel oakpane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));

JButton a = new JButton(new ImageIcon("img1.png"));
JButton b = new JButton(new ImageIcon("img2.png"));
JButton c = new JButton(new ImageIcon("img3.png"));

oakpane.add(a);
oakpane.add(b);
oakpane.add(c);

JFrame f = new JFrame("Animon");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setContentPane(oakpane);
f.pack();                      // sizes frame to buttons/icons
f.setLocationByPlatform(true);
f.setVisible(true);

Tips:

  • Do not call setBounds(...) on the frame; prefer pack() so layout and icon sizes drive the UI.
  • If the buttons are slightly larger than the images, reduce margins/borders: button.setMargin(new Insets(0,0,0,0)); or use a border that suits your design.
  • For swapping between screens (start, oak, choose), CardLayout simplifies show/hide without remove/add glitches. See CardLayout and the overview on Using Layout Managers. For button behavior and insets, see How to Use Buttons.

Recommended Answers

All 2 Replies

I always stuck in these situation about resizing buttons. so i will suggest you my solutions and if you find a better one please share:

1)the BoxLayout, instead of using gridLayout at line 67 you can use this oakpane.setLayout( new BoxLayout(oakpane, BoxLayout.X_AXIS) );

2)a FlowLayout panel into the oakpane, create a new panel, add the button to it and then add the panel to oakpane.

JPanel pa = new JPanel();
a = new JButton(new ImageIcon("charopt.png"));
a.setSize(150, 267);
pa.add(a);
oakpane.add(pa);

I hope this help.

A JButton with an ImageIcon will automatically set its own preferred size to fit the icon. The problem comes when you use a layout manager that re-sizes the buttons to achieve the desired layout. You need to chose layout managers for your frame and panels that don't resize the buttons. Have a look at http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.