java screen not showing
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: java screen not showing

  1. #1
    Join Date
    Feb 2013
    Posts
    22

    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
    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.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1c843d62,flags=296,maximumSize=,minimumSize=,pre
    ferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[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=,horizontalAlignment=LEADING] is not attached to a vertical group
    at javax.swing.GroupLayout.checkComponents(GroupLayout.java:1090)
    at javax.swing.GroupLayout.prepare(GroupLayout.java:1040)
    at javax.swing.GroupLayout.preferredLayoutSize(GroupLayout.java:878)
    at java.awt.Container.preferredSize(Container.java:1788)
    at java.awt.Container.getPreferredSize(Container.java:1773)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at javax.swing.JRootPane$RootLayout.preferredLayoutSize(JRootPane.java:917)
    at java.awt.Container.preferredSize(Container.java:1788)
    at java.awt.Container.getPreferredSize(Container.java:1773)
    at javax.swing.JComponent.getPreferredSize(JComponent.java:1662)
    at java.awt.BorderLayout.preferredLayoutSize(BorderLayout.java:719)
    at java.awt.Container.preferredSize(Container.java:1788)
    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
    Join Date
    Mar 2013
    Location
    Antwerp, Belgium
    Posts
    3

    Re: java screen not showing

    you create your objects here:
    Code:
    public class EncMain extends JFrame
    {
    private JLabel imageIcon;
    private JComboBox box1;
    private JButton button1;
    private JLabel label1;
    private JLabel label2;
    private JTextField field1;
    And later you create them again:
    Code:
    JLabel label1 = new JLabel();
    JLabel label2 = new JLabel();
    JButton button1 = new JButton();
    JTextField text1 = new JTextField();
    JComboBox box1 = new JComboBox();
    you should change that last in:
    Code:
    this.label1 = new JLabel();
    this.label2 = new JLabel();
    this.button1 = new JButton();
    this.field1 = new JTextField();
    this.box1 = new JComboBox();

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center