Results 1 to 12 of 12
Like Tree3Likes
  • 1 Post By jim829
  • 1 Post By jim829
  • 1 Post By DarrylBurke

Thread: HELP! Can't create a simple window (Code and Picutre included)

  1. #1
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default HELP! Can't create a simple window (Code and Picutre included)

    I can't get my Java Program Window to look like I want. If I use AbsoluteLayout I can design the window perfectly but then it won't finish loading the Window it's just in my Linux taskbar. I am including a picture where I set background colours to make it easy to see what I want. Whatever solution I use I need access to JTextArea = messagesArea and JLabal = lblStatusMsg for my program. Last note; please understand I chopped this out of my program to make it simple, if it is broken I broke it right now. If I use any layout besides absolute my program launches, but of course without the layout I want.

    (I am not using buttons with this program it already knows what to do)


    Java Code:
    import java.awt.Color;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JLabel;
    
    public class MainWindow {
    
    	private JFrame serFrame = new JFrame();
    	private JTextArea messagesArea = new JTextArea();
    	private JLabel lblStat = new JLabel("Status:");
    	private JLabel lblStatMsg = new JLabel("Waiting for connection");
    
    	public MainWindow() {
    		serFrame.getContentPane().setLayout(null);
    		serFrame.getContentPane().setBackground(Color.RED);
    		serFrame.setBackground(Color.GRAY);
    		serFrame.setTitle("Echo to Tablet \"Server\"");
    		
    		messagesArea = new JTextArea();
    		messagesArea.setBounds(12, 0, 416, 239);
    		serFrame.getContentPane().add(messagesArea);
    		
    		lblStat = new JLabel("Status:");
    		lblStat.setBounds(12, 251, 70, 15);
    		serFrame.getContentPane().add(lblStat);
    		
    		lblStatMsg = new JLabel("Waiting for connection!");
    		lblStatMsg.setBounds(94, 251, 323, 15);
    		serFrame.getContentPane().add(lblStatMsg);
    		serFrame.pack();
    //		serFrame.setVisible(true);
     
    }
    
    		public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                	MainWindow MyWin = new MainWindow();
                	MyWin.lblStatMsg.setText("java-forums.org connected!");
                	MyWin.serFrame.setVisible(true);
                	
                    System.out.println("Test");
                }
            });
    	}
    }
    Attached Thumbnails Attached Thumbnails HELP! Can't create a simple window (Code and Picutre included)-mywindow.jpg  
    Last edited by Gotcha; 06-14-2015 at 10:07 PM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    Please place you code between code tags. BB Code List - Java Programming Forum - Learn Java Programming.

    How is it supposed to appear? And a complete (but short) working program would be useful if you want some assistance.

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

  3. #3
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    Quote Originally Posted by jim829 View Post
    Please place you code between code tags. BB Code List - Java Programming Forum - Learn Java Programming.

    How is it supposed to appear? And a complete (but short) working program would be useful if you want some assistance.

    Regards,
    Jim
    Look it is quite obvious I am not going to be offered help here. I have no faith I can write Java programs it is not a hard language it is just not very well explained anywhere I've seen..
    Last edited by Gotcha; 06-15-2015 at 12:32 AM.

  4. #4
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Need help building a simple JFrame window (Picture included)

    I created the post under Swing but it is a newbie question. I have not cross posted to any other forum. It is basically a question on layout
    http://www.java-forums.org/awt-swing...-included.html

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    You said the following:

    If I use AbsoluteLayout I can design the window perfectly but then it won't finish loading the Window
    That sounds like a function problem. And it might prove useful for you to know why it isn't loading, even if you get a layout manager solution.

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

  6. #6
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    Quote Originally Posted by jim829 View Post
    You said the following:



    That sounds like a function problem. And it might prove useful for you to know why it isn't loading, even if you get a layout manager solution.

    Regards,
    Jim
    That's great Jim because the code is above. If only I had one person to tell me why? I think I am not understanding program execution flow and the object isn't fully created one I try to use it. I am initializing a large section of the form in the constructor. I purposely made that code to get help on but I guess it is not what you wanted to help with today.. I would actually prefer no responses to completely unhelpful challenging ones.

  7. #7
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    I believe what you need to do is to combine multiple BoxLayout managers. I did one with two JPanels. The first is the main panel and the second is the status panel. The main panel holds the text area and the status panel using a Y-AXIS constraint. The status panel uses an X-AXIS constraint which holds the status label and status message. You can give your text area a default value of rows and columns when you create it. By using layout managers, you can resize the frame and everything remains in tact.

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

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    Quote Originally Posted by Gotcha View Post
    That's great Jim because the code is above. If only I had one person to tell me why? I think I am not understanding program execution flow and the object isn't fully created one I try to use it. I am initializing a large section of the form in the constructor. I purposely made that code to get help on but I guess it is not what you wanted to help with today.. I would actually prefer no responses to completely unhelpful challenging ones.
    You didn't explain what it was that you meant by not loading which is why I wanted to see your earlier attempt. You program above compiles and produces the image that you attached. So there is not much I can do with it. But since I am not providing the kind of help you are seeking then perhaps someone else can do better.

    Regards,
    Jim
    Gotcha likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    Ok Jim I am sorry I lost my cool. So it does load on your machine, that was amazingly helpful.. I must have a problem with my environment...

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    Actually it appears in the upper left corner of my screen. It has to be resized but it looks like your image. To center it, user serFrame.setLocationRelativeTo(null);

    If you don't want to resize the frame, then do a setPreferredSize(). But I recommend using layout managers as I discussed in my other post.

    Regards,
    Jim
    Gotcha likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    Gotcha is offline Member
    Join Date
    Jun 2015
    Location
    America
    Posts
    29
    Rep Power
    0

    Default Re: HELP! Can't create a simple window (Code and Picutre included)

    The code was indeed broken but I got it now. I figured out why it wasn't working..
    Java Code:
    import java.awt.Color;
    import java.awt.EventQueue;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import javax.swing.JLabel;
    
    //I am using setbounds to position components on a null layout.. FINALLY!!!!!
    public class MainWindow {
    	//Notice I am not extending the Frames class
        private JFrame serFrame = new JFrame();
    
        private JTextArea messagesArea = new JTextArea();
        private JLabel lblStat = new JLabel("Status:");
        private JLabel lblStatMsg = new JLabel("Waiting for connection");
        
        //Constructor 
        public MainWindow() {
        	//Absolute Positioning
        	serFrame.setLayout(null);
        	/** Notice I am using just JFrame and adding components */
        	
        	//JTextArea being added
        	messagesArea = new JTextArea();
            messagesArea.setBounds(12, 0, 416, 239);
        	serFrame.add(messagesArea); //<===== See me adding JTextArea?
        	
            //JPanel 1 of 2 being added 
            lblStat = new JLabel("Status:");
            lblStat.setBounds(12, 251, 70, 15);
            serFrame.add(lblStat); //<===== See me adding JLabel?
            
            //JPanel 2 of 2 being added
            lblStatMsg = new JLabel("Waiting for connection!");
            lblStatMsg.setBounds(94, 251, 323, 15);
            serFrame.add(lblStatMsg); //<===== See me add the last JLanel?
            
            //Setting the specifics of my JFrame; title, color, size, etc...
            serFrame.setSize(400, 350);
            serFrame.setTitle("Java Forums is the best Java forum");
            serFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            serFrame.setLocationRelativeTo(null);
            serFrame.getContentPane().setBackground(Color.RED);
            serFrame.setBackground(Color.GRAY);
      
    }
            public static void main(String[] args) {
            //Set this up in the event queue, proper
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    MainWindow MyWin = new MainWindow();
                    MyWin.lblStatMsg.setText("java-forums.org connected!");
                    MyWin.serFrame.setVisible(true);
                     
                    System.out.println("Test");
                }
            });
        }
    }
    P.S. I am learning other layouts Jim but I was challenged by this one and I have this thing where I must finish things. Next up for me is learning insets actually so I am still fighting this fight.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Need help building a simple JFrame window (Picture included)

    Quote Originally Posted by Gotcha View Post
    I created the post under Swing but it is a newbie question. I have not cross posted to any other forum. It is basically a question on layout
    http://www.java-forums.org/awt-swing...-included.html
    I moved this post from a new thread started in New to Java.

    Please learn and respect the Forum Rules, specifically the second paragraph.

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

Similar Threads

  1. Help me with this bug (simple, code included)?
    By Vorpike in forum New To Java
    Replies: 6
    Last Post: 06-18-2013, 05:04 AM
  2. Simple GUI window
    By saba1998 in forum AWT / Swing
    Replies: 6
    Last Post: 11-07-2012, 01:35 PM
  3. How to create a JFrame window and create a circle.
    By tehsumo in forum AWT / Swing
    Replies: 3
    Last Post: 02-27-2012, 04:06 PM
  4. Replies: 1
    Last Post: 08-03-2011, 01:30 PM
  5. JDBC code not included into JAR file??
    By friday in forum Advanced Java
    Replies: 1
    Last Post: 12-17-2010, 07:13 PM

Tags for this Thread

Posting Permissions

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