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 3 of 3

Thread: JLabel not appearing in new Frame ? please help

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default JLabel not appearing in new Frame ? please help

    Need some help guys not cross posting this time.

    Ok here is my code:

     
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.*;
    import java.io.*; 
    import javax.swing.filechooser.FileFilter;
    import java.awt.*; 
    public class Hashnain extends JFrame{
     
    	JMenuBar menuBar;
    	   JMenu menu;
    	   JMenuItem menuItem1;
    	   JMenuItem menuItem2;
    	   JMenuItem menuItem3;
    	   JFileChooser c = new JFileChooser();
     
     
       public Hashnain(){
          super("The title");
          setLayout(new FlowLayout());
     
          //Create the menu bar.
          menuBar = new JMenuBar();
     
          //Build the first menu.
          menu = new JMenu("Guide");
          menuItem1 = new JMenuItem("Guide");
          menuItem2 = new JMenuItem("About");
          menuItem3 = new JMenuItem("Exit");
          menu.add(menuItem1);
          menu.add(menuItem2);
          menu.add(menuItem3);
          add(menu);
          menuBar.add(menu);
          add(menuBar);
     
    thehandler handler = new thehandler();
     
          menuItem1.addActionListener(handler);
          menuItem2.addActionListener(handler);
          menuItem3.addActionListener(handler);
       }
     
       private class thehandler implements ActionListener{
     
          public void actionPerformed(ActionEvent event){
             String string = "";
     
             if(event.getSource()==menuItem3)
            	 System.exit(0);
          // Demonstrate "Open" dialog:
     
             if(event.getSource()==menuItem1)
             {
             JFrame frame = new JFrame("click");
     
    JLabel bf= new JLabel("This application allow users to communicate securely.All the coding is written in Java language.");
     
    		frame.add(bf);
    		frame.setVisible(true);
     	    frame.setSize(400,400);
     
    		JTextPane textPane = new JTextPane();
    		textPane.setEditable(false);
    		textPane.setText("This is a simple Image steganography application which allows user to send and receive messages embedded inside images.The user is able to choose the image he wants and the program tells if this image will suit the text or not");
    		//  Define a keyword attribute
    		//  Add some text
    		frame.add(textPane);
     
     
     
     
      	} }
       }
    }
         Main class
     
    import javax.swing.JFrame;
     
    public class Fish {
       public static void main(String[] args){
     
          Hashnain bucky = new Hashnain();
          bucky.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          bucky.setSize(350, 100);
          bucky.setVisible(true);
       }
    }

    The problem is when you click on the "Guide" menu item there appears a new frame but I have also added a JLabel to the frame which does not appear.
    Please check my code where am I doing wrong ?

    Thanks a ton in advance.


  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: JLabel not appearing in new Frame ? please help

    Check that the layout manager knows what to do with components being added to it. The calls to the add() method don't give the layout manager any instructions on where to put the components. They could both be put in the same location, the last one hiding the first one.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: JLabel not appearing in new Frame ? please help

    Hi

    You need to specify a layout for displaying swing components.
    Say for eg you can set the frame layout to flowlayout (Flowlayout simply lays out components in a single row,)
    use this statement in your code and check
    frame.setLayout(new FlowLayout( FlowLayout.CENTER));

    Hope this solves your problem

Similar Threads

  1. Components not appearing (need help!)
    By hawkeye01 in forum Object Oriented Programming
    Replies: 6
    Last Post: March 24th, 2013, 09:41 AM
  2. Loop through JLabel and change JLabel
    By JoeBrown in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2012, 12:52 PM
  3. Strange boxes appearing
    By fishnj1333 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 11th, 2012, 05:36 AM
  4. Replies: 1
    Last Post: January 19th, 2012, 03:44 PM
  5. Replies: 1
    Last Post: August 15th, 2011, 09:26 AM