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);

}
}

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.