帮忙看看下面swing里面的jlable和jtextfield为什么只显示一个 如图
import java.awt.*;
import javax.swing.*;
public class Tianjia extends JFrame{
JLabel []jlabels;
JPanel []jpanels;
JPanel jp_center;
JPanel jPanel7;
JTextField []jTextField;
JButton jButton1,jButton2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Tianjia tianjia=new Tianjia();
}
public Tianjia(){
jp_center=new JPanel();
jp_center.setLayout(new GridLayout(6, 2));
jlabels=new JLabel[]{
new JLabel("学号"),
new JLabel("姓名"),
new JLabel("性别"),
new JLabel("年龄"),
new JLabel("系别"),
new JLabel("籍贯")
};
//
//
int size=6;
jpanels=new JPanel[size];
for(int i=0;i<jpanels.length;i++)
{
jpanels[i]=new JPanel();
}
jPanel7=new JPanel();
jTextField=new JTextField[size];
for(int i=0;i<jTextField.length;i++)
{
jTextField[i]=new JTextField(10);
}
//jTextField=new JTextField(8);
jButton1=new JButton("添加");
jButton2=new JButton("取消");
for(int i=0;i<jpanels.length;i++){
jpanels[i].add(jlabels[i],BorderLayout.CENTER);
jpanels[i].add(jTextField[i],BorderLayout.CENTER);
}
for (int j = 0; j <jpanels.length; j++) {
this.add(jpanels[j]);
}
jPanel7.add(jButton1);
jPanel7.add(jButton2);
this.add(jPanel7,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(50,250,300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("添加学生信息");
}
} 展开
package com.jee;
import java.awt.*;
import javax.swing.*;
public class Tianjia extends JFrame{
JLabel []jlabels;
JPanel []jpanels;
JPanel jp_center;
JPanel jPanel7;
JTextField []jTextField;
JButton jButton1,jButton2;
public static void main(String[] args) {
// TODO Auto-generated method stub
Tianjia tianjia=new Tianjia();
}
public Tianjia(){
jp_center=new JPanel();
jp_center.setLayout(new GridLayout(6, 2));
jlabels=new JLabel[]{
new JLabel("学号"),
new JLabel("姓名"),
new JLabel("性别"),
new JLabel("年龄"),
new JLabel("系别"),
new JLabel("籍贯")
};
//
//
int size=6;
jpanels=new JPanel[size];
for(int i=0;i<jpanels.length;i++)
{
jpanels[i]=new JPanel();
}
jPanel7=new JPanel();
jTextField=new JTextField[size];
for(int i=0;i<jTextField.length;i++)
{
jTextField[i]=new JTextField(10);
}
//jTextField=new JTextField(8);
jButton1=new JButton("添加");
jButton2=new JButton("取消");
for(int i=0;i<jpanels.length;i++){
jpanels[i].add(jlabels[i],BorderLayout.CENTER);
jpanels[i].add(jTextField[i],BorderLayout.CENTER);
}
/**
*这里加了一句,Jframe默认是border布局,现在设置为flow布局,当然你可以设置为其他布局
*/
this.setLayout(new FlowLayout());
for (int j = 0; j <jpanels.length; j++) {
this.add(jpanels[j]);
}
jPanel7.add(jButton1);
jPanel7.add(jButton2);
this.add(jPanel7,BorderLayout.SOUTH);
this.setVisible(true);
this.setBounds(50,250,300,200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("添加学生信息");
}
}