如何在一个框架中添加两个具有不同布局的面板?

时间:2017-01-24 14:12:48

标签: java swing jframe layout-manager gridbaglayout

我有一个面板whit gridBagLayout,第二个是null gridlayout。当我将其添加到主面板,并且主面板到框架一个面板消失。这是为什么?以及如何在一个框架中添加两个具有不同布局设置的面板?

以下是代码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();

    }

}

第二课:

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);
     }

}

第三课:

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);
     }

}

所以我想第二类是在面板的中心,并且有gridBagLayout,第三类我想移动对象,所以这个类没有gridlayout ...当我和那两个面板到主窗格时第二类whit gridBagLayout不起作用。

1 个答案:

答案 0 :(得分:0)

您目前正在向JPanel添加两个JFrame

JFrame f = new JFrame();

默认情况下,此框架使用BorderLayout。因此,如果您多次致电f.add(new Panel());,则只会显示最后一个,因为此布局的center区域只能显示一个JComponent。您需要使用不同的布局。