公司用的是电信的网络,前段时间公司的电脑总是拨不上去号,自己写了一个基于Java的电信拨号程序,感兴趣的朋友可以下载下看看。
下面是拨号程序的原代码,大家可以看看,有什么不合理的地方请指出来。
package com.jack.telecom.dial;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Dial extends JFrame{
private JLabel lblConName;
private JTextField txtConName;
private JLabel lblConNum;
private JTextField txtConNum;
private JLabel lblStartNum;
private JComboBox cmbStartNum;
private JLabel lblMaxNum;
private JComboBox cmbEndNum;
private JLabel lblPwd;
private JPasswordField txtPwd;
private JLabel label;
private JLabel lblEndMessage;
private JLabel lblStartMessage;
private JLabel lblConNameMessage;
private JTextArea txaConMessage;
private JLabel lblMessage;
private JTextArea txaMessage;
private JButton btnConn;
private StringBuffer showMessage;
private String conNum;
private String resultStr = "";
public Dial() {
initComponents();
this.setTitle("电信拨号程序");
this.getContentPane().setLayout(null);
this.setSize(400, 500);
this.setVisible(true);
// this.setBounds(100, 100, 400, 472);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void initComponents() {
lblConName = new JLabel();
lblConName.setText("网络连接名称:");
lblConName.setBounds(10, 26, 91, 18);
this.getContentPane().add(lblConName);
txtConName = new JTextField();
txtConName.setText("jack");
txtConName.setBounds(107, 24, 87, 22);
this.getContentPane().add(txtConName);
lblConNum = new JLabel();
lblConNum.setText("连接帐号:");
lblConNum.setBounds(10, 54, 66, 18);
this.getContentPane().add(lblConNum);
txtConNum = new JTextField();
txtConNum.setText("n037113051");
txtConNum.setBounds(107, 50, 87, 22);
this.getContentPane().add(txtConNum);
lblStartNum = new JLabel();
lblStartNum.setText("起始数:");
lblStartNum.setBounds(10, 103, 66, 18);
this.getContentPane().add(lblStartNum);
cmbStartNum = new JComboBox();
cmbStartNum.setBounds(107, 99, 87, 27);
this.getContentPane().add(cmbStartNum);
lblMaxNum = new JLabel();
lblMaxNum.setText("最大拨号数:");
lblMaxNum.setBounds(10, 138, 91, 18);
this.getContentPane().add(lblMaxNum);
cmbEndNum = new JComboBox();
cmbEndNum.setBounds(107, 132, 87, 27);
for (int i = 1; i <= 99; i++) {
cmbStartNum.addItem(i);
cmbEndNum.addItem(i);
}
this.getContentPane().add(cmbEndNum);
lblPwd = new JLabel();
lblPwd.setText("密码:");
lblPwd.setBounds(10, 179, 91, 18);
this.getContentPane().add(lblPwd);
txtPwd = new JPasswordField();
txtPwd.setText("123456");
txtPwd.setActionCommand("*");
txtPwd.setBounds(107, 177, 94, 22);
this.getContentPane().add(txtPwd);
label = new JLabel();
label.setText("默认密码为123456");
label.setBounds(211, 179, 118, 18);
this.getContentPane().add(label);
lblEndMessage = new JLabel();
lblEndMessage.setText("选择您要进行拨号的最大限制");
lblEndMessage.setBounds(200, 138, 178, 18);
this.getContentPane().add(lblEndMessage);
lblStartMessage = new JLabel();
lblStartMessage.setText("选择您要进行拨号的起始数");
lblStartMessage.setBounds(200, 103, 178, 18);
this.getContentPane().add(lblStartMessage);
lblConNameMessage = new JLabel();
lblConNameMessage.setText("此名称为您拨号连接的名称");
lblConNameMessage.setBounds(200, 26, 178, 18);
this.getContentPane().add(lblConNameMessage);
txaConMessage = new JTextArea();
String msg = "请将您拨号帐号的前10位写上\n其中最后2位可以修改";
txaConMessage.setText(msg);
txaConMessage.setBounds(200, 54, 158, 40);
this.getContentPane().add(txaConMessage);
lblMessage = new JLabel();
lblMessage.setText("连接显示信息:");
lblMessage.setBounds(10, 248, 118, 18);
this.getContentPane().add(lblMessage);
txaMessage = new JTextArea();
txaMessage.setEditable(false);
JScrollPane scroll = new JScrollPane(txaMessage,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scroll.setBounds(10, 272, 368, 155);
this.getContentPane().add(scroll);
btnConn = new JButton();
btnConn.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
Thread t = new Thread() {
public void run() {
Process p = null;
String conn = txtConNum.getText();
int startNum = ((Integer) cmbStartNum.getSelectedItem()).intValue();
int endNum = ((Integer) cmbEndNum.getSelectedItem()).intValue();
String user = txtConName.getText();
String password = txtPwd.getText();
showMessage = new StringBuffer();
for (int i = startNum; i < endNum; i++) {
if (i < 10) {
conNum = conn + "0" + i;
} else {
conNum = conn + i;
}
try {
p = Runtime.getRuntime().exec(
"rasdial.exe " + user + " " + conNum
+ " " + password);
int result = p.waitFor();
if (result == 0) {
resultStr = "连接成功";
// break;
} else {
resultStr = result + "";
}
showMessage.append("当前连接帐号为:" + conNum + "\n"
+ "连接结果:" + resultStr + "\n");
txaMessage.setText(showMessage.toString());
// new Thread().start();
// showMsg(conNum, resultStr);
// System.out.println("得到的数据:" + conNum + "\n" +
// "连接结果:"
// + resultStr + "\n");
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
};
t.start();
}
});
btnConn.setText("开始拨号");
btnConn.setBounds(107, 214, 106, 28);
this.getContentPane().add(btnConn);
}
public void radial(String conNum,int startNum,int endNum,String user,String password){
Process p = null;
String conn = txtConNum.getText();
showMessage = new StringBuffer();
for (int i = startNum; i < endNum; i++) {
if (i < 10) {
conNum = conn + "0" + i;
} else {
conNum = conn + i;
}
try {
p = Runtime.getRuntime().exec(
"rasdial.exe " + user + " " + conNum
+ " " + password);
int result = p.waitFor();
if (result == 0) {
resultStr = "连接成功";
// break;
} else {
resultStr = result + "";
}
showMessage.append("当前连接帐号为:" + conNum + "\n"
+ "连接结果:" + resultStr + "\n");
txaMessage.setText(showMessage.toString());
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
public static void main(String[] args) {
Dial d = new Dial();
}
}