gpt4 book ai didi

java - 使用默认布局将 JPanel 添加到 JFrame

转载 作者:行者123 更新时间:2023-11-30 09:24:50 25 4
gpt4 key购买 nike

很快你就可以阅读我的代码并看到我把它作为图片输出

/*
* Suppose I have 4 buttons vertically on the right hand side in First PAnel
* and 4 buttons on bottom horizantally in second Panel
* and 4 text fiedls in the center in 4 rows in third Panel
* Using Frame's default border
*/
JPanel p1= new JPanel();
for (int i = 0; i < right.length; i++) {
right[i]=new JButton("right "+(i+1));
p1.add(right[i]);
}
JPanel p2 = new JPanel();
for (int i = 0; i < down.length; i++) {
down[i] = new JButton("Down "+(i+1));
p2.add(down[i]);
}

JPanel p3=new JPanel();
for(int i = 0 ; i<text.length;i++){
text[i]=new JTextField(30);
p3.add(text[i]);
}
Container c =getContentPane();
c.add(p1,"East");
c.add(p2,"South");
c.add(p3,"Center");
setSize(300,400);
setVisible(true);
setDefaultCloseOperation(3);

输出: enter image description here

我想做成这样 enter image description here

注意第二个输出我使用了Null布局和setBounds方法

有什么建议吗?

最佳答案

为右侧和底部的按钮创建面板 - 使用 FlowLayout为他们。为标签和文本字段创建另一个面板并使用 GridLayoutGridBagLayout .

关于java - 使用默认布局将 JPanel 添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506029/

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