java怎么操作无线网卡,看到有人分享netsh用笔记本的无线网卡虚拟一个热点出来...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

package com.vankong.ui;import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.GridLayout;

import java.awt.Toolkit;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.io.IOException;

import java.util.Map;import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JPasswordField;

import javax.swing.JTextField;import com.vankong.utils.CMDResultUtils;/**

*

* 主界面功能类

*

* @author lihuan

*

*/

public class IndexUI extends JFrame { private JTextField wifiNameText = null;

private JTextField wifiPasswordText = null;

private JButton startButton = null;

private JButton stopButton = null;

private static final long serialVersionUID = 1L; public IndexUI() {

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.setSizeCenter(500, 300);

this.checkNetshState();

this.setTitle("笔记本热点");

} /**

* 检测虚拟网卡状态是否启动

*/

private void checkNetshState() {

try {

Process proces = Runtime.getRuntime().exec("netsh wlan show hostednetwork");

Map result = CMDResultUtils.getNetshExecState(proces.getInputStream());

if (result.get("SSID名称") != null) {

wifiNameText.setText(result.get("SSID名称").replace("“", "").replace("”", ""));

}

if (result.get("状态") != null && result.get("状态").equals("已启动")) {

wifiNameText.setEnabled(false);

wifiPasswordText.setEnabled(false);

startButton.setEnabled(false);

stopButton.setEnabled(true);

} else {

wifiNameText.setEnabled(true);

wifiPasswordText.setEnabled(true);

startButton.setEnabled(true);

stopButton.setEnabled(false);

}

} catch (IOException e) {

JOptionPane.showMessageDialog(null, "检测虚拟网卡时发生异常.", "错误", JOptionPane.ERROR_MESSAGE);

}

} /**

* 设置窗口大小并相对显示器居中

*/

private void setSizeCenter(int width, int height) {

Dimension screenSize = getScreenSize();

this.setBounds(screenSize.width / 2 - width / 2, screenSize.height / 2 - height / 2, width, height);

this.setLayout(new GridLayout(2, 1));

this.add(this.getTextPanel());

this.add(this.getButtonPanel());

//随机窗口样式 ^_^

this.setUndecorated(true);

switch ((int)(Math.random() * 4)) {

case 0:

this.getRootPane().setWindowDecorationStyle(4);

break;

case 1:

this.getRootPane().setWindowDecorationStyle(5);

break;

case 2:

this.getRootPane().setWindowDecorationStyle(8);

break;

default:

this.getRootPane().setWindowDecorationStyle(3);

break;

}

} private JPanel getTextPanel() {

JPanel panel = new JPanel();

panel.setLayout(new GridLayout(2, 1));

panel.add(this.getNamePanel());

panel.add(this.getPasswordPanel());

return panel;

} private JPanel getButtonPanel() {

JPanel panel = new JPanel();

panel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 50));

panel.add(this.getStartButton());

panel.add(this.getStopButton());

return panel;

} private JPanel getNamePanel() {

JPanel panel = new JPanel();

JLabel label = new JLabel();

label.setText("WiFi名称:");

wifiNameText = new JTextField(15);

panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 45));

panel.add(label);

panel.add(wifiNameText);

return panel;

}

private JPanel getPasswordPanel() {

JPanel panel = new JPanel();

JLabel label = new JLabel();

label.setText("WiFi密码:");

wifiPasswordText = new JPasswordField(15);

panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 30));

panel.add(label);

panel.add(wifiPasswordText);

return panel;

} /**

*

* 初始化启动按钮

*

* @return 启动按钮

*/

private JButton getStartButton() {

startButton = new JButton();

startButton.setText("启动");

startButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

if (wifiNameText.getText().trim().equals("")) {

JOptionPane.showMessageDialog(null, "wifi名称不允许为空!", "提示", JOptionPane.INFORMATION_MESSAGE);

return;

}

if (wifiPasswordText.getText().equals("")) {

JOptionPane.showMessageDialog(null, "wifi密码不允许为空!", "提示", JOptionPane.INFORMATION_MESSAGE);

return;

} else if (wifiPasswordText.getText().length() < 8) {

JOptionPane.showMessageDialog(null, "wifi密码必须大于等于8个字符!", "提示", JOptionPane.INFORMATION_MESSAGE);

return;

}

Process proces = null;

try {

proces = Runtime.getRuntime().exec("netsh wlan set hostednetwork mode=allow ssid=" + wifiNameText.getText() + " key=" + wifiPasswordText.getText());

if (proces.waitFor() == 0) {

proces = Runtime.getRuntime().exec("netsh wlan start hostednetwork");

if (proces.waitFor() == 0) {

JOptionPane.showMessageDialog(null, "启动成功!", "提示", JOptionPane.INFORMATION_MESSAGE);

startButton.setEnabled(false);

wifiNameText.setEnabled(false);

wifiPasswordText.setEnabled(false);

stopButton.setEnabled(true);

return;

}

}

} catch (IOException e1) {

e1.printStackTrace();

} catch (InterruptedException e1) {

e1.printStackTrace();

}

if (proces != null) {

JOptionPane.showMessageDialog(null, CMDResultUtils.getError(proces.getInputStream()), "错误", JOptionPane.ERROR_MESSAGE);

} else {

JOptionPane.showMessageDialog(null, "启动失败!", "错误", JOptionPane.ERROR_MESSAGE);

}

}

});

return startButton;

} /**

*

* 初始化停止按钮

*

* @return 停止按钮

*/

private JButton getStopButton() {

stopButton = new JButton();

stopButton.setText("停止");

stopButton.addActionListener(new ActionListener() {

@Override

public void actionPerformed(ActionEvent e) {

try {

Process proces = Runtime.getRuntime().exec("netsh wlan stop hostednetwork");

if (proces.waitFor() == 0) {

proces = Runtime.getRuntime().exec("netsh wlan set hostednetwork mode=disallow");

if (proces.waitFor() == 0) {

JOptionPane.showMessageDialog(null, "停止成功!", "提示", JOptionPane.INFORMATION_MESSAGE);

startButton.setEnabled(true);

stopButton.setEnabled(false);

wifiNameText.setEnabled(true);

wifiPasswordText.setEnabled(true);

return;

}

}

} catch (IOException e1) {

e1.printStackTrace();

} catch (InterruptedException e1) {

e1.printStackTrace();

}

JOptionPane.showMessageDialog(null, "停止失败!", "错误", JOptionPane.ERROR_MESSAGE);

}

});

return stopButton;

} /**

*

* 获取本机屏幕分辨率

*

* @return

*/

public static Dimension getScreenSize() {

Toolkit kit = Toolkit.getDefaultToolkit();

return kit.getScreenSize();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值