Java 如何使此文本字段只有一个正确的代码作为输入?
我想知道如何使我的程序只接受1101101作为JTextField txtCode上唯一正确的输入。目前,我可以在文本字段中输入任何代码,它将显示相同的输出 代码如下:Java 如何使此文本字段只有一个正确的代码作为输入?,java,text,input,field,output,Java,Text,Input,Field,Output,我想知道如何使我的程序只接受1101101作为JTextField txtCode上唯一正确的输入。目前,我可以在文本字段中输入任何代码,它将显示相同的输出 代码如下: import java.awt.FlowLayout; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swi
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
@SuppressWarnings("serial")
public class GUI extends JFrame {
JLabel coffeeProgram;
JButton button;
JTextField txtCode;
JTextField price;
JTextField description;
JLabel outputPrice;
public GUI() {
setLayout(new FlowLayout());
coffeeProgram = new JLabel("Enter Code Here:");
add(coffeeProgram);
txtCode = new JTextField(15);
add(txtCode);
button = new JButton("Submit Code");
add(button);
outputPrice = new JLabel(" Price: ");
add(outputPrice);
JTextField price;
CoffeeReturn objCoffee = new CoffeeReturn();
double myPrice = objCoffee.CoffeeCode(txtCode.getText());
price = new JTextField(15);
add(price);
price.setText("Price is"+myPrice);
}
}
我很抱歉,如果这个代码似乎有点缺乏经验。我15岁,两天前开始尝试学习java。
这是我的程序截图:看一看
它解释了如何使用格式只允许特定字符。我想您需要更改数据类型才能成功完成您正在尝试的操作 参考:
您可能会使用整数或字符串值以您想要的方式进行比较 谢谢!我去看看。