0

I have one panel whit gridBagLayout and second with null gridlayout. When I add that to main panel, and main panel to frame one panel disappears. Why is that? And how to add two panels with different layouts setings in one frame?

Here is the code main @Beowolve:

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

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

public class PrikazGUI {

    JFrame frejm;
    JPanel k;
    JButton b1,b2;

    public PrikazGUI(){
        frejm = new JFrame("Lala");
        k = new JPanel();

        KvadratPravi p = new KvadratPravi();
        JPanel grid = new JPanel();
        grid.setLayout(new GridBagLayout());
        grid.add(p);
//      Kvadrat l = new Kvadrat();
        JosJedanKvadrat jos = new JosJedanKvadrat();
//      k.setLayout(null);
//      k.setBounds(0, 444,444, 445);
        k.add(jos);
        k.add(grid);
        JPanel main = new JPanel();
        main.setLayout(null);
        k.setBounds(0, 0,1000, 1900);
        main.setBounds(0, 0,1000, 1900);
        main.add(k);



        frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frejm.setSize(1900, 1000);
        frejm.getContentPane().add(main);
//      frejm.getContentPane().add(k);
//      frejm.pack();
        frejm.setVisible(true);


    }


    public static void main(String[] args) {
        PrikazGUI a = new PrikazGUI();

    }

}

Second class:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JPanel;

public class KvadratPravi extends JPanel {

    int sizeH = 60;
    int sizeW = 60;
    public GridBagConstraints cst = new GridBagConstraints();

    public KvadratPravi() {
        JPanel j = new JPanel();
        j.setLayout(new GridBagLayout());
         cst.gridx = 0;
         cst.gridy = 0;

         add(j,cst);

    }



    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
            g.setColor(Color.PINK);
           g.drawRect(0, 0, sizeH, sizeW);
           g.fillRect(0, 0, sizeH, sizeW);
    }

     @Override
     public Dimension getPreferredSize() {
         return new Dimension(sizeH,sizeW);
     }

}

Third class:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;

public class JosJedanKvadrat extends JPanel {

    int sizeH = 60;
    int sizeW = 60;
    int x,y;


     public JosJedanKvadrat() {

         setBounds(33, 44,444, 445);
         JPanel j = new JPanel();

         setLayout(null);

         add(j);
         addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                if(!e.isMetaDown()){
                x = e.getX();
                y = e.getY();
                }
                }
                });
                addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent e) {
                if(!e.isMetaDown()){
                Point p = getLocation();
                setLocation(p.x + e.getX() - x,
                p.y + e.getY() - y);
                }
                }
                });



    }



    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
            g.setColor(Color.BLUE);
           g.drawOval(0, 0, sizeH, sizeW);


    }

     @Override
     public Dimension getPreferredSize() {
         return new Dimension(sizeH,sizeW);
     }

}

So I whant second class be in center of panel,and to have gridBagLayout, and third class I whant to move around the objects,so that class dont have gridlayout...when I and that two panels to main pane its seems that second class whit gridBagLayout does not working.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
getXgetY
  • 61
  • 1
  • 9
  • Please add small sample code to show your problem. Your explanation of the problem is not very clear. – Beowolve Jan 24 '17 at 14:36
  • @Beowolve here is code now :) – getXgetY Jan 24 '17 at 15:23
  • 1
    Don't use a null layout. Swing was designed to be used with layout managers. Also, whenever you do custom painting you need to override the `getPreferredSize()` method of the component so the layout manager know what the desired size of the component is. Don't hardcode the size of the frame. The pack() method will size the frame based on the preferred size of the components added to the frame. – camickr Jan 24 '17 at 15:40

1 Answers1

0

You are currently adding two JPanel into a JFrame.

JFrame f = new JFrame();

This frame, by default, use a BorderLayout. So if you call f.add(new Panel()); multiple time only the last one will be visible since the center area of this layout can only show one JComponent. You need to use a different layout.

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • 1
    `You need to use a different layout.` - or add the second component to a different area of the BorderLayout. For example `add(component, BorderLayout.PAGE_END)`. – camickr Jan 24 '17 at 15:36
  • @camickr I preferred to used an other layout since I wasn't sure of the content of those panel. And I prefer to see the border layout to set borders content : menu, footer, headers. But I should have mention that. Well doesn't really matter anymore – AxelH Jan 24 '17 at 23:42