java - Difference between creating an object within the constructor vs outside of the constructor? -


within program, trying create toolbar within frame. within toolbar, have 3 buttons represented picture instead of text.

the problem have found there difference in how buttons displayed if create jbutton objects within constructor, compared if did outside of constructor (but still within jframe class).

my code when create buttons within constructor :

public class tool extends jframe {     public tool()     {          jtoolbar bar = new jtoolbar();           jbutton button1 = new jbutton(img1);          jbutton button2 = new jbutton(img2);          jbutton button3 = new jbutton(img3);           bar.add(button1);          bar.add(button2);          bar.add(button3);     } } 

then buttons added nicely , neatly toolbar.

however, if this:

public class tool extends jframe {      jbutton button1 = new jbutton(img1);      jbutton button2 = new jbutton(img2);      jbutton button3 = new jbutton(img3);   public tool()         {              jtoolbar bar = new jtoolbar();               bar.add(button1);              bar.add(button2);              bar.add(button3);         }  } 

then, buttons still added toolbar. instead of being formatted nicely, seem have border around them (similar if copied image off of google , paste onto powerpoint presentation, example, , square border around image).

why case? why matter create jbutton objects?

thank in advance.

edit (complete correct code): in code below, button1 , button2 created within constructor, whereas button3 created outside of constructor. can see, there faint white border around button text "java", compared 2 other buttons.

import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class tool extends jframe {         jbutton button3 = new jbutton("java");       public tool()     {         super("tool");         setlookandfeel();         setsize(370, 200);         setdefaultcloseoperation(jframe.exit_on_close);          jbutton button1 = new jbutton("help");         jbutton button2 = new jbutton("sos");          //build toolbar         jtoolbar bar = new jtoolbar();         bar.add(button1);         bar.add(button2);         bar.add(button3);          // build text area         jtextarea edit = new jtextarea(8, 40);         jscrollpane scroll = new jscrollpane(edit);          // create frame         borderlayout border = new borderlayout();         setlayout(border);         add("north", bar);         add("center", scroll);         setvisible(true);     }      private void setlookandfeel()     {         try         {             uimanager.setlookandfeel("com.sun.java.swing.plaf.nimbus.nimbuslookandfeel");         }         catch(exception e)         {          }     }      public static void main(string[] arguments)     {         tool loot = new tool();     } } 

in first case, have 3 local variables declared in constructor.

in second case, tool class has 3 fields. can refer fields in other methods, , part of state of object.

that's significant difference in itself, shouldn't affect behaviour in itself. however, also affects timing of when jbutton instances created - when they're fields, initializer being executed before call setlookandfeel , before call setsize.

after experimenting bit, seems it's , feel important here. suggest change code make setlookandfeel static method, , call main before create any gui components. you'll consistent experience. (i suggest catching unsupportedlookandfeelexception , reflectiveoperationexception, , at least logging exception, instead of continuing without trace of what's wrong, too...)


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -