Results 1 to 4 of 4
Thread: JPanel on JFrame wierd problem
- 02-06-2012, 07:53 PM #1
random developer
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
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; } }
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.
- 02-06-2012, 08:04 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,638
- Rep Power
- 13
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)
- 02-06-2012, 08:05 PM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 785
- Rep Power
- 12
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); } }
- 02-06-2012, 08:30 PM #4
random developer
- Join Date
- Jan 2012
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
JPanel & JFrame Output Problem....... Can any one help?
By mahedi in forum New To JavaReplies: 2Last Post: 01-22-2011, 01:27 PM -
JFrame and JPanel
By khiat in forum New To JavaReplies: 6Last Post: 01-15-2011, 08:32 PM -
wierd NetBeans problem.
By edi.gotieb in forum Forum LobbyReplies: 2Last Post: 10-09-2010, 02:31 PM -
Wierd problem with abstract and extends
By g123456 in forum New To JavaReplies: 6Last Post: 05-14-2010, 07:25 PM -
wierd problem
By f_the_cook in forum Advanced JavaReplies: 4Last Post: 10-09-2008, 06:13 PM
Bookmarks