【问题标题】:Receiving text inputs via a JFormattedTextField通过 JFormattedTextField 接收文本输入
【发布时间】:2015-04-13 19:38:24
【问题描述】:

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

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

【问题讨论】:

  • getValue,应该在Oracle教程中描述,也许有关于的工作代码示例
  • 您好,我尝试使用 getvalue 但没有成功

标签: java swing jformattedtextfield maskformatter


【解决方案1】:

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

在您的构造函数中,更改以下 sn-p:

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();
    }

【讨论】:

  • 嗨 Shar1er80,我是编程新手。非常感谢您的建议。现在可以使用了。
  • 很高兴听到它正在工作!请点击对我的回答的检查,以便您的问题得到解决。
猜你喜欢
  • 2014-04-30
  • 1970-01-01
  • 2014-06-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多