Java JFormatter赢得';t工作-*JFormattedTextField1*Won';t格式
Jformatter无法工作,在加载表单时尝试格式化Java JFormatter赢得';t工作-*JFormattedTextField1*Won';t格式,java,swing,jformattedtextfield,Java,Swing,Jformattedtextfield,Jformatter无法工作,在加载表单时尝试格式化JFormattedTextField1(因此用户只能以格式输入电话号码)。但那是行不通的 不会工作=表单根本不做任何事情,JFormattedTextField1只是保持不变 代码: 嘿,今天我有一个猜测日,想和你们分享我的解决方案,也许它适合你们的需要 这是一个简单的类,它初始化JFrame并添加一个包含JFormattedTextField和JButton的JPanel。如果单击该按钮,文本字段的内容将在控制台上打印出来。如果输入不匹配,
JFormattedTextField1
(因此用户只能以格式输入电话号码)。但那是行不通的
不会工作=表单根本不做任何事情,JFormattedTextField1
只是保持不变
代码:
嘿,今天我有一个猜测日,想和你们分享我的解决方案,也许它适合你们的需要 这是一个简单的类,它初始化JFrame并添加一个包含JFormattedTextField和JButton的JPanel。如果单击该按钮,文本字段的内容将在控制台上打印出来。如果输入不匹配,控制台上将显示错误
package com.example;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.text.MaskFormatter;
public class MainFrame extends JFrame {
/**
*
*/
private static final long serialVersionUID = 4159266806348540020L;
private JFormattedTextField tf;
private JButton clickBtn;
public MainFrame(final String title) {
initGUI(title);
}
private void initGUI(String title) {
this.setTitle(title);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
try {
final MaskFormatter formatter = new MaskFormatter("###-##-####");
tf = new JFormattedTextField(formatter);
JPanel panel = new JPanel(new BorderLayout());
panel.add(tf, BorderLayout.NORTH);
clickBtn = new JButton("Click me!");
clickBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if(!tf.getText().matches(formatter.getMask())) {
System.err.println("Your Input does not match the pattern!");
} else {
System.out.println(tf.getText());
}
}
});
panel.add(clickBtn, BorderLayout.SOUTH);
this.getContentPane().add(panel, BorderLayout.CENTER);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setSize(800, 600);
this.setVisible(true);
}
}
起点
package com.example;
public class DemoApp {
public static void main(String[] args) {
new MainFrame("FormattedTextField Demo");
}
}
下次提问时请更详细
帕特里克“这行不通”永远都是不够的信息。有人有什么想法吗?
package com.example;
public class DemoApp {
public static void main(String[] args) {
new MainFrame("FormattedTextField Demo");
}
}