?? connectconf.java
字號:
import java.awt.*;
import javax.swing.border.*;
import java.net.*;
import javax.swing.*;
import java.awt.event.*;
/**
* 生成連接信息輸入的對話框
* 讓用戶輸入連接服務器的IP和端口
*/
public class ConnectConf extends JDialog {
JPanel panelUserConf = new JPanel();
JButton save = new JButton();
JButton cancel = new JButton();
JLabel DLGINFO=new JLabel(
" 默認連接設置為 127.0.0.1:8888");
JPanel panelSave = new JPanel();
JLabel message = new JLabel();
String userInputIp;
int userInputPort;
JTextField inputIp;
JTextField inputPort;
public ConnectConf(JFrame frame,String ip,int port) {
super(frame, true);
this.userInputIp = ip;
this.userInputPort = port;
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
//設置運行位置,使對話框居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setLocation( (int) (screenSize.width - 400) / 2 + 50,
(int) (screenSize.height - 600) / 2 + 150);
this.setResizable(false);
}
private void jbInit() throws Exception {
this.setSize(new Dimension(300, 130));
this.setTitle("連接設置");
message.setText(" 請輸入服務器的IP地址:");
inputIp = new JTextField(10);
inputIp.setText(userInputIp);
inputPort = new JTextField(4);
inputPort.setText(""+userInputPort);
save.setText("保存");
cancel.setText("取消");
panelUserConf.setLayout(new GridLayout(2,2,1,1));
panelUserConf.add(message);
panelUserConf.add(inputIp);
panelUserConf.add(new JLabel(" 請輸入服務器的端口號:"));
panelUserConf.add(inputPort);
panelSave.add(new Label(" "));
panelSave.add(save);
panelSave.add(cancel);
panelSave.add(new Label(" "));
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(panelUserConf, BorderLayout.NORTH);
contentPane.add(DLGINFO, BorderLayout.CENTER);
contentPane.add(panelSave, BorderLayout.SOUTH);
//保存按鈕的事件處理
save.addActionListener(
new ActionListener() {
public void actionPerformed (ActionEvent a) {
int savePort;
String inputIP;
//判斷端口號是否合法
try{
userInputIp = "" + InetAddress.getByName(inputIp.getText());
userInputIp = userInputIp.substring(1);
}
catch(UnknownHostException e){
DLGINFO.setText(
" 錯誤的IP地址!");
return;
}
//userInputIp = inputIP;
//判斷端口號是否合法
try{
savePort = Integer.parseInt(inputPort.getText());
if(savePort<1 || savePort>65535){
DLGINFO.setText(" 偵聽端口必須是0-65535之間的整數!");
inputPort.setText("");
return;
}
userInputPort = savePort;
dispose();
}
catch(NumberFormatException e){
DLGINFO.setText(" 錯誤的端口號,端口號請填寫整數!");
inputPort.setText("");
return;
}
}
}
);
//關閉對話框時的操作
this.addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent e){
DLGINFO.setText(" 默認連接設置為 127.0.0.1:8888");
}
}
);
//取消按鈕的事件處理
cancel.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
DLGINFO.setText(" 默認連接設置為 127.0.0.1:8888");
dispose();
}
}
);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -