通过JFormattedTextField接收文本输入

时间:2015-04-13 19:38:24

标签: java swing jformattedtextfield maskformatter

我在通过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);
    }
}

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

1 个答案:

答案 0 :(得分:2)

当我运行您的代码时,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();
    }