please someone tell me how to change the size of the button.the code:

JButton button1 = new JButton();
    JButton button1 = new JButton();

    public Mainmenu() {
        setLayout(new FlowLayout(100,278,158));

        Icon button2= new ImageIcon(getClass().getResource("button1.jpg"));
        button2=new JButton(button2);
        add(button);

        Icon button2= new ImageIcon(getClass().getResource("button2.jpg"));
        button2=new JButton(button2);
        add(button2);

        HandlerC hand = new HandlerC();
        button1.addActionListener(hand);
        button2.addActionListener(hand);

    }       
    private class HandlerC implements ActionListener {
        public void actionPerformed(ActionEvent e) {            

        }
    }
    }

Recommended Answers

All 4 Replies

try using the setPreferredSize method.

What size do you want it to be? Eg If you have multiple buttons do you want them all to be the same size?
setPrefferedSize is a last resort really ,because you set the size in pixels the when someone runs yur code on a retina display it looks really silly...

You can set the size of your button like this:

this.setlayoutManager(null); //Where the button is placed on a panel / frame / ...
Button but = new Button("Button");
but.setBounds(x,y,w,l); //xposition / uposition / width /length
this.add(but);

Using a null layout manager is a really bad idea unless you are coding for some very specific hardware configuration and the code will never run run on (eg) a Mac retina display. Pixel sizes are very different from one machine ot the next, so they are a near-usless way of specifying layout that has any text anywhere in it.

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.