首页 新闻 会员 周边

java 界面文本框添加

0
[已解决问题] 解决于 2012-11-14 12:38

怎么使用文本框数组在面板上添加多个文本框,为什么下面这个添加方式不对啊

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]); 
      
   }

左手、右手的主页 左手、右手 | 初学一级 | 园豆:78
提问于:2012-11-11 20:24
< >
分享
最佳答案
0

p2是不是木有设置Layout,显示出来的结果应该只有一个。如果没有设置layout的时候,用p2加的时候要设置坐标,否则会出错

奖励园豆:5
上兵伐谋 | 小虾三级 |园豆:547 | 2012-11-12 12:26

在自己的那个里面设置Layout了啊

左手、右手 | 园豆:78 (初学一级) | 2012-11-12 17:39
其他回答(1)
0

你贴出来的代码太少了,不能够说明你的问题....

请贴出全部代码.....

建议参考:http://www.cnblogs.com/hongten/archive/2012/06/02/java_alarm.html

这里有关于swing的一些东东.....

希望对你有帮助...

hongtenzone@foxmail.com

大家一起学习,共同进步.

Hongten | 园豆:1768 (小虾三级) | 2012-11-11 23:15

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();  }

  }

支持(0) 反对(0) 左手、右手 | 园豆:78 (初学一级) | 2012-11-12 15:42

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();  }

  }

支持(0) 反对(0) 左手、右手 | 园豆:78 (初学一级) | 2012-11-12 15:51
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册