通过 JFormattedTextField 接收文本输入

Receiving text inputs via a JFormattedTextField

我在通过 JFormattedTextField 从用户那里获取文本输入时遇到问题。我已经尝试将示例值(例如 172.16.10.0)传递到文本字段中,并希望将其传输到字符串变量 strIP 中。

目前我正在使用以下方法从文本字段中提取值:

strIP = txtIP.getText();

但是,调用上述方法后,我一直报错。我的示例的完整代码如下:

import java.awt.*;
import java.awt.event.*;
import javax.swing.text.MaskFormatter;
import javax.swing.*;
import java.text.ParseException;
import java.util.regex.Pattern;
import java.math.BigInteger;
import java.util.Arrays;

public class update extends JPanel implements ActionListener
{
    private JButton cmdCalculate;
    JFormattedTextField txtIP;
    JFormattedTextField txtSub;

    private JLabel  lblCalculate, lblIP, lblSub, lblNetwork, lblHost;
    private static String IPaddress = "",Subenetaddress= "", Networkaddress= "", Lastaddress= "";   
    private JPanel panAnswerArea, panNorthArea, panBase, panAddressGrid, panIP, panSub, panButton;

    public update() 
    {       
        super.setLayout(new BorderLayout());    
        cmdCalculate = new JButton("Calculate");
        cmdCalculate.addActionListener(this);

        lblIP = new JLabel("IP Address: ");
        lblSub = new JLabel("Subnet Mask: ");
        panIP = new JPanel();
        panSub = new JPanel();
        panIP.add(lblIP);
        panSub.add(lblSub);
        MaskFormatter mf = null;

        try {
           MaskFormatter mf = new MaskFormatter("***.***.***.***");
           JFormattedTextField txtIP = new JFormattedTextField(mf);
           panIP.add(txtIP);
           txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
           txtIP.setPreferredSize(new Dimension(150, 24)); 
        } catch (ParseException pe) {
            e.printStackTrace();
        }       

        lblCalculate = new JLabel("Calculate Network and Host parts: ", JLabel.LEFT);
        lblNetwork = new JLabel("Network address: ", JLabel.LEFT);
        lblHost = new JLabel("Host address: ", JLabel.LEFT);

        lblNetwork.setText("Network address: ");
        lblHost.setText("Host address: ");

        panAnswerArea = new JPanel(new BorderLayout()); 
        panNorthArea = new JPanel(new BorderLayout()); 
        panAddressGrid = new JPanel(new GridLayout(4,2)); 
        panButton = new JPanel();

        panAnswerArea = new JPanel(new BorderLayout()); 
        this.add(panAnswerArea, BorderLayout.SOUTH);
        panAnswerArea.add(lblNetwork, BorderLayout.NORTH);
        panAnswerArea.add(lblHost, BorderLayout.SOUTH);

        panNorthArea = new JPanel(new BorderLayout());
        this.add(panNorthArea, BorderLayout.NORTH);
        panNorthArea.add(panAddressGrid, BorderLayout.SOUTH);
        panAddressGrid.add(panIP);
        panAddressGrid.add(panSub);
        panAddressGrid.add(panButton);
        panButton.add(lblCalculate);
        panButton.add(cmdCalculate);
    }

    public void actionPerformed (ActionEvent e)
    {
        String strIP= "";
        String strSub= "";
        String IPaddress = "",Subenetaddress= "", Networkaddress= "", Lastaddress= "", inversbits= "",inversesubnetmask= "";
        strIP = txtIP.getText();// Problem is here 
    }

    public static void main(String[] args)
    {
        JFrame.setDefaultLookAndFeelDecorated(true);
        JFrame frame = new JFrame("Subnet Calculators"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
        JComponent paneMain = new update();
        paneMain.setOpaque(true);
        paneMain.setPreferredSize(new Dimension(500, 300));
        frame.setContentPane(paneMain);
        frame.pack();
        frame.setVisible(true);
    }
}

非常感谢任何反馈和建议。

当我 运行 你的代码时,txtIP 的值是 null

在您的构造函数中,更改以下代码段:

try {
    MaskFormatter mf = new MaskFormatter("***.***.***.***");
    JFormattedTextField txtIP = new JFormattedTextField(mf);
    panIP.add(txtIP);
    txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
    txtIP.setPreferredSize( new Dimension( 150, 24 )); 
} catch (ParseException pe) {
    e.printStackTrace();
}

改为如下所示:

try {
        mf = new MaskFormatter("***.***.***.***");
        // You have txtIP declared as a class variable, and you (I believe the term is called shadowed) 
        // hid the class variable with your original code and txtIP is treated as a local variable in your constructor
        // so the class field was never initialized
        txtIP = new JFormattedTextField(mf);
        panIP.add(txtIP);
        txtIP.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        txtIP.setPreferredSize(new Dimension( 150, 24)); 
    } catch (ParseException pe) {
        e.printStackTrace();
    }