how to put two jpanels side by side

后端 未结 1 1820
眼角桃花
眼角桃花 2021-01-13 16:24

I try to put two jpanels side by side, but in this moment i can not do what i want I have this code,

this.videoPanel= new JPanel();
this.videoPanel.setBackgr         


        
相关标签:
1条回答
  • 2021-01-13 17:02

    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);
        }
    
     }
    
    0 讨论(0)
提交回复
热议问题