[英]how do I filter what I entered in the JTextField and display the filtered result?
package javaisnotbannana;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
public class Javaisnotbannana{
public static void main(String[] args) {
window();
}
////////////////////////////////////////////////////
public static void window()
{
JFrame window= new JFrame();
JPanel jp = new JPanel();
JLabel jl = new JLabel();
JTextField jt = new JTextField(30);
JButton jb = new JButton("Enter");
window.setTitle("ThisisTitleofWindow");
window.setVisible(true);
window.setSize(500, 500);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//textfield
jp.add(jt);
jt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String inoutt = jt.getText();
jl.setText(inoutt);
}
});
Why dose this lower section have the problem "Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range:(how ever many characters i entered)".str is receiving what is typed in the Jtextfield and im trying to filter the input to give a different output. 为什么在此下半部分遇到问题“线程“ AWT-EventQueue-0”中的异常java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:(我输入了多少个字符)”。str正在接收Jtextfield中键入的内容我试图过滤输入以提供不同的输出。 With out a filter it works fine like above just press enter,but when i try to press the button i and filter i get an error.
没有过滤器,它像上面一样工作正常,只需按Enter键,但是当我尝试按按钮i并过滤器时出现错误。
//button
jp.add(jb);
jb.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String str = jt.getText();
String text="";
int A=0;
int B=1;
int C=2;
for(int num=0;num<=str.length()/3;num++)
{
if (str.charAt(A) == 'T'&&str.charAt(B) == 'A'&&str.charAt(C)=='S')
{
text+="smell tasty";
}
else if(str.charAt(A) == 'B'&&str.charAt(B) == 'A'&&str.charAt(C)=='N')
{
text+="bannanas";
}
A+=3;
B+=3;
C+=3;
}
jl.setText(text);
}
});
jp.add(jl);
window.add(jp);
}
}
your index is overshooting use this instead 您的指数超调,请改用
for(int num=0;num<str.length()/3;num++)
just < not <= 只是<不是<=
The problem seems to the fact that you are overthinking the it. 问题似乎在于您对它的想法过多。 Remember, Java
String
s are zero indexed so num<=str.length()/3
should be something more like num < str.length()/3
请记住,Java
String
的索引为零,因此num<=str.length()/3
应该更像num < str.length()/3
But, having said that, the whole thing seems woefully inefficient (not to mention confusing) 但是,话虽如此,整件事似乎效率低下(更不用说混淆了)
You could use something like... 您可以使用类似...
for (int num = 0; num + 3 < str.length(); num++) {
String test = str.substring(num, 3);
if ("tas".equalsIgnoreCase(test)) {
text = "smell tasty";
break;
} else if ("ban".equalsIgnoreCase(test)) {
text = "bannanas";
break;
}
}
But even that's a long way around an otherwise simple problem. 但这对于解决本来很简单的问题还是很遥远的。
Instead, you could simply use something like... 相反,您可以简单地使用类似...
String str = "tas";
String text = "";
if (str.toLowerCase().contains("tas")) {
text = "smell tasty";
} else if (str.toLowerCase().contains("ban")) {
text = "bannanas";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.