0、前言
没有前言,直接开搞!!!
1、准备
集成开发环境:IDEA
JDK:1.8
2、创建项目
3、 项目结构
单文件就随便怎么放了
4、代码
Demo.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.net.*;
import java.util.*;
/**
* @author KonBAI
* @version 1.0
* Create by 2020/05/10 01:08
*/
public class Demo extends JFrame {
/**
* 窗口标题
*/
// TODO: 根据自己需要修改窗口标题
public static final String WINDOWS_TITLE = "名字-333";
public Demo() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
JLabel inputLabel = new JLabel();
// 接收用户输入内容
JTextField inputTextField = new JTextField();
// 提交按钮
JButton submitBtn = new JButton();
// 获取所有的网络接口信息按钮
JButton findAllBtn = new JButton();
JScrollPane scrollPane = new JScrollPane();
// 输出文本
JTextArea viewTextArea = new JTextArea();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setTitle(WINDOWS_TITLE);
inputLabel.setText("请输入 IP/InterfaceName:");
submitBtn.setText("提 交");
submitBtn.addActionListener(evt -> jButtonActionPerformed(evt, viewTextArea, inputTextField.getText()));
findAllBtn.setText("获取所有的网络接口信息");
findAllBtn.addActionListener(evt -> jButtonActionPerformed(evt, viewTextArea, null));
viewTextArea.setColumns(20);
viewTextArea.setRows(10);
scrollPane.setViewportView(viewTextArea);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(inputLabel, GroupLayout.PREFERRED_SIZE, 150, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(inputTextField, GroupLayout.PREFERRED_SIZE, 180, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(submitBtn, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(46, 46, 46))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(findAllBtn, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(scrollPane)))
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(inputLabel, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
.addComponent(inputTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(submitBtn))
.addComponent(findAllBtn)
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
pack();
}
private void jButtonActionPerformed(ActionEvent evt, JTextArea viewTextArea, String input) {
viewTextArea.setText("");
boolean flag = false;
try {
//获取所有接口,并放进枚举集合中,然后使用Collections.list()将枚举集合转换为ArrayList集合
ArrayList<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (Iterator<NetworkInterface> interfaceIterator = interfaces.iterator(); interfaceIterator.hasNext(); ) {
NetworkInterface networkInterface = interfaceIterator.next();
String intName = networkInterface.getName(); //获取接口名
//获取每个接口中的所有ip网络接口集合,因为可能有子接口
ArrayList<InetAddress> inets = Collections.list(networkInterface.getInetAddresses());
for (Iterator<InetAddress> inetAddrIterator = inets.iterator(); inetAddrIterator.hasNext(); ) {
InetAddress inetAddress = inetAddrIterator.next();
//只筛选ipv4地址,否则会同时得到Ipv6地址
if (inetAddress instanceof Inet4Address) {
String ip = inetAddress.getHostAddress();
if (Objects.isNull(input)) {
flag = true;
viewTextArea.append("InetfaceName:" + intName + " | IPv4:" + ip + "\n");
} else if (intName.equals(input) || ip.equals(input)) {
flag = true;
viewTextArea.append("InetfaceName:" + intName + " | IPv4:" + ip + "\n");
}
}
}
}
if (!flag) {
viewTextArea.setText("未找到 '" + input + "' 相关IP/InterfaceName,请重新输入");
}
} catch (SocketException s) {
s.printStackTrace();
}
}
public static void main(String[] args) {
// TODO: 添加窗口样式
EventQueue.invokeLater(() -> new Demo().setVisible(true));
}
}
5、运行截图
5、自定义修改
- 1、修改窗口标题
- 2、添加窗口样式(可有可无)
添加代码如下 :
// TODO: 添加窗口样式
try {
/**
* Metal风格: javax.swing.plaf.metal.MetalLookAndFeel
* Nimbus风格: javax.swing.plaf.nimbus.NimbusLookAndFeel
* Motif风格: com.sun.java.swing.plaf.motif.MotifLookAndFeel
* Windows风格: com.sun.java.swing.plaf.windows.WindowsLookAndFeel
* WindowsClassic风格: com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel
*/
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception e) {
e.printStackTrace();
}