标签: jformattedtextfield

如何设置jFormattedTextField所以它只允许2个数字?

我是Java和NetBeans的初学者.我正在尝试制作一个简单的程序,你引入2个数字,它们的总和除以2.但是,我正在使用JFormattedTExtFields,我不知道如何自定义其中允许的输入.基本上我正试图找出如何:

  • 只允许输入数字JFormmatedTextField;
  • 只允许一定数量的数字;

java swing jformattedtextfield

5
推荐指数
1
解决办法
1万
查看次数

使用getValue for JFormattedText在另一个JFormattedText字段中显示

我有一个程序,我根据所选的单选按钮计算2个变量中的任何一个.我正在尝试使用getValueJFormattedText字段中获取值并将其显示在另一个JFormattedText字段上(最终我将使用该数字进行一些计算).

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.text.DecimalFormat;

public class FutureValueFrame extends JFrame
{
    public FutureValueFrame()
    {
        setTitle("Sample App");
        setSize(400,400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }


    public static void main(String[] args)
    {
        JFrame f = new FutureValueFrame();

        //GUI and BUTTONS
        JRadioButton monthlyRadioButton = new JRadioButton("Monthly Payment");
        JRadioButton loanAmountButton = new JRadioButton("Loan Amount");
        ButtonGroup selection = new ButtonGroup();
        selection.add(monthlyRadioButton);
        selection.add(loanAmountButton);

        JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));
        JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));
        JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##")); …
Run Code Online (Sandbox Code Playgroud)

java swing jformattedtextfield

5
推荐指数
1
解决办法
6071
查看次数

在JFormattedTextField中删除掩码时出现问题

我在JFormattedTextField中遇到掩码时遇到问题

我知道它用空格替换无效字符,或者用setPlaceholderCharacter定义的任何东西,但我需要它做的是允许删除或退格,并且不插入空格代替我删除的字符,只要其余的掩码中允许使用string.

例如,使用mask : *#*****,字符串"12 abc"有效.
如果你把光标放在b和c字符之间,然后按退格按钮,我需要它来删除b,导致"12 ac".相反,它删除它,并添加一个空格,成为:"12 a c".

下面是一个简单的代码示例来演示.

我很感激任何想法或例子来解决这个问题.


public class testFrame extends javax.swing.JFrame {

    public testFrame() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.FlowLayout());

        setMinimumSize(new Dimension(300,150));

        java.awt.Button closeButton = new java.awt.Button();
        JFormattedTextField maskTextField = new JFormattedTextField();
        maskTextField.setMinimumSize(new Dimension(100,30));

        getContentPane().add(maskTextField);

        closeButton.setLabel("close");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.exit(0);
            }
        });

        getContentPane().add(closeButton);

        try {
            MaskFormatter someMask = new MaskFormatter("*#****");
            DefaultFormatterFactory formatterFactory 
                = new DefaultFormatterFactory(someMask);
            maskTextField.setFormatterFactory(formatterFactory);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        maskTextField.setText("12 …
Run Code Online (Sandbox Code Playgroud)

java swing jformattedtextfield

5
推荐指数
1
解决办法
1904
查看次数

JFormattedTextField中未清除的值

我发现很难清楚JFormattedTextFields 的价值.怎么做?

我试过了

txtJFormattedTextField.setText("");
Run Code Online (Sandbox Code Playgroud)

但是当焦点再次失去时,我清除的价值就会出现.我阅读了有关此问题的API.

我为日期字段创建JTFormattedTextFields的工厂如下 -

public static JFormattedTextField createDateField() {
        MaskFormatter maskFormatter = null;
        try {
            maskFormatter = new MaskFormatter("##/##/####");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        JFormattedTextField formattedTextField = new JFormattedTextField(
                maskFormatter);
        SwingHelper.installDateValidation((AbstractDocument) formattedTextField
                .getDocument());
        formattedTextField.setColumns(10);
        return formattedTextField;
    }
Run Code Online (Sandbox Code Playgroud)

试过了

try {
    ((JFormattedTextField) txtSigninDate).commitEdit();
} catch (ParseException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

结果是一个例外 - 下面是异常跟踪.

java.text.ParseException: stringToValue passed invalid value
    at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:456)
    at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:371)
    at javax.swing.JFormattedTextField.commitEdit(JFormattedTextField.java:530)
    at app.view.action.Authentication_Action.clearSigninFields(Authentication_Action.java:207)
    at app.view.action.authentication.AuthenticationCommand.decorateSignout(AuthenticationCommand.java:285)
    at app.view.action.authentication.AuthenticationCommand.executeSignin(AuthenticationCommand.java:122)
    at app.view.action.Authentication_Action$2.actionPerformed(Authentication_Action.java:292)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
Run Code Online (Sandbox Code Playgroud)

java swing jformattedtextfield

5
推荐指数
1
解决办法
9607
查看次数

如何自定义格式化JFormattedTextField以显示IP地址?

我一直在尝试自定义JFormattedTextField显示用于输入IP地址的掩码.

我读了javadoc,我尝试过###.###.###.###,但在很多情况下都无效.

我找到了一些文章,他们使用正则表达式格式化值,但他们没有显示输入IP的掩码.

我想在文本字段中显示3个点来显示IP地址.像这样:

 .   .   .
Run Code Online (Sandbox Code Playgroud)

在很多情况下我可以输入IP地址.您可以在Windows中看到IPv4配置.

 .   .   .
Run Code Online (Sandbox Code Playgroud)

你能帮助我吗?谢谢阅读

java swing jformattedtextfield

5
推荐指数
1
解决办法
1847
查看次数

Swing - MaskFormatter - 从文本字段的右侧输入数字

我是Swing Java开发的新手.有人可以帮我这个.

我有一个带有maskformatter的jformattedtextfield.它工作得很好.但我唯一想知道的是,如果我们能够从右边输入数字.以下代码可以很好地从左到右输入数字.

感谢您的时间.

这是我的java代码:

public class MaskFormattedTextExample extends JFrame {

    private static final long serialVersionUID = -1212313123;

    JFormattedTextField timeField;

    public MaskFormattedTextExample() {
        initComponents();
    }

    private void initComponents() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(new Dimension(200, 200));
        getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));

        MaskFormatter mask = null;
        try {
            mask = new MaskFormatter("##:##:##");
            mask.setPlaceholderCharacter('_');
        } catch (ParseException e) {
            e.printStackTrace();
        }

        timeField = new JFormattedTextField(mask);
        timeField.setHorizontalAlignment(JTextField.RIGHT);
        timeField.setCaretPosition(JTextField.RIGHT);

        getContentPane().add(timeField);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                new MaskFormattedTextExample().setVisible(true);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

java swing jformattedtextfield

5
推荐指数
1
解决办法
1612
查看次数

带有 MaskFormatter java 的可选字符

我需要一个简单的东西。

我有一个JFormattedTextFieldMaskFormatter“##”,但我想第一个数字是可选的。

我想得到的信息是一个月的哪一天。

谢谢。

java jformattedtextfield maskformatter

5
推荐指数
0
解决办法
388
查看次数

通过Java中的JFormattedTextFields进行验证

我想有两个文本域,它们将具有以下验证

  1. 两者都应该是整数
  2. 一个文本字段中的值应该是其他值的整数倍

我已经使用了两个JFormattedTextFields.这是我到目前为止所做的

NumberFormat updateFormat,sampleFormat;
updateFormat = NumberFormat.getNumberInstance();
updateFormat.setMaximumFractionDigits(0);
updateFormat.setParseIntegerOnly(true);
sampleFormat = NumberFormat.getNumberInstance();
sampleFormat.setMaximumFractionDigits(0);
sampleFormat.setParseIntegerOnly(true);
javax.swing.JFormattedTextField jFormattedTextField1 =new javax.swing.JFormattedTextField(updateFormat);        
javax.swing.JFormattedTextField jFormattedTextField2 = new javax.swing.JFormattedTextField(sampleFormat);
Run Code Online (Sandbox Code Playgroud)

在这个的帮助下,我的第一部分是完整的现在我被第二部分困住了,即jFormattedTextField1值应该是jFormattedTextField2的整数倍.我想我需要通过扩展它来自定义我的NumberFormat.但是我该怎么办呢?请帮帮我

java validation swing jformattedtextfield

4
推荐指数
1
解决办法
1411
查看次数

Swing:JFormattedTextField仅使用Netbeans设置为数字

我有一点问题JFormattedTextField:我想将格式化的文本字段设置为仅数字.我在NetBeans上创建了一个带Swing接口的接口.

我的代码如下:

package SerasApp;

import static java.lang.System.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.MaskFormatter;
import javax.swing.text.NumberFormatter;

/**
 * @author adam
 */
public class Main extends javax.swing.JFrame {

    /**
     * Creates new form Main
     */
    public Main() {
        initComponents();
    }
    //set position of label to be the center of the frame
    public void PositionHiSera()
    {
        //find position of jpanel
        int FrameHeight = this.Intro.getHeight();
        int FrameWidth = this.Intro.getWidth();
        int LabelSize[] = new int[2]; …
Run Code Online (Sandbox Code Playgroud)

java swing netbeans matisse jformattedtextfield

4
推荐指数
1
解决办法
1万
查看次数

格式化文本字段和JComboBox在一起

我有一个GUI窗口,询问休息时间.我想得到的结果是,例如,1:15 - int hours = 1和int mins = 15 - 单击继续按钮后.我得到的结果要么是小时,要么是分钟,因为我不能让JComboBox和JButton一起工作(我想).另外,我不太清楚如何检查用户是输入了数字还是输入了无效的输入.这是代码:

@SuppressWarnings("serial")
public class FormattedTextFields extends JPanel implements ActionListener {

    private int hours;
    private JLabel hoursLabel;
    private JLabel minsLabel;
    private static String hoursString = " hours: ";
    private static String minsString = " minutes: ";
    private JFormattedTextField hoursField;
    private NumberFormat hoursFormat;

    public FormattedTextFields() {

        super(new BorderLayout());
        hoursLabel = new JLabel(hoursString);
        minsLabel = new JLabel(minsString);
        hoursField = new JFormattedTextField(hoursFormat);
        hoursField.setValue(new Integer(hours));
        hoursField.setColumns(10);
        hoursLabel.setLabelFor(hoursField);
        minsLabel.setLabelFor(minsLabel);

        JPanel fieldPane = new JPanel(new GridLayout(0, 2)); …
Run Code Online (Sandbox Code Playgroud)

java user-interface swing combobox jformattedtextfield

3
推荐指数
1
解决办法
1928
查看次数