Results 1 to 2 of 2
  1. #1
    jumpman8947 is offline Member
    Join Date
    Feb 2013
    Posts
    38
    Rep Power
    0

    Default java screen not showing

    I programmed a screen where it supposed to show different labels, text areas, buttons, etc, and it compiles with no errors but when I run the program the command prompt shows a lot of exceptions I can't figure out why its there, and my window isn't showing up... here's my code thanks
    Java Code:
    import java.awt.*;
    import java.io.*;
    import sun.audio.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.Color.*;
    import javax.swing.JFrame.*;
    import java.awt.EventQueue.*;
    
    public class EncMain extends JFrame
    {
    	private JLabel imageIcon;
    	private JComboBox box1;
    	private JButton button1;
    	private JLabel label1;
    	private JLabel label2;
    	private JTextField field1;
    
    
    	public EncMain()
    	{
    		JLabel label1 = new JLabel();
    		JLabel label2 = new JLabel();
    		JButton button1 = new JButton();
    		JTextField text1 = new JTextField();
    		JComboBox box1 = new JComboBox();
    
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setTitle("Main Menu");
    		setBackground(new Color(204, 102, 0));
    
    		label1.setIcon(new ImageIcon ("bible3.gif"));
    		label1.setBorder(BorderFactory.createLineBorder(Color.blue));
    
    		label2.setIcon(new ImageIcon ("seed.jpg"));
    		label2.setPreferredSize(new Dimension(414,330));
    
    		button1.setBackground( new Color(0, 0, 0));
    		button1.setForeground(new Color(255, 0, 0));
    		button1.setText("Exit");
    
    		text1.setBackground(new Color(0, 102, 102));
    		text1.setFont(new Font("Times New Roman", 1, 24));
    		text1.setText("Welcome to Wisdom");
    
    		box1.setModel(new DefaultComboBoxModel(new String[]
    		{
    			"Gospel", "Raeggae", "Hip-Hop"
    
    		}));
    		GroupLayout layout = new GroupLayout(getContentPane());
    		getContentPane().setLayout(layout);
    		layout.setHorizontalGroup(
    			layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addComponent(label2, GroupLayout.PREFERRED_SIZE, 340, GroupLayout.PREFERRED_SIZE)
    			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 286, Short.MAX_VALUE)
    			.addComponent(label1, GroupLayout.PREFERRED_SIZE, 146, GroupLayout.PREFERRED_SIZE))
    			.addGroup(layout.createSequentialGroup()
    				.addGap(300, 300, 300)
    				.addComponent(button1, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE)))
    			.addContainerGap())
    			.addGroup(layout.createSequentialGroup()
    				.addGap(262, 262, 262)
    				.addComponent(text1, GroupLayout.PREFERRED_SIZE, 228, GroupLayout.PREFERRED_SIZE)
    				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 116, Short.MAX_VALUE)
    				.addComponent(box1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    				.addGap(108, 108, 108))
    
    		);
    
                              pack();
                              setVisible(true);
    
    
    
    		layout.setVerticalGroup(
    			layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
    			.addGroup(layout.createSequentialGroup()
    			.addGap(8, 8, 8)
    			.addComponent(field1, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
    			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
    			.addComponent(label2, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE))
    			.addGroup(layout.createSequentialGroup()
    			.addGap(16, 16, 16)
    			.addComponent(box1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
    			.addGap(18, 18, 18)
    			.addComponent(label1)))
    			.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
    			.addComponent(button1, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
    			.addContainerGap())
    
    			);
    			pack();
    			setVisible(true);
    		}
    		public static void main(String[] args)
    		{
    			new EncMain();
    		}
    
    }
    The exception threads are as followed

    Exception in thread "main" java.lang.IllegalStateException: javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.Bas icTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0 .0,border=javax.swing.plaf.BorderUIResource$Compou ndBorderUIResource@1c843d62,flags=296,maximumSize= ,minimumSize=,pre
    ferredSize=,caretColor=sun.swing.PrintColorUIResou rce[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResourc e[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIRes ource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selection
    Color=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],columns=0,columnWidth=0,command=,horizontalAlignm ent=LEADING] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayou t.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1 040)
    at javax.swing.GroupLayout.preferredLayoutSize(GroupL ayout.java:878)
    at java.awt.Container.preferredSize(Container.java:17 88)
    at java.awt.Container.getPreferredSize(Container.java :1773)
    at javax.swing.JComponent.getPreferredSize(JComponent .java:1662)
    at javax.swing.JRootPane$RootLayout.preferredLayoutSi ze(JRootPane.java:917)
    at java.awt.Container.preferredSize(Container.java:17 88)
    at java.awt.Container.getPreferredSize(Container.java :1773)
    at javax.swing.JComponent.getPreferredSize(JComponent .java:1662)
    at java.awt.BorderLayout.preferredLayoutSize(BorderLa yout.java:719)
    at java.awt.Container.preferredSize(Container.java:17 88)
    at java.awt.Container.getPreferredSize(Container.java :1773)
    at java.awt.Window.pack(Window.java:809)
    at EncMain.<init>(EncMain.java:73)
    at EncMain.main(EncMain.java:102)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: java screen not showing

    Every component in a GroupLayout has to belong somewhere in a Horizontal and Vertical group before you attempt to pack() or set the GUI visible.

    Additionally, whenever you post code on a forum, and particularly when appending a stack trace, make sure that the line numbers remain the same. Line 74 in your posted code corresponds to line 73 in the code that resulted in that error.

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

Similar Threads

  1. Java Console for Java Applet not showing any code
    By Guestnorth90 in forum Java Applets
    Replies: 1
    Last Post: 02-20-2013, 05:50 PM
  2. Replies: 9
    Last Post: 09-06-2012, 09:37 AM
  3. Showing normal text on screen
    By johny2011 in forum Advanced Java
    Replies: 1
    Last Post: 05-18-2011, 10:29 AM
  4. Reading characters without showing up on the screen?
    By alexsb92 in forum New To Java
    Replies: 11
    Last Post: 03-28-2010, 08:08 PM
  5. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 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
  •