从Java NetBeans中的某些JtextField删除空格 [英] Remove spaces from certain JtextField in java netbeans

查看:137
本文介绍了从Java NetBeans中的某些JtextField删除空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从JtextField中删除空格,因此当用户单击按钮时,它将自动从他/她编写的文本中删除空格.

I want to remove spaces from a JtextField, so when the user click the button it automatically removes the space from the text he/she wrote.

推荐答案

简单来说,您需要向用户要单击的按钮添加一个动作侦听器. 例如:该按钮将用于发布内容. "POST"

simply, you need to add an action listener to the button which the user is going to click. for example: the button will be for posting something. "POST"

public class YourProject extends JFrame implements ActionListener{

JtextField text = new JtextField();
JButton post = new JButton("POST");

public YourProject(){

add(text);
add(post);
post.addactionlistener(this);
setVisible(true);

}



 @Override
public void actionPerformed(ActionEvent e) {

if(e.getSource()==post) {

String removed = text.getText().trim();

System.out.println(removed);

}

如果用户输入"Hello World",然后单击发布",则输出将为"HelloWorld". 希望这会有所帮助.

If the user writes "Hello World" then clicks post, the output will be "HelloWorld". hope this helps.

这篇关于从Java NetBeans中的某些JtextField删除空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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