我想在单击按钮时显示文本字段中键入的内容 [英] I want to display what is typed in the textfield when a button is clicked

查看:71
本文介绍了我想在单击按钮时显示文本字段中键入的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在点击按钮时显示文本字段中输入的内容



我尝试过的内容:



i want to display what is typed in the textfield when a button is clicked

What I have tried:

import java.awt.*;
import java.awt.event.*;

public class Q7 {
    Q7(){
       Frame f= new Frame("Calculator");
       TextField txt1 = new TextField(15);
       Button b = new Button("Submit");
       Label lbl = new Label();
       
       b.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
               txt1.addItemListener(new ItemListener(){
                   @Override
                   public void itemStateChanged(ItemEvent e){
                       String s=txt1.getSelectedItem();
                        l2.setText(s);
                   }
               });
           }
       });
       
       f.addWindowListener(new WindowAdapter(){
           public void windowClosing(WindowEvent we){
            System.exit(0);
           }
       });
       f.add(txt1);
       f.add(b);
       f.add(lbl);
       
       f.setLayout(new FlowLayout());
       f.setSize(400,300);
       f.setVisible(true);
       
       
    }   
    public static void main(String[] args){
      Q7 f = new Q7();   
    }
}

推荐答案

l2.setText(s);



您还没有在任何地方定义l2。如果为变量使用了合适的有意义的名称,而不是1或2个字母,那么您的代码将更容易调试。


You have not defined l2 anywhere. Your code would be easier to debug if you used proper meaningful names for your variables, rather than 1 or 2 letters.


这篇关于我想在单击按钮时显示文本字段中键入的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆