Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: My GUI programs will not display.

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation My GUI programs will not display.

    I am currently new to Java and I am trying to create a GUI but when i run my code nothing shows up except an empty box only showing the title. Can anyone explain what could possible be wrong?

    import javax.swing.* ;
    import java.awt.event.* ;
    import java.awt.* ;
     
     
    public class Airplane extends JFrame 
    {
    	private static final int WIDTH = 400;
    	private static final int HEIGHT = 300;
    	private JLabel reserveR;
    	private JTextField reserveTF;
    	private JButton yesB, noB;
    	private CalculateButtonHandler cbhandler;
     
    public Airplane()
    {
    	Container pane = getContentPane();
    	pane.setLayout(new GridLayout(3,1));
    	setTitle("Airplane Seating");
    	setSize(WIDTH,HEIGHT);
    	setDefaultCloseOperation(EXIT_ON_CLOSE);
    	setVisible(true);
     
     
    	reserveR= new JLabel("To reserve a ticket click Yes or No ", SwingConstants.CENTER);
    	reserveTF = new JTextField(10);
     
     
    	yesB = new JButton ("Yes");
    	cbhandler = new CalculateButtonHandler();
    	yesB.addActionListener(cbhandler);
     
    	noB = new JButton ("No");
    	cbhandler = new CalculateButtonHandler();
    	noB.addActionListener(cbhandler);
     
     
    	pane.add(reserveR);
    	pane.add(yesB);
    	pane.add(noB);
    	pane.add(reserveTF);
     
    }
    private class CalculateButtonHandler implements ActionListener
    {
    	public void actionPerformed (ActionEvent event)
    	{
    		if ( event.getSource() == yesB)
    		{
    		System.exit(0);
    		}
    		else if ( event.getSource() == noB)
    		{
    			System.exit(0);
    		}
    	}
    }
    public static void main(String[] args)
    {
        Airplane Airp = new Airplane();
    }
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: My GUI programs will not display.

    Try waiting until AFTER all the components have been added before calling setVisibile()
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Greetings programs
    By neveser in forum Member Introductions
    Replies: 2
    Last Post: December 7th, 2012, 10:41 PM
  2. Using i-1 in programs
    By JoshKesner in forum Java Theory & Questions
    Replies: 4
    Last Post: November 7th, 2012, 06:01 PM
  3. SWING GUI CONSOLE WINDOW DISPLAY
    By Khadafi in forum Java Theory & Questions
    Replies: 5
    Last Post: January 10th, 2012, 09:15 AM
  4. Multiple errors - Reading data from a file and using a GUI to display
    By toledospod in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: December 1st, 2011, 09:34 AM
  5. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM

Tags for this Thread