JLabel and Jframe

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shinzon
    New Member
    • Jun 2007
    • 7

    JLabel and Jframe

    Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this:


    [code=java]
    JLabel jl = new JLabel("DVD ID #= " + dvd[0][0]); //Displays DVD id

    JLabel j2 = new JLabel("Name of DVD = " + dvd[0][1]); //Displays dvd name

    JFrame frame = new JFrame( "Matt's DVD's" ); // Sets frame for label
    frame.setDefaul tCloseOperation ( JFrame.EXIT_ON_ CLOSE );
    frame.getConten tPane().add(jl) ;
    frame.setSize( 420, 170 ); // set frame size
    frame.setVisibl e( true ); // display frame
    [/code]

    I got it to display one of them, but it will not display a second one. What do I need to add to make it work? Any help would be awesome
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Shinzon
    Ok so I have been working on this for a moment now and wondering how to add multiple jlabels to a jframe. the code looks like this:

    Code:
    JLabel jl = new JLabel("DVD ID #= " + dvd[0][0]); //Displays DVD id 
    
           JLabel j2 = new JLabel("Name of DVD = " + dvd[0][1]); //Displays dvd name
           
           JFrame frame = new JFrame( "Matt's DVD's" ); // Sets frame for label
           frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
           frame.getContentPane().add(jl);
           frame.setSize( 420, 170 ); // set frame size
           frame.setVisible( true ); // display frame
    I got it to display one of them, but it will not display a second one. What do I need to add to make it work? Any help would be awesome
    You now need to learn about layout managers. GridLayout might be the easiest for starters so you may want to learn that first. You should also consider using panels to better format your JFrame

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      The Swing approach to visual components is a layered one: there are components
      and containers. A container also is a component but it can contain other components.
      A simple component is e.g. a JLabel, a JMenuItem etc.

      A container needs a LayoutManager that determines the location of the other
      components and their size. Most LayoutManagers negotiate with the contained
      components about their actual size. LayoutManagers also determine how many
      components can be contained, e.g. the BorderLayout can only contain five
      other components.
      Note that a container can contain other containers as well because containers
      are also components.

      I normally sketch things on paper to figure out where and how I want to display
      the entire GUI. In your case a GridLayout may come in handy: set the LayoutManager
      of the contentPane of the JFrame to a GridLayout, feed it all the JLabels and
      you're in business.

      kind regards,

      Jos

      Comment

      • Shinzon
        New Member
        • Jun 2007
        • 7

        #4
        Originally posted by JosAH
        In your case a GridLayout may come in handy: set the LayoutManager
        of the contentPane of the JFrame to a GridLayout, feed it all the JLabels and
        you're in business.
        Any links or some reading on how the layout would work? Because as it stands I can only add one jlabel. Basically what I want is the ability to have as many jlabels as possible in the container of jframe.

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Shinzon
          Any links or some reading on how the layout would work? Because as it stands I can only add one jlabel. Basically what I want is the ability to have as many jlabels as possible in the container of jframe.
          You can read about here.

          I hope you've downloaded the whole tutorial.

          Comment

          • emekadavid
            New Member
            • Mar 2007
            • 46

            #6
            place your components like Jlabel in a swing container, then in calling the contentPane, Jframe add the container as its contentpane. Window is the only container for JFrame. A JPanel is a swing container for swing's components.
            The following code illustrates this. The constructor adds the components, you Jlabels to itself, since it inherits from Jpanel and then Jframe takes it as its contentpane.
            Code:
            package examples;
            
            import javax.swing.*;
                    
            public class Trials extends JPanel {
                
                //i added three labels; you can as much as you like
                JLabel label_1_ID, label_1_name, label_2_ID, label_2_name;
                String[][] mattDvds = {{"1","The homecoming"}, 
                                    {"2", "Castaway"}, 
                                    {"3" ,"Matt's Trunk"}};
                /** Creates a new instance of Trials */
                public Trials() {
                    //note that the default layout for JPanel is flowlayout
                    //so you can change layout with subsequent labels
                    label_1_ID = new JLabel("DVD ID# = "+ mattDvds[0][0]);
                    add(label_1_ID);
                    label_1_name = new JLabel("DVD Name: "+ mattDvds[0][1]);
                    add(label_1_name);
                    label_2_ID = new JLabel("DVD ID# = "+mattDvds[1][0]);
                    add(label_2_ID);
                    label_2_name = new JLabel("DVD Name: "+mattDvds[1][1]);
                    //...you can add other labels. try using a second method and a loop
                }
                
                //create a method for the frame a la java tutorial
                public static void dFrame(){
                    JFrame frame = new JFrame("Matt's DVDs");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    
                    //extends from JPanel, your contentpane
                    JComponent contentPane = new Trials(); 
                    contentPane.setOpaque(true);
                    frame.getContentPane().add(contentPane);
                    
                    frame.pack();
                    frame.setVisible(true);
                    
                }
                
                public static void main(String[] args){
                    javax.swing.SwingUtilities.invokeLater(new Runnable(){
                        public void run(){
                            dFrame();
                        }
                    });
                }
            }

            Comment

            Working...