import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;import javax.swing.*;import com.borland.jbcl.layout.XYConstraints;
import com.borland.jbcl.layout.XYLayout;public class Logon extends JFrame {
private JComboBox jcomboServer; private JLabel labelServer, labelUserName, labelUserPassword; private JTextField textUserName; private JPasswordField pdField; private JButton butOK, butUpDateSer, butCancel, butHelp; private XYLayout xYLayout;

private ArrayList serlist; public Logon() {
serlist=new ArrayList();
jcomboServer = new JComboBox();

File fileSerList = new File("bin/serverlist.ini"); if (fileSerList.exists()) { try {
FileReader fr = new FileReader("bin/serverlist.ini"); BufferedReader br = new BufferedReader(fr); String line = null;
int i = 0; while ((line = br.readLine()) != null) {

String strIPadd;
String strPort; if (line.indexOf(":") != -1) {
// System.out.println(line);
strIPadd = line.substring(0, line.indexOf(":"));
strPort = line.substring(line.indexOf(":") + 1, line
.length());
serlist.add(line);
// System.out.println(strIPadd+":"+strPort);
jcomboServer.addItem(strIPadd + ":" + strPort); } }
br.close();
} catch (IOException e) { JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示",
JOptionPane.ERROR_MESSAGE);
} } else {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(
fileSerList));
out.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示",
JOptionPane.ERROR_MESSAGE);
}
} labelServer = new JLabel("服务器");
labelUserName = new JLabel("用户标识");
labelUserPassword = new JLabel("口令");
butOK = new JButton("确定");
butUpDateSer = new JButton("更改服务器");
butUpDateSer.setToolTipText("更改服务器... ...");
butUpDateSer.addActionListener(new OpenUpDataWindowsAction());
butCancel = new JButton("取消"); butHelp = new JButton("帮助"); ;
textUserName = new JTextField();
pdField = new JPasswordField("password"); xYLayout = new XYLayout();
Container container = getContentPane();
container.setLayout(xYLayout); container.add(labelServer, new XYConstraints(15, 20, 55, 20));
container.add(jcomboServer, new XYConstraints(80, 20, 120, 20)); container.add(labelUserName, new XYConstraints(15, 50, 55, 20));
container.add(textUserName, new XYConstraints(80, 50, 120, 20)); container.add(labelUserPassword, new XYConstraints(15, 80, 55, 20));
container.add(pdField, new XYConstraints(80, 80, 120, 20)); container.add(butOK, new XYConstraints(220, 20, 70, 20));
container.add(butUpDateSer, new XYConstraints(220, 50, 70, 20));
container.add(butCancel, new XYConstraints(220, 80, 70, 20)); this.setContentPane(container); this.setTitle("登录到服务器");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
this.setSize(320, 150);
int w = getSize().width;
int h = getSize().height;
int x = (dim.width - w) / 2;
int y = (dim.height - h) / 2;
this.setLocation(x, y);
this.setVisible(true);
this.setResizable(false); }

public void jcomboServerListReload(){

File fileSerList = new File("bin/serverlist.ini"); if (fileSerList.exists()) { try {
FileReader fr = new FileReader("bin/serverlist.ini"); BufferedReader br = new BufferedReader(fr); String line = null;
int i = 0; while ((line = br.readLine()) != null) {

String strIPadd;
String strPort; if (line.indexOf(":") != -1) {
// System.out.println(line);
strIPadd = line.substring(0, line.indexOf(":"));
strPort = line.substring(line.indexOf(":") + 1, line
.length());
serlist.add(line);
// System.out.println(strIPadd+":"+strPort);
jcomboServer.addItem(strIPadd + ":" + strPort); } }
br.close();
} catch (IOException e) { JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示",
JOptionPane.ERROR_MESSAGE);
} } else {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(
fileSerList));
out.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示",
JOptionPane.ERROR_MESSAGE);
}
}

}
public static void main(String[] args) {

Logon logonWindows = new Logon();

} class OpenUpDataWindowsAction implements ActionListener { public void actionPerformed(ActionEvent e) {
UpDataServer updataserverWindows = new UpDataServer();
updataserverWindows.ReadServerList();

}
}}

解决方案 »

  1.   

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Dimension;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.io.*;
    import java.util.ArrayList;import javax.swing.*;
    import javax.swing.border.Border;import com.borland.jbcl.layout.XYConstraints;
    import com.borland.jbcl.layout.XYLayout;public class UpDataServer extends JFrame { // private JPanel jPaneMessage;
    private JComboBox jcomboServer; private JLabel labServer, labHostIP, labHostPort; private JButton butAdd, butUpDateSer, butDel, butClose; private JTextField jtextFieldIP, jtextFieldPort; private XYLayout xYLayout; private JTextArea jtextareaMessage; private FileReader fr; private ArrayList serlist; private String strIPadd; private String strPort; private String selectSerIP; private String selectSerPort; private ipValid testIP;

    private Logon logonWindows; public UpDataServer() { serlist = new ArrayList();
    testIP = new ipValid(); jcomboServer = new JComboBox();
    jcomboServer.setToolTipText("");
    jcomboServer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
    // jtextFieldIP.setText(jcomboServer.getSelectedItem().toString());
    jtextFieldIP.setText(jcomboServer.getSelectedItem().toString()
    .substring(
    0,
    jcomboServer.getSelectedItem().toString()
    .indexOf(":")));
    String temp;
    temp = jcomboServer.getSelectedItem().toString(); selectSerPort = temp.substring(temp.indexOf(":") + 1, temp
    .length());
    jtextFieldPort.setText(selectSerPort); } }); jtextareaMessage = new JTextArea();
    jtextareaMessage.setText("        在主机IP和端口中输入新服\n" + "务器地址和端口,点添加按钮后\n"
    + "可将新服务器加入服务器列表。");
    jtextareaMessage.setBackground(Color.lightGray);
    jtextareaMessage.setEditable(false); labServer = new JLabel("服务器(S)");
    labHostIP = new JLabel("主机IP(N)");
    labHostPort = new JLabel("端口(P)"); jtextFieldIP = new JTextField();
    jtextFieldPort = new JTextField(); butAdd = new JButton("添加(A)");
    butAdd.addActionListener(new ActionListener() {
    private String newIpPort; private String serIPPort; private String addSerIP; private String addSerPort; private String temp; public void actionPerformed(ActionEvent arg0) {



    addSerIP = jtextFieldIP.getText();
    addSerPort = jtextFieldPort.getText();
    int intPort=Integer.parseInt(addSerPort);

    if((0<intPort)&&(intPort<65535)){ if (testIP.ipValid(addSerIP)) { serIPPort = addSerIP + ":" + addSerPort; // System.out.println(serIPPort); String tempip;
    String tempport; for (int a = 0; a < serlist.size(); a++) {
    tempip = serlist.get(a).toString().substring(0,
    serlist.get(a).toString().indexOf(":"));
    tempport = serlist.get(a).toString().substring(
    serlist.get(a).toString().indexOf(":") + 1,
    serlist.get(a).toString().length());
    if (serIPPort.equals(tempip + ":" + tempport)) {
    JOptionPane.showMessageDialog(null, "该服务器已经存在!",
    "错误提示", JOptionPane.ERROR_MESSAGE);
    temp = tempip + ":" + tempport;
    } }
    newIpPort = serIPPort;
    if (!newIpPort.equals(temp)) {
    System.out.println(newIpPort);
    serlist.add(newIpPort);
    jcomboServer.addItem(newIpPort);
    } File fileSerList = new File("bin/serverlist.ini"); try {
    BufferedWriter out = new BufferedWriter(new FileWriter(
    fileSerList));
    for (int a = 0; a < serlist.size(); a++) {
    out.write(serlist.get(a).toString() + "\n");
    }
    out.close();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }
    } } else {
    JOptionPane.showMessageDialog(null, "输入的IP地址或端口[1-65535]非法", "错误提示",
    JOptionPane.ERROR_MESSAGE);
    }
    } }); butUpDateSer = new JButton("更新(U)");
    butUpDateSer.setEnabled(false); butDel = new JButton("删除(D)");
    butDel.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try {
    int delitem = jcomboServer.getSelectedIndex();
    // System.out.println(delitem);
    if (delitem < 0) {
    JOptionPane.showMessageDialog(null, "没有可用服务器", "错误提示",
    JOptionPane.ERROR_MESSAGE); } else {
    jcomboServer.removeItemAt(delitem); serlist.remove(delitem);
    // System.out.println(serlist.size());
    }
    } catch (NullPointerException i) {
    JOptionPane.showMessageDialog(null, "没有可用服务器", "错误提示",
    JOptionPane.ERROR_MESSAGE);
    }

    File fileSerList = new File("bin/serverlist.ini"); try {
    BufferedWriter out = new BufferedWriter(new FileWriter(
    fileSerList));
    for (int a = 0; a < serlist.size(); a++) {
    out.write(serlist.get(a).toString() + "\n");
    }
    out.close();
    } catch (IOException e) {
    // TODO 自动生成 catch 块
    e.printStackTrace();
    }


    } });
      

  2.   

    butClose = new JButton("关闭(C)");
    butClose.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) {
    dispose(); } }); xYLayout = new XYLayout();
    Container container = getContentPane();
    container.setLayout(xYLayout); container.add(labServer, new XYConstraints(15, 20, 55, 20));
    container.add(jcomboServer, new XYConstraints(80, 20, 120, 20));
    container.add(labHostIP, new XYConstraints(220, 20, 55, 20));
    container.add(jtextFieldIP, new XYConstraints(280, 20, 120, 20));
    container.add(labHostPort, new XYConstraints(220, 50, 120, 20));
    container.add(jtextFieldPort, new XYConstraints(280, 50, 50, 20)); Border etched = BorderFactory.createEtchedBorder();
    jtextareaMessage.setBorder(BorderFactory.createEtchedBorder());
    container.add(jtextareaMessage, new XYConstraints(25, 50, 180, 60)); container.add(butAdd, new XYConstraints(20, 120, 80, 20));
    container.add(butUpDateSer, new XYConstraints(115, 120, 80, 20));
    container.add(butDel, new XYConstraints(210, 120, 80, 20));
    container.add(butClose, new XYConstraints(305, 120, 80, 20)); this.setContentPane(container); this.setTitle("更新服务器");
    this.setSize(420, 190);
    setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width - w) / 2;
    int y = (dim.height - h) / 2;
    this.setLocation(x, y);
    this.setVisible(true);
    this.setResizable(false);
    } public void ReadServerList() {
    File fileSerList = new File("bin/serverlist.ini"); if (fileSerList.exists()) { try {
    fr = new FileReader("bin/serverlist.ini"); BufferedReader br = new BufferedReader(fr); String line = null;
    int i = 0; while ((line = br.readLine()) != null) { if (line.indexOf(":") != -1) {
    // System.out.println(line);
    strIPadd = line.substring(0, line.indexOf(":"));
    strPort = line.substring(line.indexOf(":") + 1, line
    .length());
    serlist.add(line);
    // System.out.println(strIPadd+":"+strPort);
    jcomboServer.addItem(strIPadd + ":" + strPort); } }
    br.close();
    } catch (IOException e) { JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示",
    JOptionPane.ERROR_MESSAGE);
    } } else {
    try {
    BufferedWriter out = new BufferedWriter(new FileWriter(
    fileSerList));
    out.close();
    } catch (IOException e) {
    JOptionPane.showMessageDialog(null, e.getMessage(), "错误提示",
    JOptionPane.ERROR_MESSAGE);
    }
    } }}
      

  3.   

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;public class ipValid {

    boolean ipValid(String s){
        String regex0="(2[0-4]\\d)" + "|(25[0-5])";
        String regex1="1\\d{2}";
        String regex2="[1-9]\\d";
        String regex3="\\d";
        String regex="("+regex0+")|("+regex1+")|("+regex2+")|("+regex3+")";
        regex="("+regex+").("+regex+").("+regex+").("+regex+")";
        Pattern p=Pattern.compile(regex);
        Matcher m=p.matcher(s);
        return m.matches();
      }

    boolean portVlid(String s){
    String port1="[1-9]\\d";
    String port2="2\\d{4}";
    String port3="3\\d{4}";
    String port4="4\\d{4}";
    String port5="5\\d{4}";
    String port6="6[0-4]\\d{3}"+"|(65[0-2]\\d{2})"+"|(655[0-2]\\d{1})"+"|(6553[0-5])";;
    //String port7="65[0-2]\\d{2}";
    //String port8="655[0-2]\\d{1}";
    String port="("+port1+")|("+port2+")|("+port3+")|("+port4+")|("+port5+")|("+port6+")";
    Pattern p=Pattern.compile(port);
    Matcher m=p.matcher(s);
    return m.matches();

    }}