如果我正确理解了您的问题,则希望并排放置2个JPanel。查看布局GridLayout。
您需要setLayout(new GridLayout(1,2)); 上面写着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条)