3

I have this code

package com.net.Forms;
import javax.swing.JButton;
import javax.swing.JFrame;

public class MainForm {

    protected static JFrame window = new JFrame("Test Form");
    protected static JButton btnOK = new JButton("OK!");

    public static void Main() {
        load();
        return;
    }
        public static void load() {
        window.setSize(500, 500);
        window.setVisible(true);
        //btnOK.setSize(50, 50); //here
        window.add(btnOK);
        btnOK.setEnabled(true);
        btnOK.setVisible(true);


        }

}

Why is the button still filling the frame instead of being 50 X 50 like i stated above

Any help would be appreciated

06needhamt
  • 1,555
  • 2
  • 19
  • 38
  • 1
    Read up on [`BorderLayout`](http://docs.oracle.com/javase/tutorial/uiswing/layout/border.html). That is the default layout manager for `JFrame`s. – mre Apr 29 '13 at 17:25
  • See http://stackoverflow.com/questions/5714214/set-size-wont-work-in-java – PM 77-1 Apr 29 '13 at 17:47

2 Answers2

5

The default Layout for JFrame is BorderLayout. That's why when you are adding a JButton to it , It is adding the JButton to the center and expanding it to cover entire window. BorderLayout doesn't respect the setSize(..) method of components being added to them. If you still want to give a preferred size to the component being added to JFrame you should change the layout to be FlowLayout or GridLayout or others.. and then use setPreferredSize(..) method with the component while adding it to the JFrame. For example Your code could be modified in following way.

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

public class MainForm {

    protected  JFrame window = new JFrame("Test Form");
    protected  JButton btnOK = new JButton("OK!");

    public static void main(String st[]) {
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                MainForm mf = new MainForm();
                mf.load();
            }
        });

    }
    public void load() {
    Container c = window.getContentPane();
    c.setLayout(new FlowLayout());//Set layout to be FlowLayout explicitly.
    btnOK.setPreferredSize(new Dimension(100,50));//use set PreferredSize
    c.add(btnOK);
    c.setSize(500, 500);
    c.setVisible(true);
    }

}
Vishal K
  • 12,976
  • 2
  • 27
  • 38
  • 1
    [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing](http://stackoverflow.com/q/7229226/230513)? – trashgod Apr 29 '13 at 18:36
  • @trashgod: So what you suggest in this case ? – Vishal K Apr 29 '13 at 18:40
  • 1
    Better to `pack()` the enclosing `Window` in order to _rely_ on the UI delegate's calculation. If you override `getPreferredSize()`, make the bounds no smaller to avoid [this](http://stackoverflow.com/a/12532237/230513). – trashgod Apr 29 '13 at 18:44
  • So what you suggest in this case ? 1. create public method for GUI, to avoiding whatever inside main() 2. calling new MainForm() should be inside invokeLater. 3. all GUI creations in public MainForm() { should be ended window.setVisible(true);, 4. change ordering of code lines, 5. please don't put Xxx to JFrame, put there JPanel for this job 6. could be find out there, other 3-5 non_critical issues – mKorbel Apr 29 '13 at 19:17
  • @mKorbel Yeah I did that. But I had asked trashgod to suggest a way to achieve what OP wants without using `setPreferredSize`. – Vishal K Apr 29 '13 at 19:27
-2

Its just a bug or sth. the last element added to frame Takes Whole of it.

all you need to do :
declare a new simple component like JLabel()
add it to frame.
Dont set Bounds Or Size For it.just a new Label.
MAKE SURE that this label is the last element added to frame.
Hope it Works.

AbbasEbadian
  • 653
  • 5
  • 15
  • Welcome to SO! Please take a look at the [How To Answer](https://stackoverflow.com/help/how-to-answer) page. There is already a very detailed answer to this question that was accepted 4+ years ago. Your answer unfortunately does not provide any additional information that would help OP or the public researching this issue. – JNYRanger Jul 21 '17 at 14:11