怎么使用文本框数组在面板上添加多个文本框,为什么下面这个添加方式不对啊
JPanel p1=new JPanel();
int n=10;
JTextField t[]=new JTextField[n];
int m;
for(m=0;m<t.length;m++)
{
p2.add(t[m]);
}
p2是不是木有设置Layout,显示出来的结果应该只有一个。如果没有设置layout的时候,用p2加的时候要设置坐标,否则会出错
在自己的那个里面设置Layout了啊
你贴出来的代码太少了,不能够说明你的问题....
请贴出全部代码.....
建议参考:http://www.cnblogs.com/hongten/archive/2012/06/02/java_alarm.html
这里有关于swing的一些东东.....
希望对你有帮助...
大家一起学习,共同进步.
package 裁判评分; import java.awt.*; import java.awt.event.*;
import javax.swing.*;
//import 关于界面20121026.QueryFrame;
public class caipanJFrame extends JFrame implements ActionListener,WindowListener { JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JLabel b1=new JLabel("裁判人数"); TextField t1=new TextField("",7); TextField t2=new TextField("",7); JButton button1=new JButton("平均分"); JButton button2=new JButton("添加"); public caipanJFrame() { super("模拟裁判评分"); //this.setBounds(280, 100,1000, 1500); this.setSize(300, 300); this.setBackground(Color.lightGray); //this.setLayout(new GridLayout(3,1)); this.add(p1,BorderLayout.NORTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //p1.setLayout(new GridLayout(1,3)); p1.add(b1); p1.add(t1); p1.add(button2); this.add(p2,BorderLayout.CENTER); //this.add(p3); button2.addActionListener(this); p2.setLayout(new FlowLayout()); //p3.setLayout(new GridLayout(1,2)); p3.add(button1); p3.add(t2); this.add(p3,BorderLayout.SOUTH); button1.addActionListener(this); this.addWindowListener(this);
this.setVisible(true); } /*public int caipanshu() { int n; String str; str=t1.getText(); n=Integer.parseInt(str); return n; }*/ public int zhuanhuan(String str) { int m; m=Integer.parseInt(str); return m; } public void actionPerformed(ActionEvent e) { int n; String str; str=t1.getText(); n=Integer.parseInt(str); JTextField t[]=new JTextField[n]; if (e.getSource()==button2) { int m; for(m=0;m<t.length;m++) { p2.add(t[m]); } } if (e.getSource()==button1) { int i; int zongzhi=0; String r; for(i=0;i<n;i++) { String s; s=t[i].getText(); zongzhi+=zhuanhuan(s); } zongzhi=zongzhi/n; r=String.valueOf(zongzhi); t2.setText(r);
} } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String arg[]) { new caipanJFrame(); }
}
package 裁判评分; import java.awt.*; import java.awt.event.*;
import javax.swing.*;
//import 关于界面20121026.QueryFrame;
public class caipanJFrame extends JFrame implements ActionListener,WindowListener { JPanel p1=new JPanel(); JPanel p2=new JPanel(); JPanel p3=new JPanel(); JLabel b1=new JLabel("裁判人数"); TextField t1=new TextField("",7); TextField t2=new TextField("",7); JButton button1=new JButton("平均分"); JButton button2=new JButton("添加"); public caipanJFrame() { super("模拟裁判评分"); //this.setBounds(280, 100,1000, 1500); this.setSize(300, 300); this.setBackground(Color.lightGray); //this.setLayout(new GridLayout(3,1)); this.add(p1,BorderLayout.NORTH); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //p1.setLayout(new GridLayout(1,3)); p1.add(b1); p1.add(t1); p1.add(button2); this.add(p2,BorderLayout.CENTER); //this.add(p3); button2.addActionListener(this); p2.setLayout(new FlowLayout()); //p3.setLayout(new GridLayout(1,2)); p3.add(button1); p3.add(t2); this.add(p3,BorderLayout.SOUTH); button1.addActionListener(this); this.addWindowListener(this);
this.setVisible(true); } /*public int caipanshu() { int n; String str; str=t1.getText(); n=Integer.parseInt(str); return n; }*/ public int zhuanhuan(String str) { int m; m=Integer.parseInt(str); return m; } public void actionPerformed(ActionEvent e) { int n; String str; str=t1.getText(); n=Integer.parseInt(str); JTextField t[]=new JTextField[n]; if (e.getSource()==button2) { int m; for(m=0;m<t.length;m++) { p2.add(t[m]); } } if (e.getSource()==button1) { int i; int zongzhi=0; String r; for(i=0;i<n;i++) { String s; s=t[i].getText(); zongzhi+=zhuanhuan(s); } zongzhi=zongzhi/n; r=String.valueOf(zongzhi); t2.setText(r);
} } public void windowClosing(WindowEvent e) { System.exit(0); } public static void main(String arg[]) { new caipanJFrame(); }
}