0

I have a JFrame and a JPanel, and I desire that the JPanel is shown in the JFrame.

I have written the following code:

public MainScreen() {
    initComponents();        
    setVisible(false);
    setExtendedState(JFrame.MAXIMIZED_BOTH);
    PnlButons pnlButons = new PnlButons();
    pnlButons.setSize(pnlButons.getHeight(),JFrame.MAXIMIZED_VERT);

    add(pnlButons,BorderLayout.WEST);
    setVisible(true);
    pack();
}

I tried many tutorials, but none of them helped and the JPanel isn't displayed.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Francesc Clopes
  • 353
  • 1
  • 7
  • 19

2 Answers2

0
import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Swing extends JFrame{

    Swing() {
    //    initComponents();        
        setVisible(false);
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        JButton pnlButons = new JButton();
        pnlButons.setSize(pnlButons.getHeight(),JFrame.MAXIMIZED_VERT);

        add(pnlButons,BorderLayout.WEST);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) {
        new Swing();
    }
}

and Here is the link to for your screen.

https://www.dropbox.com/s/wncelkzj9rwxlwj/SOVImage.png

Manish Singh
  • 310
  • 1
  • 5
  • 19
  • I just copied an pasted your code to see how it is behaving and I changed pnlButtons to JButton because I am not aware with pnlButtons.Its working,I was able to see a button on left – Manish Singh Dec 23 '13 at 16:40
  • I need to display a JPanel, that will contain buttons. I build the JPanel in grafical mode, but now I need that it will be displayed into the jframe parent. – Francesc Clopes Dec 23 '13 at 17:18
  • 1
    My appology I did'nt understand your graphical mode.But as far as I am getting,you want to show buttons which is attached to JPanel and this panel in turn is attached to JFRAME.If it is the case then we need to create number of JButton objects and add it on the JPanel by selecting desired Layout. – Manish Singh Dec 23 '13 at 17:36
0

set the size of your JPanel by setPreferredSize(new Dimension(width, height)

Muhammad
  • 6,725
  • 5
  • 47
  • 54
  • 2
    See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Dec 23 '13 at 19:44