Results 1 to 3 of 3
  1. #1
    FermataPlays is offline Member
    Join Date
    Mar 2015
    Rep Power

    Default Why isn't setSize(x,y) working?

    Java Code:
    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.InputEvent;     
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    // Imports all needed packages for Swing and AWT.
    public class SwingExample {
    	public static void main(String args[]) {
    		JFrame frame = new JFrame("Swing Program");
    		// Creates frame and sets it to exit when closed.
    		JButton button = new JButton("Click Me!");
    		JLabel label = new JLabel("You clicked the button!");
    		// Creates button and (invisible) label. Label will appear when button is clicked.
    		ActionListener buttonListener = new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    		// Creates ActionListener that will show the label.
    		// Adds buttonListener to button. Button, when clicked, will display label.
    		frame.setSize(200, 100);
    		// Sets layout and size of frame. Adds button and label to frame and displays.
    Code compiles fine without any errors. However I get a window at this size:
    Why isn't setSize(x,y) working?-setsize-.png

    EDIT: Also as I re-size the window, there isn't anything in it.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Why isn't setSize(x,y) working?

    The default LayoutManager for JFrame is BorderLayout. And without specifying a location, you are placing the button and then the label in the center of the frame. Best method is to have your class extend JPanel or just create a JPanel and add that to the JFrame. Then simply add the components to the JPanel. JPanels default layout is FlowLayout.

    Note: In the first example, you could do the following.

    Java Code:
    frame.add(button, BorderLayout.NORTH);
    frame.add(label, BorderLayout.SOUTH);
    Recommend you read up on Layout Managers. Check out the tutorial link in my signature.

    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Why isn't setSize(x,y) working?

    Quote Originally Posted by FermataPlays View Post
    Java Code:
    		// Creates button and (invisible) label. Label will appear when button is clicked.
    And when you go through those tutorials, take a close look at CardLayout.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Japplet setSize()
    By ninjaturtlez in forum Java Applets
    Replies: 6
    Last Post: 12-22-2011, 01:32 AM
  2. setSize without frames
    By Javaer in forum New To Java
    Replies: 1
    Last Post: 01-09-2011, 02:41 AM
  3. setSize on BoxLayout
    By smray7 in forum New To Java
    Replies: 7
    Last Post: 12-15-2010, 11:18 PM
  4. setSize() not working?
    By AedonetLIRA in forum Java Applets
    Replies: 3
    Last Post: 12-02-2010, 05:17 AM
  5. When to use setSize()
    By Lil_Aziz1 in forum New To Java
    Replies: 7
    Last Post: 05-29-2010, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts