⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 serverframe.java

📁 JAVA写的聊天市 JAVA写的聊天市
💻 JAVA
字号:
package server;

import javax.swing.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import javax.swing.border.*;

/**
 * <p>Title: 聊天室服务器</p>
 * <p>Description: 聊天室</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company:BUAA</p>
 * @author WengHaibin
 * @version 1.0
 */

public class ServerFrame
    extends JFrame
    implements ActionListener,Runnable {
  public static Color DarkColor = new Color(55, 77, 118); //暗色
  public static Color LightColor = new Color(111, 146, 212); //亮色
  private static int port=5000;
  private JScrollPane scrollPane=new JScrollPane();
  JEditorPane messagePane=new JEditorPane();
  private Border border1;
  
  private boolean startStatus=false;


  Dimension faceSize = new Dimension(580, 500);
  Image icon;
  private int counter = 0;
	
	TextArea messageTextArea=new TextArea();
  JPanel toolBar = new JPanel(); //工具栏
  JPanel actionPanel = new JPanel(); //用户操作栏
  JPanel contentPanel = new JPanel(); //容器
  Border emptyBorder = BorderFactory.createEmptyBorder(); //未选中时的边框
  JButton startButton = new JButton(); //"开始"
  JButton stopButton = new JButton();  //"停止"
  JButton aboutButton = new JButton(); //"关于"

  //设置关于窗口
  AboutDialog aboutDialog;

  public static JTextField statusField = new JTextField(
      "欢迎来到聊天室服务器");
  ImageIcon imgStart, imgStop, imgAbout;


  public ServerFrame() {
    initResource();
    initUI();

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.pack();
    this.setSize(faceSize);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    this.setLocation( (int) (screenSize.width - faceSize.getWidth()) / 2,
                     (int) (screenSize.height - faceSize.getHeight()) / 2);
    this.setResizable(false);
    this.setTitle("聊天室服务器"); //设置标题
    this.setIconImage(icon); //设置程序图标

  }

  /**
   * 初始化系统所需要的资源
   */
  public void initResource() {
    //程序图标
    icon = getImage("images/app.gif");
    
    imgStart = new ImageIcon(getImage("images/start.gif"));
    imgStop = new ImageIcon(getImage("images/stop.gif"));
    imgAbout = new ImageIcon(getImage("images/about.gif"));
    
    aboutDialog =new AboutDialog(this);
  }

  /**
   * 初始化用户界面
   */
  public void initUI() {
    //界面整体布局
    Border border = BorderFactory.createBevelBorder(BevelBorder.LOWERED,
        new Color(45, 92, 162),
        new Color(43, 66, 97),
        new Color(45, 92, 162),
        new Color(84, 123, 200));
    BorderLayout borderLayout = new BorderLayout();
    toolBar.setBackground(DarkColor);
    toolBar.setBorder(border);
    toolBar.setPreferredSize(new Dimension(580, 48));
    toolBar.setMinimumSize(new Dimension(580, 48));
    toolBar.setLayout(new FlowLayout(FlowLayout.LEFT));
    actionPanel.setBackground(LightColor);
    actionPanel.setBorder(border);
    actionPanel.setPreferredSize(new Dimension(160, 400));
    actionPanel.setMinimumSize(new Dimension(160, 380));
    contentPanel.setBackground(DarkColor);
    contentPanel.setBorder(border);
    contentPanel.setPreferredSize(new Dimension(420, 380));
    contentPanel.setMinimumSize(new Dimension(420, 380));
    
    messagePane.setPreferredSize(new Dimension(400, 410));

    this.getContentPane().setLayout(borderLayout);
    this.getContentPane().add(toolBar, BorderLayout.NORTH);
    this.getContentPane().add(actionPanel, BorderLayout.CENTER);    //actionPanel是”开始“
    this.getContentPane().add(contentPanel, BorderLayout.EAST);  //content是真正游戏的地方
    
    
    //加入信息面板
    contentPanel.add(scrollPane);
    scrollPane.setHorizontalScrollBarPolicy(JScrollPane.
                                            HORIZONTAL_SCROLLBAR_NEVER); //这是水平的拉条
    scrollPane.setVerticalScrollBarPolicy(JScrollPane.
                                          VERTICAL_SCROLLBAR_ALWAYS); //这是垂直的拉条
    border1=BorderFactory.createEmptyBorder();
    scrollPane.setBorder(border1);
    scrollPane.getViewport().add(messagePane,null);

    //加入开始按钮
    actionPanel.add(startButton);
    startButton.setBorder(emptyBorder);
    startButton.setIcon(imgStart);
    startButton.addActionListener(this);

    //加入关闭按钮
    actionPanel.add(stopButton);
    stopButton.setBorder(emptyBorder);
    stopButton.setIcon(imgStop);
    stopButton.addActionListener(this);

    //加入关于
    toolBar.add(aboutButton);
    aboutButton.setBorder(emptyBorder);
    aboutButton.setIcon(imgAbout);
    aboutButton.addActionListener(this);
  }


  /**
   * 通过给定的文件名获得图像
   * @param filename 给定图像的文件名
   * @return 图像
   */
  Image getImage(String filename) {
    URLClassLoader urlLoader = (URLClassLoader)this.getClass().
        getClassLoader();
    URL url = null;
    Image image = null;
    url = urlLoader.findResource(filename);
    image = Toolkit.getDefaultToolkit().getImage(url);
    MediaTracker mediatracker = new MediaTracker(this);
    try {
      mediatracker.addImage(image, 0);
      mediatracker.waitForID(0);
    }
    catch (InterruptedException _ex) {
      image = null;
    }
    if (mediatracker.isErrorID(0)) {
      image = null;
    }

    return image;
  }

  public void actionPerformed(ActionEvent e){
    Object obj = e.getSource();
    if (obj == startButton) { //开始
    	if(!startStatus){
    	new Thread(this).start();
    	startStatus=false;
    } 
    }
    else if (obj == stopButton) { //关闭
      //ui.refresh();
    }
    else if (obj == aboutButton) { //关于
      aboutDialog.setVisible(true);
    }
  }
  
  public void run(){
  	Server server=new Server(this,port);
  }

  public static void main(String args[]) throws Exception{
    JFrame.setDefaultLookAndFeelDecorated(true);
    JDialog.setDefaultLookAndFeelDecorated(true);
    ServerFrame sFrame = new ServerFrame();
 
    sFrame.setVisible(true);
    
    
      if (args[0].equals("-start"))
        {
            while(true)
            {
               
                log("this is a logging");

                // Sleep for 5 secs
                Thread.currentThread().sleep(5000);

                // Check for termination
                if (stop)
                {
                    log("Service stopped.");
                    break;
                }
            }
        }
        else
        // Stop the service
        if (args[0].equals("-stop"))
        {
            // Set the termination flag
            stop = true;
        }
    
    
    
    
    static void log(String msg) throws IOException
    {
    	PrintWriter pw new PrintWriter((new FileWriter ("server.log"),true);
      pw.println(msg);
      pw.close();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -