• 0

[Java] Resizing Buttons


Question

I am new to Java so you'll be seeing more of these threads. I'll try to put all my questions in one thread.

How can I resize a button in Java?

This is JButton I am using. The obvious thought would be the 'setSize' method but appearently it's not valid of JButton.

Thank you.

-0

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

jbutton.setSize(width,height) or jbutton.setSize(Dimension d) or you can use setBounds(x,y,height,width) or setBounds(Rectangle r). Make sure you did an "import javax.swing.*;" too.

Link to comment
Share on other sites

  • 0

package Test;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {

    public Test()
    {
        setSize (450, 200);
        setTitle ("Test");
        
        Container contMain = getContentPane();
        contMain.setLayout (new FlowLayout());

        JButton jb = new JButton (".");
        
        contMain.add (jb);
        
    }
    
}

That is what I have at the moment. Neither setSize nor setBounds is working. No errors are shown during compilation. The button is neither expanding nor contracting.

-0

Link to comment
Share on other sites

  • 0

1) You're not setting your frame visible. e.g. somewhere put setVisible(true).

2) I don't see a button setSize there, which you said was your problem.

The following code works for me:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Test extends JFrame {
   public Test()
   {
       setSize (450, 200);
       setTitle ("Test");

       Container contMain = getContentPane();
       contMain.setLayout (new FlowLayout());

       JButton jb = new JButton (".");
       jb.setSize(100,100);
       contMain.add (jb);
       this.setVisible(true);
   }
}

Link to comment
Share on other sites

  • 0
1) You're not setting your frame visible. e.g. somewhere put setVisible(true).

That is just the "blueprint" class as I've been told. I do have a driver/demo class (the one with the public static void main...). That class calls this one and also sets the frame visibility to true.

That still doesn't work for me. Still no errors are shown; it just doesn't work.

Anyways, I found out the syntax.

jb.setPreferredSize(new Dimension (50, 50));

Thanks for your replies though Kjordan2001 :)

-0

Link to comment
Share on other sites

  • 0

// Replace
contMain.setLayout (new FlowLayout());

// By this
contMain.setLayout (null);

See if this works for you :)

ps: told the same thing in two topics now! :p Restecpa!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.