Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some problems in your code : </p> <p>Firstly, if using <code>frame.setLayout(new FlowLayout(FlowLayout.LEFT));</code>(<strong>FlowLayout</strong>) then here<code>frame.getContentPane().add(toolBar.createToolBar(), BorderLayout.NORTH);</code> why are you specifying <code>BorderLayout.NORTH</code> which has no <strong>effect</strong> </p> <p>again case with <code>frame.getContentPane().add(LabelCreator.createLabel(frame, "Java Swing", "purple"),BorderLayout.NORTH);</code></p> <p>EDIT :<br> For your <code>contentpane</code> problem, firstly looking at following code(demo) :</p> <pre><code>JFrame frame= new JFrame(); frame.getContentPane().setBackground(Color.red); frame.getContentPane().setPreferredSize(new Dimension(width, height)); frame.pack(); frame.setVisible(true); </code></pre> <p>When you pass <strong>different</strong> values for <code>width</code> and <code>height</code> you will see <strong>whole size</strong> of <code>frame</code> will get effected as i have not specified <code>setSize</code> anywhere, the reason for this is </p> <ol> <li><p>To appear onscreen, every GUI component must be part of a containment hierarchy. A containment hierarchy is a tree of components that has a top-level container as its root. We'll show you one in a bit. </p></li> <li><p>Each GUI component can be contained only once. If a component is already in a container and you try to add it to another container, the component will be removed from the first container and then added to the second.</p></li> <li><p>Each top-level container has a <strong>content pane</strong> that, generally speaking, contains (directly or indirectly) the visible components in that top-level container's GUI.</p></li> <li><p>You can optionally add a menu bar to a top-level container. The menu bar is by convention positioned within the top-level container, but outside the content pane. Some look and feels, such as the Mac OS look and feel, give you the option of placing the menu bar in another place more appropriate for the look and feel, such as at the top of the screen.</p></li> </ol> <p>So the <strong>conclusion</strong> is that changing <code>contentpane</code> size will change <code>frame</code> size.</p> <p>As your <strong>statement</strong> <code>frame.setContentPane(ContentPaneCreator.createContentPane("darkseagreen", 800, 255));</code> is making the <code>panel</code> as <code>contentpane</code>, so again <strong>changing</strong> its size is to change <code>frame</code> <strong>size</strong></p> <p>But the other way around is :</p> <pre><code>frame.getContentPane().add(ContentPaneCreator.createContentPane("darkseagreen", 800, 255)); </code></pre> <p><code>add</code> a <code>container</code> to <code>jframe</code> and then <code>add</code> your <strong>components</strong> to that <code>container</code>( jpanel in your case )</p> <p>Hope this will help and <strong>one thing</strong> more i also used to code like you creating <code>methods</code> and <code>classes</code> for every <strong>little purpose</strong>, but sometimes they raise <strong>complexities</strong> instead of <strong>simplicity</strong>, so my suggestion to you is to <strong>follow a design pattern</strong> like <code>MVC</code> or anyother you like</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload