how to put two jpanels side by side

别说谁变了你拦得住时间么 提交于 2019-12-01 19:36:37

If I understand your question correctly, you want to place 2 JPanel side by side. Look at the Layout GridLayout.

You'd want setLayout(new GridLayout(1,2)); which says 1 Row, 2 Col


package SSCCE;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SideBYSide extends JFrame{

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

    public SideBYSide(){
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(100, 75);
        this.setLayout(new BorderLayout());
        this.setVisible(true);

        JPanel container = new JPanel();
        JPanel panelOne = new JPanel();
        JPanel panelTwo = new JPanel();

        panelOne.add(new JLabel("1"));
        panelTwo.add(new JLabel("2"));

        container.setLayout(new GridLayout(1,2));
        container.add(panelOne);
        container.add(panelTwo);

        this.add(container);
    }

 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!