package
com.cuit.library.source.server;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ServerFrame extends JFrame
{
/**
*
*/
private static final long serialVersionUID =
4320272482766976925L;
static String username="fuwuqi";
private Image img=new
ImageIcon("image/fuwuqi.ico").getImage();
public static JTextArea fuwuqizhuangtai;
public static int
zhuangtai=1; //0是停止,1是开始,2是暂停
public JButton zantingButton;
public JButton tingzhiButton;
public JButton kaishiButton;
static public JScrollPane scrollPane;
/**
* Launch the application
* @param args
*/
public static void main(String args[]) throws
IOException{
try {
ServerFrame
frame = new ServerFrame();
frame.setVisible(true);
frame.setBounds(300,250,300,280);
frame.setResizable(false);
} catch (Exception e) {
e.printStackTrace();
}
ServerSocket ss=new
ServerSocket(3000);
System.out.println("stsart..."+ss+username);
try{
while(true){
Socket
s=ss.accept();
System.out.println("用户"+s.getInetAddress().getHostAddress()+"请求连接!");
fuwuqizhuangtai.append("用户"+s.getInetAddress().getHostAddress()+"请求连接!\n");
fuwuqizhuangtai.selectAll();
try{
new
ServerBeginThread(s);
}
catch(IOException
e){
s.close();
}
System.out.println(username);
}
}
finally{
ss.close();
}
}
/**
* Create the frame
*/
public ServerFrame() {
super();
getContentPane().setLayout(new
GridBagLayout());
setTitle("图书馆管理系统服务器");
setBounds(300, 250, 300,
280);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setIconImage(img);
kaishiButton = new
JButton();
kaishiButton.setFont(new
Font("新宋体", Font.PLAIN, 12));
kaishiButton.setToolTipText("开始");
kaishiButton.setText("运行");
kaishiButton.setEnabled(false);
final GridBagConstraints
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.insets = new
Insets(0, 0, 0, 30);
gridBagConstraints.gridy =
0;
gridBagConstraints.gridx =
0;
getContentPane().add(kaishiButton,
gridBagConstraints);
kaishiButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
fuwuqizhuangtai.append("服务器已开始运行.......\n");
fuwuqizhuangtai.selectAll();
zhuangtai=1;
zantingButton.setEnabled(true);
tingzhiButton.setEnabled(true);
kaishiButton.setEnabled(false);
}
});
zantingButton = new
JButton();
zantingButton.setFont(new
Font("新宋体", Font.PLAIN, 12));
zantingButton.setToolTipText("暂停");
zantingButton.setText("暂停");
zantingButton.setEnabled(true);
final GridBagConstraints
gridBagConstraints_1 = new GridBagConstraints();
gridBagConstraints_1.gridy =
0;
gridBagConstraints_1.gridx =
1;
getContentPane().add(zantingButton,
gridBagConstraints_1);
zantingButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
fuwuqizhuangtai.append("服务器已暂停运行.......\n");
fuwuqizhuangtai.selectAll();
zhuangtai=2;
kaishiButton.setEnabled(true);
tingzhiButton.setEnabled(true);
zantingButton.setEnabled(false);
}
});
tingzhiButton = new
JButton();
tingzhiButton.setFont(new
Font("新宋体", Font.PLAIN, 12));
tingzhiButton.setToolTipText("停止");
tingzhiButton.setText("停止");
final GridBagConstraints
gridBagConstraints_2 = new GridBagConstraints();
gridBagConstraints_2.gridy =
0;
gridBagConstraints_2.gridx =
2;
getContentPane().add(tingzhiButton,
gridBagConstraints_2);
tingzhiButton.addActionListener(new
ActionListener() {
public void
actionPerformed(ActionEvent e) {
JOptionPane
optionPane=new JOptionPane();
optionPane.setFont(new
Font("新宋体", Font.PLAIN, 12));
int
queding=JOptionPane.showConfirmDialog(null, //
选“是”queding=0,选“否”queding=1
"选“是(Y)”,程序退出\n选“否(N)”,取消操作",
"确定停止程序",
JOptionPane.YES_NO_OPTION);
if(queding==0){
fuwuqizhuangtai.append("服务器已停止运行!\n");
System.exit(1);
}
}
});
final JLabel label = new
JLabel();
label.setFont(new
Font("新宋体", Font.PLAIN, 12));
label.setText("服务器状态:");
final GridBagConstraints
gridBagConstraints_3 = new GridBagConstraints();
gridBagConstraints_3.insets =
new Insets(15, 0, 5, 195);
gridBagConstraints_3.gridwidth
= 3;
gridBagConstraints_3.gridy =
1;
gridBagConstraints_3.gridx =
0;
getContentPane().add(label,
gridBagConstraints_3);
scrollPane = new
JScrollPane();
final GridBagConstraints
gridBagConstraints_4 = new GridBagConstraints();
gridBagConstraints_4.gridwidth
= 3;
gridBagConstraints_4.ipadx =
30;
gridBagConstraints_4.ipady =
160;
gridBagConstraints_4.fill =
GridBagConstraints.BOTH;
gridBagConstraints_4.insets =
new Insets(0, 0, 0, 0);
gridBagConstraints_4.gridy =
2;
gridBagConstraints_4.gridx =
0;
getContentPane().add(scrollPane,
gridBagConstraints_4);
fuwuqizhuangtai = new
JTextArea();
scrollPane.setAutoscrolls(true);
scrollPane.setViewportView(fuwuqizhuangtai);
fuwuqizhuangtai.setEditable(false);
fuwuqizhuangtai.append("服务器已开始运行!\n");
//
}
}/**/