Java中GridLayout如何3行2列?
--------------------编程问答-------------------- 你这个代码要实现什么功能啊?
import java.awt.*;
public class Test{
public static void main(String args[]){
Frame fr = new Frame();
fr.setLayout(new GridLayout(3,2));
Label info1 = new Label("名称:");
Label info2 = new Label("地址:");
TextField i1 = new TextField(10);
TextField i2 = new TextField(10);
Button b1 = new Button("保存");
Panel panel = new Panel();
panel.add(info1);
panel.add(i1);
panel.add(info2);
panel.add(i2);
panel.add(b1);
fr.add(panel);
fr.setSize(400,300);
fr.setVisible(true);
}
}
猜你的意思 应该是要实现这样的功能
其余功能还很不全 你再完善
import java.awt.*;--------------------编程问答-------------------- Panel main = new Panel(new BordLayout());
import javax.swing.*;
public class Test extends JFrame{
public static void main(String args[]){
Frame fr = new Frame();
fr.setLayout(new GridLayout(3,1));
JPanel panel1=new JPanel();
JPanel panel2=new JPanel();
JPanel panel3=new JPanel();
Label info1 = new Label("名称:");
Label info2 = new Label("地址:");
TextField i1 = new TextField(10);
TextField i2 = new TextField(10);
Button b1 = new Button("保存");
panel1.add(info1);
panel1.add(i1);
panel2.add(info2);
panel2.add(i2);
panel3.add(b1);
fr.add(panel1);
fr.add(panel2);
fr.add(panel3);
fr.setSize(400,300);
fr.setVisible(true);
}
}
Panel content = new Panel(new GridLayout(2,2,5,5));
Label info1 = new Label("名称:");
Label info2 = new Label("地址:");
TextField i1 = new TextField(10);
TextField i2 = new TextField(10);
content.add(info1);
content.add(i1);
content.add(info2);
content.add(i2);
main.add(content,BorderLayout.CENTER);
Button b1 = new Button("保存");
main.add(b1,BorderLayout.SOUTH); --------------------编程问答-------------------- 感谢楼上二位回复,我是初学者,为何我设置“GridLayout(3,2)”,但结果显示不是三行两列的呢? --------------------编程问答-------------------- Frame fr = new Frame();
JPanel panel1=new JPanel(new GridLayout(3,2));
Label info1 = new Label("名称:");
Label info2 = new Label("地址:");
TextField i1 = new TextField(10);
TextField i2 = new TextField(10);
Button b1 = new Button("保存");
panel1.add(info1);
panel1.add(i1);
panel1.add(info2);
panel1.add(i2);
panel1.add(b1);
fr.add(panel1);
fr.setSize(400,300);
fr.setVisible(true);
--------------------编程问答-------------------- Frame fr = new Frame();
JPanel panel1=new JPanel(new GridLayout(3,2));
Label info1 = new Label("名称:");
Label info2 = new Label("地址:");
TextField i1 = new TextField(10);
TextField i2 = new TextField(10);
Button b1 = new Button("保存");
panel1.add(info1);
panel1.add(i1);
panel1.add(info2);
panel1.add(i2);
panel1.add(b1);
fr.add(panel1);
fr.setSize(400,300);
fr.setVisible(true);
--------------------编程问答-------------------- Frame fr = new Frame();
fr.setLayout(new GridLayout(3,1));
这个是没用的,不是这样设置
应该这样
JPanel panel1=new JPanel(new GridLayout(3,1));
然后,把那些组件加到这个面板上。
--------------------编程问答-------------------- 其实组件不是直接加到JFrame 上的,是加到里面的contentPane上的 --------------------编程问答--------------------
因为你只放了五个上去。 --------------------编程问答-------------------- 为何输入框和按钮都特别的大?
--------------------编程问答-------------------- GridLayout会强制所有组件同一大小。 --------------------编程问答-------------------- OK.学习了。那么new GridLayout(3,3,2,2)? --------------------编程问答--------------------
import java.awt.*;
public class Test{
public static void main(String args[]){
Frame fr = new Frame();
Label info1 = new Label("名称:");
Label info2 = new Label("地址:");
TextField i1 = new TextField(10);
TextField i2 = new TextField(10);
Button b1 = new Button("保存");
Panel panel = new Panel();
panel.setLayout(new GridLayout(0,2));
panel.add(info1);
panel.add(i1);
panel.add(info2);
panel.add(i2);
panel.add(b1);
fr.add(panel);
fr.setSize(450,300);
fr.setVisible(true);
}
}
3行3列间隙2
补充:Java , Java SE