package com.Exa;
import java.awt.Container;
import java.awt.event.*;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
public class Example9_10 extends JFrame
{
private JButton button=new JButton("1");
public Example9_10(String title)
{
super(title);
final JTextField tf=new JTextField();
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
int cont=Integer.parseInt(tf.getText());

}
});
Container c=getContentPane();
c.add(tf);
c.add(button);
setSize(500,500);
setVisible(true);

}
public static void main(String args[])
{
new Example9_10("监听器");
}
}我想单击button的时候 把它的名字送到tf中  该怎么弄啊 这是以上写的代码 有点问题  

解决方案 »

  1.   

                    int cont=Integer.parseInt(tf.getText());
    这句有错误。tf中你没有赋值,所以tf.getText()的值为null;
      

  2.   

    关键是他现在图形用户界面都不显示JTextField 这一个选框啊????
    一点按钮就提示出错
      

  3.   

    把谁的名字送到jf中? button的名字吗,你是说button按钮上的显示的文本送到jf中吗?
      

  4.   

    import java.awt.event.*;
    import java.awt.*;///////////import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    public class Example9_10 extends JFrame
    {
        private JButton button=new JButton("1");
        public Example9_10(String title)
        {
            super(title);
            final JTextField tf=new JTextField(30);///////////////
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    //int cont=Integer.parseInt(tf.getText());
                    tf.setText(button.getText());//////////
                }
            });
            Container c=getContentPane();
            c.setLayout(new FlowLayout());////////////
            c.add(tf);
            c.add(button);
            setSize(500,500);
            setVisible(true);    }
        public static void main(String args[])
        {
            new Example9_10("监听器");
        }
    }
      

  5.   

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.awt.event.*;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    public class Example9_10 extends JFrame
    {
        private JButton button=new JButton("1");
        public Example9_10(String title)
        {
            super(title);
            final JTextField tf=new JTextField("显示名字的地方");
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    int cont=Integer.parseInt(tf.getText());
                    
                }
            });
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
            this.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 0));
            this.add(tf);
            this.add(button);
            setSize(300,300);
            setVisible(true);
            
        }
        public static void main(String args[])
        {
            new Example9_10("监听器");
        }
    }不知道你要的什么效果,给你改成这样自己去实现吧。
      

  6.   

    import java.awt.Container;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    public class ArrayWay extends JFrame
    {
        private JButton button=new JButton("1");
        final JTextField tf=new JTextField(30);
        public ArrayWay(String title)
        {
            super(title);
            this.setLayout(new FlowLayout());
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    tf.setText(e.getActionCommand());
                 //int cont=Integer.parseInt(tf.getText());
                    
                }
            });
            Container c=getContentPane();
            c.add(tf);
            c.add(button);
            setSize(500,500);
            setVisible(true);
            
        }
        public static void main(String args[])
        {
            new ArrayWay("监听器");
        }
    }
      

  7.   

    你先写:import java.awt.*;
    然后在构造方法中加入:this.setLayout(new FlowLayout());然后设置JTextField的大小就可以了。
      

  8.   

    你这个 用getName() 这个方法 你的定义一个setName()的方法吧???
    要不好想什么也不输出的
      

  9.   

    mport java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Container;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JTextField;
    public class Example9_10 extends JFrame
    {
        private JButton button=new JButton("JButton");
        public Example9_10(String title)
        {
            super(title);
            final JTextField tf=new JTextField("JTextField");
            tf.setSize(500, 100);
            tf.setBackground(Color.white);
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    tf.setText(button.getText());
                }
            });
            Container c=getContentPane();
            c.setLayout(new BorderLayout());
            c.add(tf,BorderLayout.NORTH);
            c.add(button,BorderLayout.SOUTH);
            setSize(500,500);
            setVisible(true);
            
        }
        public static void main(String args[])
        {
            new Example9_10("监听器");
        }
    }是要这样子的吗?
      

  10.   

    你想把JButton的名字(也就是Jbutton上的文本)放到jf中..可以这样做
    1,你先获得Jbutton上的文本,用String str=button.getText(); 这样str="1";
    2。设置tf上的文本,用tf.setText(str);
    另外你的布局有点问题,默认的布局是BorderLayout的,你添加了两个组件
    c.add(tf);//默认的放中间
    c.add(button);//这个也是默认的放中间,这样button就把tf覆盖了
    把你的代码改了下,可以得到你要的效果了public class Example9_10 extends JFrame
    {
        private JButton button=new JButton("1");
        public Example9_10(String title)
        {
            super(title);
            final JTextField tf=new JTextField();
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent evt){
                    String str=button.getText();//获取button上的文本"1"
                    tf.setText(str);//将tf的内容设置为"1";
                }
            });
            Container c=getContentPane();
            c.add(tf,BorderLayout.SOUTH);//将tf放在下面,注意看下面tf中的内容变化
            c.add(button,BorderLayout.NORTH);//将button放在上面
            setSize(500,500);
            setVisible(true);
            
        }
        public static void main(String args[])
        {
            new Example9_10("监听器");
        }
    }