指定java小程序中文本字段的范围
这是我发布的代码:指定java小程序中文本字段的范围,java,applet,textfield,operator-keyword,relational,Java,Applet,Textfield,Operator Keyword,Relational,这是我发布的代码: import java.awt.*; import java.awt.event.*; import java.applet.*; /* <applet code="front" width=500 height=500></applet> */ public class front extends Applet implements ActionListener { String msg="";
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="front" width=500 height=500></applet> */
public class front extends Applet implements ActionListener {
String msg="";
TextArea text,text1;
TextField txt;
Button load, enter;
public void init() {
enter=new Button("Enter");
load=new Button("Load");
txt=new TextField(5);
text=new TextArea(10,15);
add(load);
add(text);
add(txt);
add(enter);
load.addActionListener(this);
txt.addActionListener(this);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Load")) {
msg = "You pressed Load";
} else {
if(txt.getText().toString().equals ("6")) {
msg="Set the text for 6";
text.setText("Text");
} else {
msg="Invalid number";
text.setText("");
}
}
repaint();
}
public void paint(Graphics g) {
g.drawString(msg,350,250);
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.applet.*;
/* */
公共类前端扩展小程序实现ActionListener{
字符串msg=“”;
text区域文本,text1;
文本字段txt;
按钮加载,输入;
公共void init(){
输入=新按钮(“输入”);
加载=新按钮(“加载”);
txt=新文本字段(5);
文本=新文本区域(10,15);
添加(加载);
添加(文本);
添加(txt);
添加(输入);
load.addActionListener(这个);
txt.addActionListener(这个);
输入.addActionListener(this);
}
已执行的公共无效行动(行动事件ae)
{
字符串str=ae.getActionCommand();
如果(str.equals(“Load”)){
msg=“您按下了加载键”;
}否则{
如果(txt.getText().toString().equals(“6”)){
msg=“将文本设置为6”;
text.setText(“文本”);
}否则{
msg=“无效号码”;
text.setText(“”);
}
}
重新油漆();
}
公共空间涂料(图g){
g、 抽绳(味精350250);
}
}
如您所见,当textfield中的值等于6时,它会显示一条消息。但是现在我希望msg只在5-6范围内显示。所以我尝试了以下代码
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <applet code="front" width=500 height=500></applet> */
public class front extends Applet implements ActionListener {
String msg="";
TextArea text,text1;
TextField txt;
Button load, enter;
public void init() {
enter=new Button("Enter");
load=new Button("Load");
txt=new TextField(5);
text=new TextArea(10,15);
add(load);
add(text);
add(txt);
add(enter);
load.addActionListener(this);
txt.addActionListener(this);
enter.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
String str = ae.getActionCommand();
if(str.equals("Load")) {
msg = "You pressed Load";
} else {
String a = txt.getText();
int a1=Integer.parseInt(a); //I also used Integer.valueOf(a)
if(a1>="5"&&a1<="6")
{
msg="Set the text";
text.setText("Text");
} else {
msg="Invalid number";
text.setText("");
}
}
repaint();
}
public void paint(Graphics g) {
g.drawString(msg,350,250);
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.applet.*;
/* */
公共类前端扩展小程序实现ActionListener{
字符串msg=“”;
text区域文本,text1;
文本字段txt;
按钮加载,输入;
公共void init(){
输入=新按钮(“输入”);
加载=新按钮(“加载”);
txt=新文本字段(5);
文本=新文本区域(10,15);
添加(加载);
添加(文本);
添加(txt);
添加(输入);
load.addActionListener(这个);
txt.addActionListener(这个);
输入.addActionListener(this);
}
已执行的公共无效行动(行动事件ae)
{
字符串str=ae.getActionCommand();
如果(str.equals(“Load”)){
msg=“您按下了加载键”;
}否则{
字符串a=txt.getText();
int a1=Integer.parseInt(a);//我还使用了Integer.valueOf(a)
如果(a1>=“5”&&a1=不能应用于int,则为java.lang.String
运算符尝试将int与字符串值进行比较
if(a1>="5"&&a1<="6") // 5 and 6 are string representation whereas a1 is int
如果(a1>=“5”&&a1=5&&a1请编辑您的帖子,我看不懂!您可以使用JSpinner来代替。它将使您能够指定最小/最大范围并执行大部分验证本身
if(a1>=5 && a1<=6) // 5 and 6 are int representation