java swing ip地址输入框,用户在文本字段Java Swing中输入时,屏蔽IP地址

I have a JTextField to accommodate an ip address with 3 dots.

255.120.320.123. When the user enters this IP address, I want to mask it like ...

I was referring this thread, How to custom formatter JFormattedTextField to display an IP address?

jFormattedTextField did not work for me. Can anyone give me an example with jFormattedTextField with 3 dots visible?

Or do I need to use 4 jFomattedTextField/JPasswordField as mentioned in this thread?

Thanks in advance.

解决方案

Seems you need to use MaskFormatter ,for example:

try {

MaskFormatter mf = new MaskFormatter("###.###.###.###");

JFormattedTextField f = new JFormattedTextField(mf);

add(f);

} catch (ParseException e) {

e.printStackTrace();

}

f72QD.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的Java Swing程序,用于实现文件传输的IP地址输入框和端口号输入框: ```java import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; import java.net.*; public class FileTransfer extends JFrame implements ActionListener { private JTextField ipField; private JTextField portField; private JButton sendButton; private JButton chooseButton; private JFileChooser fileChooser; public FileTransfer() { setTitle("File Transfer"); setSize(400, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new BorderLayout()); JPanel topPanel = new JPanel(); topPanel.setLayout(new GridLayout(2, 2)); JLabel ipLabel = new JLabel("IP Address:"); ipField = new JTextField(); JLabel portLabel = new JLabel("Port:"); portField = new JTextField(); topPanel.add(ipLabel); topPanel.add(ipField); topPanel.add(portLabel); topPanel.add(portField); add(topPanel, BorderLayout.NORTH); JPanel centerPanel = new JPanel(); centerPanel.setLayout(new GridLayout(1, 2)); chooseButton = new JButton("Choose File..."); chooseButton.addActionListener(this); centerPanel.add(chooseButton); sendButton = new JButton("Send File"); sendButton.addActionListener(this); centerPanel.add(sendButton); add(centerPanel, BorderLayout.CENTER); fileChooser = new JFileChooser(); setVisible(true); } public void actionPerformed(ActionEvent e) { if (e.getSource() == chooseButton) { int returnVal = fileChooser.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fileChooser.getSelectedFile(); // Do something with the file } } else if (e.getSource() == sendButton) { String ipAddress = ipField.getText(); int portNumber = Integer.parseInt(portField.getText()); File file = fileChooser.getSelectedFile(); try { Socket socket = new Socket(ipAddress, portNumber); OutputStream outputStream = socket.getOutputStream(); FileInputStream fileInputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, bytesRead); } fileInputStream.close(); outputStream.close(); socket.close(); JOptionPane.showMessageDialog(this, "File sent successfully!"); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "Error sending file: " + ex.getMessage()); } } } public static void main(String[] args) { new FileTransfer(); } } ``` 这个程序创建了一个窗口,包含一个IP地址输入框、一个端口号输入框、一个“Choose File...”按钮和一个“Send File”按钮。当用户点击“Choose File...”按钮,程序打开一个文件选择器,允许用户选择要发送的文件。当用户点击“Send File”按钮,程序使用Socket连接到指定的IP地址和端口号,并将文件发送到远程主机。 请注意,此程序仅是一个示例,并不包含完整的错误处理和安全功能。在实际应用,您需要添加更多的错误处理和安全检查,以确保文件传输的安全和可靠。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值