import java.awt.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import formatstring;public class MyApp extends JApplet
{
final JButton b1;
JTextField jtf=new JTextField(formatstring.f_string);
JPanel jp1;
public MyApp()
{
Container con=getContentPane();
JRootPane rootpane=getRootPane();
jtf.setDocument(new formatstring(jtf));
b1=new JButton("OK");
jp1=new JPanel();
rootpane.setDefaultButton(b1);
con.setLayout(new FlowLayout());
jp1.add(jtf);
con.add("Center",jp1);
con.add("South",b1);
b1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
// dlg_show(b1.getText());
}
});
b1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// dlg_show(b1.getText());
}
});
}
} import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
class formatstring extends PlainDocument
{
public static String f_string="000.000.000.000";
private static int s1=3,s2=7,s3=11;
private JTextComponent jtc;
private int newoffset;
int i=-1;
dlg dlg1;
public formatstring(JTextComponent jtf)
{
jtc=jtf;
try
{
insertString(0,f_string,null);
}
catch(Exception e)
{
}
}
public void insertString(int offset,String s,AttributeSet aset) throws BadLocationException
{
String asd;
int i;
if(s.equals(f_string))
{
super.insertString(offset,s,aset);
}
else
{
try
{
i=offset;
if ((i==0)||(i==3)||(i==7)||(i==11) )
{
if (Integer.parseInt(s)>2)
return;
}
else
{
if (Integer.parseInt(s)>5)
return;
}
Integer aa=new Integer(i);
// dlg1=new dlg("i",1,aa.toString());
}
catch(Exception ex)
{
return;
}
newoffset=offset;
if(ats(offset))
{
newoffset++;
jtc.setCaretPosition(newoffset);
}
super.remove(newoffset,1);
super.insertString(newoffset,s,aset);
}
}
public void remove(int offset,int length) throws BadLocationException
{
if (ats(offset))
jtc.setCaretPosition(offset-1);
else
jtc.setCaretPosition(offset);
}
private boolean ats(int offset)
{
return offset==s1||offset==s2||offset==s3;
}
}
import java.applet.*;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.event.*;
import formatstring;public class MyApp extends JApplet
{
final JButton b1;
JTextField jtf=new JTextField(formatstring.f_string);
JPanel jp1;
public MyApp()
{
Container con=getContentPane();
JRootPane rootpane=getRootPane();
jtf.setDocument(new formatstring(jtf));
b1=new JButton("OK");
jp1=new JPanel();
rootpane.setDefaultButton(b1);
con.setLayout(new FlowLayout());
jp1.add(jtf);
con.add("Center",jp1);
con.add("South",b1);
b1.addMouseListener(new java.awt.event.MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
// dlg_show(b1.getText());
}
});
b1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(ActionEvent e)
{
// dlg_show(b1.getText());
}
});
}
} import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;
class formatstring extends PlainDocument
{
public static String f_string="000.000.000.000";
private static int s1=3,s2=7,s3=11;
private JTextComponent jtc;
private int newoffset;
int i=-1;
dlg dlg1;
public formatstring(JTextComponent jtf)
{
jtc=jtf;
try
{
insertString(0,f_string,null);
}
catch(Exception e)
{
}
}
public void insertString(int offset,String s,AttributeSet aset) throws BadLocationException
{
String asd;
int i;
if(s.equals(f_string))
{
super.insertString(offset,s,aset);
}
else
{
try
{
i=offset;
if ((i==0)||(i==3)||(i==7)||(i==11) )
{
if (Integer.parseInt(s)>2)
return;
}
else
{
if (Integer.parseInt(s)>5)
return;
}
Integer aa=new Integer(i);
// dlg1=new dlg("i",1,aa.toString());
}
catch(Exception ex)
{
return;
}
newoffset=offset;
if(ats(offset))
{
newoffset++;
jtc.setCaretPosition(newoffset);
}
super.remove(newoffset,1);
super.insertString(newoffset,s,aset);
}
}
public void remove(int offset,int length) throws BadLocationException
{
if (ats(offset))
jtc.setCaretPosition(offset-1);
else
jtc.setCaretPosition(offset);
}
private boolean ats(int offset)
{
return offset==s1||offset==s2||offset==s3;
}
}
解决方案 »
- swing awt 界面中文乱码? Linux(fedora ) OS
- SSH做添加的过程中出现了异常javax.servlet.ServletException: not-null property references a nu
- 这两种加载工程资源方法的区别
- 360度跪求新手入门视频教程,顺便问下关于引用变量
- JToggleButton这个按钮大家用过吗?我觉得好象没什么必要用到这个按钮
- 谁能给一个控制台下的ant代码
- 一个关于小数位数截取的简单问题
- 一个关于java反射的问题
- Boland JB的小问题,上分了
- 怎么对Ctrl键和鼠标左键同时按下进行判断呀?
- 怎样学JAVA?
- 急救!!!!!!!!!!!请各位高手指点!!
textField.addKeyListener(new KeyAdapter()
{
public void keyTyped(KeyEvent evt)
{
char ch=evt.getKeyChar();
if(Character.isUpperCase(ch)
ch=Character.toLowerCase(ch);
if(ch>'a' && ch<'z')
evt.consume();
}
});
if(ch>'a' && ch<'z')
应改为
if(ch>='a' && ch<='z')
这种方法的好处是,处理程序永远不会接收到不合法的键入事件