vie7now Posted June 7, 2004 Share Posted June 7, 2004 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 More sharing options...
0 kjordan2001 Posted June 7, 2004 Share Posted June 7, 2004 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 More sharing options...
0 vie7now Posted June 7, 2004 Author Share Posted June 7, 2004 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 More sharing options...
0 kjordan2001 Posted June 8, 2004 Share Posted June 8, 2004 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 More sharing options...
0 vie7now Posted June 8, 2004 Author Share Posted June 8, 2004 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 More sharing options...
0 kjordan2001 Posted June 8, 2004 Share Posted June 8, 2004 Still interesting that one doesn't work for you. What Java version are you using? Link to comment Share on other sites More sharing options...
0 vie7now Posted June 8, 2004 Author Share Posted June 8, 2004 SDK 1.4.2. The IDE is NetBeans. -0 Link to comment Share on other sites More sharing options...
0 SIG Posted June 8, 2004 Share Posted June 8, 2004 // 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 More sharing options...
Question
vie7now
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