6 Replies - 436 Views - Last Post: 03 April 2012 - 02:16 PM Rate Topic: -----

#1 ntski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-April 12

Simple GUI program isn't running

Posted 03 April 2012 - 01:49 PM

Hello.

I'm supposed to be making a simple GUI program for my CSCI class and I pieced together this, yet it's not running correctly; nothing is opening.

Here is my code:

public class GUIassignment extends JFrame
{
    public GUIassignment()
    {
    JPanel p1 = new JPanel();
    
    p1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 10));
    
    p1.add(new JButton("Button 1"));
    p1.add(new JButton("Button 2"));
    p1.add(new JButton("Button 3"));
    
    JPanel p2 = new JPanel();
    
    p2.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 10));
    
    p2.add(new JButton("Button 4"));
    p2.add(new JButton("Button 5"));
    p2.add(new JButton("Button 6"));
    
    Container gui = getContentPane();
    gui.add(p1, BorderLayout.NORTH);
    gui.add(p2, BorderLayout.SOUTH);
    }

    public static void main(String[] args) 
    {
        GUIassignment gui = new GUIassignment();
        
    
    }
} 



Is This A Good Question/Topic? 0
  • +

Replies To: Simple GUI program isn't running

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Simple GUI program isn't running

Posted 03 April 2012 - 01:52 PM

gui.setVisible(true);

Was This Post Helpful? 0
  • +
  • -

#3 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Simple GUI program isn't running

Posted 03 April 2012 - 01:56 PM

Or since you're making your JFrame visible, you simply need:

setVisible( true );

After that, you'll find it all scrunched up in the top left corner of you screen, so expand it to see what you've made.

Edit:

To fix the scrunching, add

pack();

before setting it visible.

This post has been edited by GregBrannon: 03 April 2012 - 02:01 PM

Was This Post Helpful? 0
  • +
  • -

#4 ntski  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 03-April 12

Re: Simple GUI program isn't running

Posted 03 April 2012 - 02:02 PM

Oddly enough, a window is still not opening when I run the program.
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: Simple GUI program isn't running

Posted 03 April 2012 - 02:03 PM

It may be too small or obscured for you to see. Add the pack() statement, and that may make it more obvious. It's there . . . or at least it's visible here.
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Simple GUI program isn't running

Posted 03 April 2012 - 02:08 PM

You also will want to add -

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


To make sure that the program exits when you close the JFrame.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Simple GUI program isn't running

Posted 03 April 2012 - 02:16 PM

gui in the main() method
not gui in the constructor

How to confuse everybody
Actually in the constructor you don't need to access the ContentPane you can simply write

add(p1, BorderLayout.NORTH);
add(p2, BorderLayout.SOUTH);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1