Results 1 to 4 of 4
  1. #1
    IndiDev is offline random developer
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Question JPanel on JFrame wierd problem

    I tried to add a JPanel to a JFrame, everything was fine, no errors, no runtime-errors. I've set everything visible, added the JPanel to the JFrame but still don't get any output Frame. I compared it to the syntax of several Youtube Videos but it's still not showing anything. maybe someone of you can help me?

    (note: the comments are in german, my native language, but they are very simple and actually not needed. You should understand the code even without comments)

    Main Class:

    Java Code:
    public class MyMenue{
    
      public static void main(String[] args){
      
        Menue m = new Menue();
        MenuePanel me = new MenuePanel();
        m.setVisible(true);
        me.setVisible(true);
      }
    }

    Frame Class:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Menue extends JFrame {
    
     private MenuePanel mp = new MenuePanel();
     
      //Konstruktor der Klasse Menue
      public Menue(){
        super("4Gewinnt-Game");                                   //Mit dem Konstruktor von JFrame
        this.setLayout(null);                                     //kein Layoutmanager
        this.setBounds(100,100,515,555);                          //Größe und Positon
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);      //Operation beim Fenster schließen
        this.setResizable(false);                                 //Fenster veränderbar oder nicht?
        this.add(mp);                                             //fügt MenuePanel mp zu Menue hinzu
      }
      
    }

    Panel Class:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class MenuePanel extends JPanel {
    
     private JLabel lblText;
     private JButton[] btn = new JButton[3];
     private JTextField txt1;
     private int i = 0;
     private int j;
     private int a = 0;
     JLabel l1 = new JLabel(new ImageIcon("startmenue.png"));
     MenueListener myL = new MenueListener(this);
     private Menue m = new Menue();
     
     //Konstruktor der Klasse MenuePanel
      public MenuePanel(){
        this.setLayout(null);                                            //kein Layoutmanager
        this.setBounds(0,0,515,555);                                     //Größe und Positon
        this.setBackground(Color.WHITE);                                 //Hintergrundfarbe
    
        l1.setBounds(0,0,515,555);
    
    
        for (j= 0; j < btn.length;j++){
    
         btn[j] = new JButton();
         btn[j].setBounds(150,250+a,100,23);
         a = a +25;
         if (j == 0)
          btn[0].setIcon(new ImageIcon("start.png"));
         if (j == 1)
          btn[1].setIcon(new ImageIcon("optionen.png"));
         if (j == 2)
          btn[2].setIcon(new ImageIcon("beenden.png"));
         btn[j].addActionListener(myL);
         this.add(btn[j]);
        }
    
         this.setLayout(null);
         this.add(l1);
         this.setBounds(0,0,515,555);
         m.add(this);
    
        this.setVisible(true);                             //Sichtbar oder nicht?
      }
    
      //Getter für btn[j]
      public JButton getBtnOp(int j){
        return this.btn[j];
      }
      
      //Getter für l1
      public JLabel getL1(){
        return this.l1;
      }
    
    }
    i didn't copy the listeners in, because they're not actually neccesary to at least show the frame.

    This is the first time i tried JPanels, so I assume i forgot something at defining the JPanel...
    but even then, at least the empty JFrame would show up right?

    Im thankful for every piece of hint or advice.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: JPanel on JFrame wierd problem

    You should think carefully about how the program flows. Say you 1) create a Menue 2)this creates a MenuePanel 3) the MenuePanel has a Menue variable...which in turn goes to step 1. Repeat indefinitely == infinite loop. Be careful where you instantiate one class relative to another. I recommend reviewing closely some of the examples in the Oracle tutorials and use those code flows as a guide

    And FWIW, do not use a null layout. Use an appropriate LayoutManager dependent upon how you wish your components to be organized.
    A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

  3. #3
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: JPanel on JFrame wierd problem

    ...why does the JPanel creates a JFrame and vice versa ? Why do you instantiate both at the main too and so on...thats strange!
    Java Code:
    class MenuePanel extends JPanel {
    
    	private JLabel lblText;
    	private JButton[] btn = new JButton[3];
    	private JTextField txt1;
    	private int i = 0;
    	private int j;
    	private int a = 0;
    	JLabel l1 = new JLabel(new ImageIcon("startmenue.png"));
    	
    
    	// Konstruktor der Klasse MenuePanel
    	public MenuePanel() {
    		this.setLayout(null); // kein Layoutmanager
    		this.setBounds(0, 0, 515, 555); // Größe und Positon
    		this.setBackground(Color.WHITE); // Hintergrundfarbe
    
    		l1.setBounds(0, 0, 515, 555);
    
    		for (j = 0; j < btn.length; j++) {
    
    			btn[j] = new JButton();
    			btn[j].setBounds(150, 250 + a, 100, 23);
    			a = a + 25;
    			if (j == 0)
    				btn[0].setIcon(new ImageIcon("start.png"));
    			if (j == 1)
    				btn[1].setIcon(new ImageIcon("optionen.png"));
    			if (j == 2)
    				btn[2].setIcon(new ImageIcon("beenden.png"));
    			
    			class Menue extends JFrame {
    
    
    				// Konstruktor der Klasse Menue
    				public Menue(MenuePanel panel) {
    					super("4Gewinnt-Game"); // Mit dem Konstruktor von JFrame
    					this.setLayout(null); // kein Layoutmanager
    					this.setBounds(100, 100, 515, 555); // Größe und Positon
    					this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Operation beim
    																			// Fenster
    																			// schließen
    					this.setResizable(false); // Fenster veränderbar oder nicht?
    					this.add(panel); // fügt MenuePanel mp zu Menue hinzu
    				}
    
    			}this.add(btn[j]);
    		}
    
    		this.setLayout(null);
    		this.add(l1);
    		this.setBounds(0, 0, 515, 555);
    
    		this.setVisible(true); // Sichtbar oder nicht?
    	}
    
    	// Getter für btn[j]
    	public JButton getBtnOp(int j) {
    		return this.btn[j];
    	}
    
    	// Getter für l1
    	public JLabel getL1() {
    		return this.l1;
    	}
    
    }
    Java Code:
    class Menue extends JFrame {
    
    
    	// Konstruktor der Klasse Menue
    	public Menue(MenuePanel panel) {
    		super("4Gewinnt-Game"); // Mit dem Konstruktor von JFrame
    		this.setLayout(null); // kein Layoutmanager
    		this.setBounds(100, 100, 515, 555); // Größe und Positon
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Operation beim
    																// Fenster
    																// schließen
    		this.setResizable(false); // Fenster veränderbar oder nicht?
    		this.add(panel); // fügt MenuePanel mp zu Menue hinzu
    	}
    
    }
    Java Code:
    public class MyMenue{
     
      public static void main(String[] args){
      		MenuePanel me = new MenuePanel();
    		Menue m = new Menue(me);
    		m.setVisible(true);
    		me.setVisible(true);
      }
    }
    by the way: Null Layout Is Evil!

  4. #4
    IndiDev is offline random developer
    Join Date
    Jan 2012
    Posts
    4
    Rep Power
    0

    Default Re: JPanel on JFrame wierd problem

    Ohh of course, the JPanels are only added in the Frame class. That makes a lot more sense

    Ok thanks for your help and have a nice day.

Similar Threads

  1. Replies: 2
    Last Post: 01-22-2011, 12:27 PM
  2. JFrame and JPanel
    By khiat in forum New To Java
    Replies: 6
    Last Post: 01-15-2011, 07:32 PM
  3. wierd NetBeans problem.
    By edi.gotieb in forum Forum Lobby
    Replies: 2
    Last Post: 10-09-2010, 01:31 PM
  4. Wierd problem with abstract and extends
    By g123456 in forum New To Java
    Replies: 6
    Last Post: 05-14-2010, 06:25 PM
  5. wierd problem
    By f_the_cook in forum Advanced Java
    Replies: 4
    Last Post: 10-09-2008, 05:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •