Results 1 to 14 of 14
  1. #1
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default Title not displayer

    Hey guys. For some reason, title "Welcome to Movie2Go Kioks Services" is not being displayed. Thanks for your help.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class NewJFrame extends JFrame implements ActionListener
    {
        JFrame frame = new JFrame();
        FlowLayout layoutcenter = new FlowLayout (FlowLayout.CENTER);
        JPanel leftpanel = new JPanel(layoutcenter);
        JPanel rightpanel = new JPanel(layoutcenter);  
        Container framecontainer = frame.getContentPane();
        Color backgroundcolor = new Color(179,50,55);
        Color white = new Color(255,255,255);
        Font titlefont = new Font("Arial",Font.BOLD,40);
        JLabel title = new JLabel ("Welcome to Movie2Go Kiosk Services");
        public static void main (String[] args)
        {
            NewJFrame driver = new NewJFrame();
            driver.start();
        }
        public void start()
        {
            frame();
            panel();
            title();
        }
        public void frame()
        {
          frame.setSize(1280, 1024);
          frame.setTitle("Movie2Go Kiosk Service");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
        }
        public void panel()
        {
            JPanel leftpanel = new JPanel();
            JPanel rightpanel = new JPanel();  
            leftpanel.setBounds(0,0,290,1024);
            rightpanel.setBounds(290,0,990,1024);
            leftpanel.setBackground(white);
            rightpanel.setBackground(white);
            frame.add(leftpanel);
            frame.add(rightpanel);
        }
        public void title()
        {
             title.setFont(titlefont);
             title.setForeground(backgroundcolor);
             rightpanel.add(title);
        }
        public void actionPerformed(ActionEvent e)
        {
        }
    }

  2. #2
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    Someone please help

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What do you add that JLabel to? And what do you add that to? Do you eventually add it to a component that is visualized in the JFrame itself?

    Other questions:
    1) Why have your class subclass JFrame?
    2) You're avoiding the use of layout managers, why?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    Im sorry. I am fairly new to java having started learning it earlier this year at my high school. Now to answer your questions.
    I am directly adding the JLabel to a JFrame.
    I found a tutorial online on graphics where its examples had JFrame as a subclass and I decided to follow that.
    I tried using FlowLayout earlier and it still did not work.

  6. #6
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    Heres an updated version of my code. I tried using revalidate and repaint but it still did not work.
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    public class Driver extends JFrame implements ActionListener
    {
        JFrame frame = new JFrame(); //Creates frame
        JPanel leftpanel = new JPanel(); //Creates panel
        JPanel rightpanel = new JPanel();  //Creates panel
        Color backgroundcolor = new Color(179,50,55); //Gets background color
        Color white = new Color(255,255,255); //Gets color white
        Font titlefont = new Font("Arial",Font.BOLD,40); //Gets title font 
        JLabel title = new JLabel ("Welcome to Movie2Go Kiosk Services"); //Title
        JLabel film =  new JLabel ("film-reel.jpg");
        JButton mem = new JButton("I am an existing member");
        JButton memberlogin = new JButton("Login");
        JButton nonmem = new JButton("I am new");
        JButton checkinfo = new JButton("Check your information");
        JButton rentmovie = new JButton("Rent a movie");
        TextField membername = new TextField("Please Enter Your Name Here");
        TextField memberpass = new TextField("Please Enter Your Password Here");
        public static void main (String[] args)
        {
            Driver run = new Driver(); //Instantiate Driver
            run.start(); //Runs start method of Driver
        }
        public void start()
        {
            frame(); //Runs frame method
            panel(); //Runs panel method
            startpage(); //Runs startpage method
            startoption(); //Runs startoption method
        }
        public void frame()
        {
            frame.setSize(1280, 1024); //Sets size of frame
            frame.setTitle("Movie2Go Kiosk Service"); //Sets title of frame
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Exits program when closed
            frame.setVisible(true); //Makes frame visible
        }
        public void panel()
        {
            JPanel leftpanel = new JPanel(); //Instantiate new JPanel
            JPanel rightpanel = new JPanel(); //Instantiate new JPanel
            leftpanel.setBounds(0,0,290,1024); //Sets size and position for leftpanel
            rightpanel.setBounds(290,0,990,1024); //Sets size and position for rightpanel
            leftpanel.setBackground(white); //Sets background for leftpanel
            rightpanel.setBackground(backgroundcolor); //Sets background for rightpanel
            frame.add(leftpanel); //Adds leftpanel onto frame
            frame.add(rightpanel); //Adds rightpanel onto frame
        }
        public void startpage()
        {
            title.setFont(titlefont); //Sets font title
            title.setForeground(white); //Sets color of title
            rightpanel.add(title); //Adds title onto rightpanel
            rightpanel.revalidate();
            rightpanel.repaint();
        }
        public void startoption()
        {
            mem.setBounds(45,100,200,30); 
            mem.addActionListener(this);
            nonmem.setBounds(45,130,200,30);
            nonmem.addActionListener(this);
            leftpanel.add(nonmem);
            leftpanel.add(mem);
            leftpanel.revalidate();
            leftpanel.repaint();
        }
        public void actionPerformed(ActionEvent e)
        {
            Object source = e.getSource(); //Gets source of ActionEvent
        }
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by weezy2894 View Post
    I am directly adding the JLabel to a JFrame.
    Where? Which line(s) of code do this?

    I found a tutorial online on graphics where its examples had JFrame as a subclass and I decided to follow that.
    Don't do this unless you have reason to do it. You are creating and displaying a separate JFrame in your program and you are not using your NewJFrame class as a JFrame object, so it should not extend JFrame.

    I tried using FlowLayout earlier and it still did not work.
    Nothing will work if you don't add the JLabel to either directly or indirectly (via a hierarchy of containers that eventually lead to the JFrame) to the JFrame.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by weezy2894 View Post
    Heres an updated version of my code. I tried using revalidate and repaint but it still did not work.
    ...
    Again, answer this simple question, what do you add the title JLabel to?

  9. #9
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    line56: rightpanel.add(title); //Adds title onto rightpanel

    Jlabel title is added onto Jpanel rightpanel via add()
    Last edited by weezy2894; 04-18-2011 at 02:39 AM.

  10. #10
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Where? Which line(s) of code do this?
    Sorry I meant that I was adding JLabel to JPanel not JLabel to JFrame
    Last edited by weezy2894; 04-18-2011 at 02:38 AM.

  11. #11
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default

    Java Code:
    public class Driver extends JFrame implements ActionListener
    Why are you still extending JFrame?

    Java Code:
    frame.setSize(1280, 1024); //Sets size of frame
    Don't assume a certain resolution. I still use 1024 x 768 so your app won't work for me. If you want to maximize the app at startup then you can use:

    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);

    Java Code:
    leftpanel.setBounds(0,0,290,1024); //Sets size and position for leftpanel
    rightpanel.setBounds(290,0,990,1024); //Sets size and position for rightpanel
    Again this doesn't make sense since I don't use that resolution. That is why you should always be using a layout manager so you don't hard code values like that.

    Java Code:
    frame.add(leftpanel); //Adds leftpanel onto frame
    frame.add(rightpanel); //Adds rightpanel onto frame
    This is causing some wierd results. By default a JFrame uses a BorderLayout. When you add a component to the frame and you don't specify a location the component is added to the CENTER. You are adding two components to the center but the layout manager only manages the last component added to the frame. So the size of that component is set to take up the entire space of the frame. However because of the way Swing does its painting, it paints the last component added first. So the right panel gets painted and then the left panel gets painted on top of it. So by a fluke you actually see two panels painted even though the layout manager know about one of them.

    This is obviously not a good design and is very confusing. You need to read the section from the Swing tutorial on A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container). I suggest you start with the example code which show a better structure for you programs.

    I have no idea why the label doesn't appear. But I'm sure by using proper layout managers and proper program structure you will solve your problem.

  12. #12
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    Thank you for your help

  13. #13
    weezy2894 is offline Member
    Join Date
    Apr 2011
    Location
    SoCal
    Posts
    10
    Rep Power
    0

    Default

    I have a question. What if one of my panels is 1/4 the size of the other and I want to lay them side by side. How would I do that with any of the layout managers?

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

    Default

    Hm, perhaps by spending a bit more than half an hour to go through the tutorial linked by camickr?

    db

Similar Threads

  1. How to rename GUI Title
    By rjagan in forum New To Java
    Replies: 5
    Last Post: 06-11-2014, 11:51 AM
  2. Change the title
    By AndrewSD in forum NetBeans
    Replies: 1
    Last Post: 01-11-2011, 03:23 AM
  3. Replies: 2
    Last Post: 02-10-2010, 08:27 PM
  4. set * at tab title
    By keffie91 in forum Advanced Java
    Replies: 7
    Last Post: 10-06-2008, 07:21 PM
  5. How can I add an ICON on a JDialog's Title-Bar?
    By iimasd in forum AWT / Swing
    Replies: 2
    Last Post: 11-06-2007, 12:54 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
  •