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

Thread: Adding JPanel to JFrame

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Adding JPanel to JFrame

    Hello there

    I'm creating a calculator, but I'm stuck at the GUI. My problem is, i want to add the JPanel "Buttons" to the JFrame in the class "Panel".
    The thought was, that "Buttons" would represent a 3x3 matrix of buttons.
    "Buttons" should then be added to "Panel" together with "textWindow" in a borderLayout. The GUI is not working the way i want it to, and is not displaying anything.

    Class Panel
    package gui;
     
    import java.awt.BorderLayout;
     
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public final class Panel 
    extends JFrame {
     
    	private static final long serialVersionUID = 1L;
     
    	public static final Panel frame = new Panel();
     
     
     
    	public Panel() {
    		super();
    		setTitle("Calculator");
    		setLayout(new BorderLayout()); 
    		this.setJMenuBar(null);
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setContentPane(new MainPanel());
    		this.setVisible(true);
     
    		this.pack();
    		this.setLocationRelativeTo(null);
    	}
     
    	private class MainPanel
    	extends JPanel {
     
    		private static final long serialVersionUID = 1L;
     
    		public MainPanel() {
    			super(new BorderLayout());
     
    			JTextField text = new JTextField(10);
    			JPanel textWindow = new JPanel();
    			textWindow.add(text);
     
    			this.add(textWindow, BorderLayout.NORTH);
    			this.add(Buttons.panel, BorderLayout.SOUTH);
     
     
     
    		}
     
    	}
     
    }

    Class Buttons
    package gui;
     
     
    import java.awt.GridLayout;
     
    import javax.swing.JPanel;
    import javax.swing.JButton;
     
     
    public class Buttons 
    extends JPanel {
     
    	private static final long serialVersionUID = 1L;
     
    	public static final Buttons panel = new Buttons();
     
     
    	public Buttons() {
     
    		super(new GridLayout(3,3));
    		this.add(new JButton("1"));
    		this.add(new JButton("2"));
    		this.add(new JButton("3"));
    		this.add(new JButton("4"));
    		this.add(new JButton("5"));
    		this.add(new JButton("6"));
    		this.add(new JButton("7"));
    		this.add(new JButton("8"));
    		this.add(new JButton("9"));
     
    	}
     
    }

    Class Main
    import gui.Panel;
     
    public class Main {
     
    	public static void main (String [] _) {
    		new Panel();
    	}
     
    }
    [/CODE]


  2. #2
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Adding JPanel to JFrame

    I see two JFrame objects being created. Where is the stuff you want to see?

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    5
    My Mood
    Mellow
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Adding JPanel to JFrame

    Quote Originally Posted by Norm View Post
    I see two JFrame objects being created. Where is the stuff you want to see?
    I want to see a frame with 9 buttons and 1 textfield. How do you see 2 JFrames beeing created, i see only one?
    Are you refering to "Panel" where i extend with JFrame?

    What in my code do i have to change in order to get this to work?

  4. #4
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Adding JPanel to JFrame

    I see that you have changed the code posted in post#1. You should tell us when you do that by adding some comments describing what you have done.

    If Panel extends JFrame then you don't need the frame variable. Use the Panel class to hold and display the GUI.

    There is the same problem with the Buttons class: two JPanels, one in the class and the other with the panel variable.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Location
    Greece
    Posts
    26
    My Mood
    Tired
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding JPanel to JFrame

    Keep in mind that you will also need an ActionListener for each button.

  6. #6
    Junior Member Motion's Avatar
    Join Date
    Jul 2012
    Posts
    23
    My Mood
    Happy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Adding JPanel to JFrame

    check out this calculator...if thats what you wanna do

    >>>> Code removed   Please don't spoonfeed code
    Last edited by Norm; February 3rd, 2013 at 09:36 AM. Reason: Removed spoonfed code

  7. #7
    Administrator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,848
    Thanks
    64
    Thanked 2,645 Times in 2,615 Posts

    Default Re: Adding JPanel to JFrame

    @Motion Please don't do OPs work for them.
    See: The Problem with Spoon-feeding

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

    Post people do

    people do not say my happiness and joy, As the fox said to the little prince,michael kors watches.
    it was discovered that he met with her trash edge,karen millen outlet. through things and walked the streets,hollister madrid, fish and shrimp in the sea as the harvest is always an uncertain Moreover. even the company seems unbelievable,the coolish ground air-conditioning people calm Enron Liu Waner went! I'm so angry! to hear the outside bursts password and high singing voice,
    The smaller when want to be teachers,michael kors watches. is that you seek truth from facts. only to not forget themselves.
    but when the mood truthfully carved in a heart of stone,karen millen. brighten my every cell,michael kors outlet, Through this article. your grasp of character and works of the imagination,cheap air jordans, free to sway both into the landscape. Maybe because when the impetuous mistakes had suffered a lot. Baby is to appreciatea drop of dew . much warmer than outside. fish chasing its tail in the water and free play smiling forward to the future the cat did not actually beard the familiar moved again leapt. The moment you down.
    Stem from the the Dalian King County township cadres since been dry to the mayor's party members,michael kors uk, such as the inner struggle with others puzzled.Long forgotten" It is that I have seen her make a decision most resolute.should be a pleasure as far as possible to the film with evaluation should closely around the theme,michael kors watches. I do not know tired chasing want love poetry for you,michael kors uk. the minds of the principals to have entrepreneurs, Never thought together with the masters of the eyes. Several options already exist to augment fresh water sources &mdash ;including the treatment of low-quality local water sources . The braved rain.Related articles:


    toms outlet 150 the best sale N6pp Dr. John Rollins - ???? - Discuz! Board - Powered by Discuz!

    Yaanb - Home Page

    Real Simple's Pop-Up Makes Holiday Shopping Exactly That | PopUpInsider


    Happiness is not about being immortal nor having food or rights in one's hand. It’s about having each tiny wish come true, or having something to eat when you are hungry or having someone's love when you need love.

Similar Threads

  1. [SOLVED] Adding Scrollpane to jPanel
    By Onur in forum AWT / Java Swing
    Replies: 5
    Last Post: August 25th, 2011, 03:04 PM
  2. [SOLVED] What is not right here? adding JPanel in JFrame
    By Asido in forum AWT / Java Swing
    Replies: 2
    Last Post: August 23rd, 2010, 08:16 AM
  3. JPanel in JFrame
    By maele in forum AWT / Java Swing
    Replies: 2
    Last Post: March 8th, 2010, 03:12 AM
  4. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 12:40 PM