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

Thread: Extending JFrame

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Extending JFrame

    The last time I said that I would fix the code.

    Well I clicked on the error and eclipse suggested a fix and I used it and further placed two sub menus under "save"

    I really do not know why the change worked

    Why should I do this (file.add(save1) when I have already called the file save (JMenuItem save = new JMenu("Save")

    Can you please help me to understand the code or send me a PDF on the extending JFrame

    Thanks


    Here is the new code

    import java.awt.BorderLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
     
    import  javax.swing.JButton;
    import  javax.swing.JFrame;
    import  javax.swing.JMenu;
    import  javax.swing.JMenuBar;
    import  javax.swing.JMenuItem;
     
    public class Menu3 extends JFrame implements ActionListener
    {
     
    	private static final long serialVersionUID = 1L;
     
    		public static void main (String[] args)
    		{
     
    	new Menu3().setVisible(true);
    		}
    	private Menu3()
    		{
    super("Tutorial = Rhoen Menu #1");
    setSize (600,600);
    setResizable (false);
    setDefaultCloseOperation (EXIT_ON_CLOSE);
     
    JButton button = new JButton ("Click me");
    button.addActionListener(this);
    button.setActionCommand("Click");
     
    JMenuBar bar = new JMenuBar();
    JMenu file = new JMenu ("File");
    JMenuItem  newMenuItem = new JMenu("New");
    JMenuItem  save = new JMenu("Save");
     
     
    JMenuItem  close  = new JMenuItem("Exit");
    close.addActionListener(this) ;
     
    JMenuItem extra = new JMenu("Extra");
    JMenuItem hello = new JMenuItem("Hello");
    JMenuItem hello2 = new JMenuItem("Hello 2");
     
    extra.add(hello);
    extra.add(hello2);
     
    JMenuItem save1 = new JMenu("Save");
    JMenuItem now = new JMenuItem("Now");
    JMenuItem later = new JMenuItem("Later");
     
    save1.add(now);
    save1.add(later);
     
     
    file.add (newMenuItem);
    file.add(save1);
    file.add(extra);
    file.addSeparator();
    file.add(close);
     
    bar.add(file);
    add(button,  BorderLayout.WEST);
    setJMenuBar (bar);
    		}
     
    public void actionPerformed(ActionEvent e)
    	{
    	String  name  = e.getActionCommand();
    	if (name.equals("Click"))
    	{
    		System.out.println("Click me");
    	}
    	else if (name.equals("Exit"))
    	{
    System.out.println("Closed");
    System.exit(0); 
    	}
    }
    }
    Last edited by jps; September 2nd, 2013 at 01:35 AM. Reason: code tags


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Extending JFrame

    Hello.
    If you have time try to read the chapter on "GUI Components" from the textbook "Java How to Program" by Deitel & Deitel.
    All your doubts shall be cleared.

    Syed.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Extending JFrame

    Please post your code in code tags.

    I don't understand what you saw in Eclipse and what happened when you allowed Eclipse to mess with your code. I rarely let Eclipse do things to my code, but if I'm curious, I use undo/redo multiple times until I see all of the changes and understand them. See if you can recreate the condition before Eclipse made the changes, and then post the error (or was it simply a warning?) so that we can explain to you what's going on.

    As for explaining to you the code you posted, what part(s) do you have questions about? I doubt you need a line-by-line explanation, and you're not likely to get it. You might refer to the Java Swing Tutorials for further insight into creating GUIs, but be sure to ignore their advice to use Netbeans and the GUI Builder.

    If you're doing self-study and don't have a requirement to know Swing, you might also consider moving to JavaFX, since that's the direction Oracle is taking Java.

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending JFrame

    If you have been following my communication: I posted a question and the person who answered the question suggested that I go through the code and find the error (apparently the error was simple). when I went through the code I could not find the error and so I right clicked the error and eclipse suggested a correction which I incorporated into the code and it worked. To see the code please look at the post made before this one.

    I will go to Java Swing tutorial

    Why should I not use Netbeans I was planning to use it since it seemed a bit easier

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Extending JFrame

    Quote Originally Posted by runkerr View Post
    Why should I not use Netbeans I was planning to use it since it seemed a bit easier
    Because an IDE, especially a GUI builder, hides a ton of stuff from you, which makes it much harder to understand what's going on. For example: why are you extending JFrame?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Extending JFrame

    Quote Originally Posted by runkerr View Post
    Why should I not use Netbeans I was planning to use it since it seemed a bit easier
    I don't mind if you use Netbeans or any IDE, but I see how what I said could be interpreted that way. I recommend that you don't use the Netbeans GUI Builder, unless your objective is to construct a GUI the slightly easier way that you don't understand or know how to modify or maintain. If you want to learn how to construct GUIs that you can modify, maintain, and reuse, then learn how to code them by hand first.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Extending JFrame

    I really want to learn from the ground up (how to construct GUIs) so I will wait a while before dealing with Netbeans

    Thanks for the advice

Similar Threads

  1. Extending Jframe
    By runkerr in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 11th, 2013, 12:19 PM
  2. Extending Jframe
    By runkerr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 8th, 2013, 12:41 AM
  3. Extending JFrame + having multiple windows.
    By RedCloudTsunami in forum AWT / Java Swing
    Replies: 7
    Last Post: August 5th, 2012, 01:24 PM
  4. Extending class
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 8
    Last Post: January 10th, 2012, 05:06 PM
  5. class extending
    By Reem in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 18th, 2010, 01:49 AM