怎样给一个JTextField定义如####.00的金额形式的掩码 干脆用两个JTextField吧,一个用来写整数,一个用来写小数。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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; } } what is that "dlg"????? // dlg1=new dlg("i",1,aa.toString());这句是被注释的,没用的 两个JTextField吧,一个用来写整数,一个用来写小数。 to hexiaofeng and vdragon :两个JTextField?太玩笑了吧 ? 单引号的问题 java关于文件操作的问题 java问题 学习我们公司的前辈写的代码,和大家一起学习 请大家回答我一个WebLogic和JBuilder编程的问题,谢谢大家了 jsmooth制作的exe压缩文件损坏 我的Eclipse为什么不是中文的,我的朋友的怎么是中文的呀 好书推荐:《Java就业培训教程》 [怪象论坛]为什么总有那么多“我要……”? 新手:各位大虾用什么工具编java?有人说vj++不好。 谁直到这是怎么回事 怎样把图象保存在数据库中?
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;
}
}
这句是被注释的,没用的
两个JTextField?太玩笑了吧 ?