0

I am working in java with SWING and AWT components. I am confused about layouts in it. I have add a panel and add buttons inside that panel using setBounds(), then add this panel to a frame which is using flow layout. But the panel is not been able to display itself.. Here is my code:

public class MainWindow implements ActionListener {
    private JFrame frame;
    private JPanel panel;
    private JButton play;
    private JButton exit;

    public MainWindow() {
        frame = new JFrame();
        panel = new JPanel();

        play = new JButton(new ImageIcon("button.png"));
        exit = new JButton(new ImageIcon("exit.png"));

        panel.setLayout(null);
        play.setBounds(200, 200, 90, 40);
        exit.setBounds(200, 300, 90, 40);
        panel.setBackground(new Color(45, 204, 112));
        play.addActionListener(this);
        exit.addActionListener(this);
        panel.add(play);
        panel.add(exit);

        Container cont = frame.getContentPane();
        cont.setLayout(new FlowLayout());
        cont.add(panel);

        frame.setSize(800, 700);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Awais
  • 69
  • 1
  • 6
  • 2
    The solution is to 1) Avoid `null` layouts and `setBounds(...)` as this produces rigid GUI's that look terrible on all platforms other than your own and that are a witch to upgrade, enhance and debug (as you're finding out). 2) Read up on how to use the layout managers and then use them. You can find the tutorials here: [Layout Manager Tutorials](http://docs.oracle.com/javase/tutorial/uiswing/layout/) – Hovercraft Full Of Eels Mar 19 '15 at 17:38
  • 1
    Also, please check out the similar questions that can be found on this site: [site search](http://stackoverflow.com/search?q=%5Bswing%5D+layout+managers). – Hovercraft Full Of Eels Mar 19 '15 at 17:45

0 Answers0