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: 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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Adding JPanel to JFrame

    I see two JFrame objects being created. Where is the stuff you want to see?
    If you don't understand my answer, don't ignore it, ask a question.

  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
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 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.
    If you don't understand my answer, don't ignore it, ask a question.

  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
    21
    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 10:36 AM. Reason: Removed spoonfed code

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,164
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Adding JPanel to JFrame

    @Motion Please don't do OPs work for them.
    See: http://www.javaprogrammingforums.com...n-feeding.html
    If you don't understand my answer, don't ignore it, ask a question.

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, 04:12 AM
  4. need help with ActionListener,JPanel,JFrame
    By amahara in forum AWT / Java Swing
    Replies: 5
    Last Post: February 3rd, 2010, 01:40 PM