sách gpt4 ai đã đi

java - JPanel 中的 JPanel

In lại 作者:行者123 更新时间:2023-12-01 14:14:49 28 4
mua khóa gpt4 Nike

我在使用 Java JPanel 时遇到问题。我想将 2 个具有不同布局的 JPanel 放入一个也有布局的 JPanel 中。有可能让它发挥作用吗?

    BibP()
setLayout(new GridLayout(5, 1)); //The big JPanel
add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));

A 和 B 都是作为 JPanel 扩展的类,无论我更改或注释什么,B 总是出现在 1 行中。我向 A 添加了 4 个元素,向 B 添加了 14 个元素(JLabels 和 JTextAreas),其中没有太多代码,只有添加和一些计算。

问题可能出在我试图放置大 JPanel 的 JFrame 中。

    JFrame.this.add(new BigP(),BorderLayout.CENTER);    

biên tập:

    public class BigP extends JPanel{
//Labels and TextAres

public class A extends JPanel{
public A(){
setBorder(new EmptyBorder(0, -50, 0, 0));
//get date and add to textareas
//add the label and textareas
}
}

public class B extends JPanel{
public B (){
setBorder(new EmptyBorder(0, -50, 0, 0));
setBackground(Color.red);
//colum longs for text areas less then 5
//add labels and textareas
}
}

public BigP(){
setLayout(new GridLayout(5, 1));
setBorder(new EmptyBorder(3,-160,0,0));

add(new A(), new FlowLayout(4));
add(new B(), new GridLayout(7,2));
}
}

感谢您的帮助。

经过几次尝试:

如果我使用这个:

    add(new B(), new GridLayout(7,2));

当我打印布局时,我在 B 中得到了这个:

java.awt.FlowLayout[hgap=5,vgap=5,align=center]

如果我在B中设置布局:

    setLayout(new GridLayout(7, 2));

信息正确:

java.awt.GridLayout[hgap=0,vgap=0,rows=7,cols=2]

但是只有 2 个 JTextAreas 可见,而本应有 14 个元素。

1 Câu trả lời

Is it even possible to make it work?

是的,它通常被称为复合布局。

您遇到的问题是您正在尝试提供 Layout 作为当前容器布局管理器的约束(在您的情况下,幸运的是它没有期待任何)...

相反,直接向子面板提供布局管理器...类似...

setLayout(new GridLayout(5, 1)); //The big JPanel

JPanel a = new JPanel(new FlowLayout(4));
JPanel b = new JPanel(new GridLayout(7,2));
add(a);
add(b);

看看Laying Out Components Within a Container了解更多详情...

用示例更新

简单的可运行示例,使用标签作为占位符。

nhập mô tả hình ảnh ở đây

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;

public class ExampleLayout {

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

public ExampleLayout() {
EventQueue.invokeLater(new Runnable() {
@Ghi đè
công khai void run() {
thử {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}

JPanel mainPane = new JPanel(new GridLayout(5, 1));
mainPane.add(new APanel());
mainPane.add(new BPanel());

JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPane);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}

public class APanel extends JPanel {

public APanel() {
setLayout(new FlowLayout(4));
for (int index = 0; index < 4; index++) {
add(new JLabel(Integer.toString(index)));
}
setBorder(new LineBorder(Color.RED));
}

}

public class BPanel extends JPanel {

public BPanel() {
setLayout(new GridLayout(7, 2));
for (int index = 0; index < 14; index++) {
add(new JLabel(Integer.toString(index)));
}
setBorder(new LineBorder(Color.BLUE));
}

}

}

这个setBorder(new EmptyBorder(3,-160,0,0));看起来也是错误的。 EmptyBorder 永远不应该有负值

关于java - JPanel 中的 JPanel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18202415/

28 4 0
Bài viết được đề xuất: java - 如何在 WebSphere 之外正确存储 Jar 库?
Bài viết được đề xuất: r - 使用 ggplot2 为原生圆形图增添趣味
Bài viết được đề xuất: php gRPC 连接
Bài viết được đề xuất: makefile - 让 CMake 声明一个目标虚假
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com