gpt4 book ai didi

java - 如何并排放置两个jpanel

转载 作者:搜寻专家 更新时间:2023-11-01 01:43:55 27 4
gpt4 key购买 nike

我试着并排放置两个 jpanels,但此时我不能做我想做的事我有这个代码,

this.videoPanel= new JPanel();
this.videoPanel.setBackground(new Color(102, 102, 102));
this.videoPanel.setPreferredSize(new Dimension(320, 240));
this.videoPanel.setLayout(new BoxLayout(this.videoPanel, 1));

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);

layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(this.videoPanel, 10, 400, 500)));

layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(this.videoPanel, 10, 400, 500)));

pack();

`我尝试再放一个 jpanel:

 this.videoPanel1.setBackground(new Color(102, 102, 102));
this.videoPanel1.setPreferredSize(new Dimension(320, 240));
this.videoPanel1.setLayout(new BoxLayout(this.videoPanel1, 2));

GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);



layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(this.videoPanel1, 10, 400, 500)
.addComponent(this.videoPanel, 10, 400, 500)));

layout.setVerticalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(this.videoPanel1, 10, 400, 500)
.addComponent(this.videoPanel, 10, 400, 500)));
pack();

此代码不起作用,也不会给出错误。

最佳答案

如果我对你的问题的理解正确,你想并排放置 2 个 JPanel。查看布局 GridLayout。

你需要 setLayout(new GridLayout(1,2));上面写着 1 行,2 列


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

}

关于java - 如何并排放置两个jpanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17974011/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com