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

📄 mainframe.java

📁 java编写的一个网络象棋,通过对方ip连接,开始游戏,同时可以进行聊天,聊天部分通过线程实现,可以同时进行多个会话.
💻 JAVA
字号:
package cchess;

import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.MaskFormatter;
import chatAndNet.ClientThread;
import chatAndNet.ServerThread;

import net.*;

public class MainFrame extends JFrame{

/*	{
		try {
			javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
		} catch(Exception e) {
			e.printStackTrace();
		}
	}*/

	ImageIcon noIcon = new ImageIcon("Image" + File.separator + "no.gif");
	Image frameIconImage = getToolkit().getImage("Image" + File.separator + "frame.gif");
	ImageIcon saveIcon = new ImageIcon("Image" + File.separator + "save.gif");	
	ImageIcon loadIcon = new ImageIcon("Image" + File.separator + "load.gif");
	ImageIcon connectIcon = new ImageIcon("Image" + File.separator + "connect.gif");
	ImageIcon disconnectIcon = new ImageIcon("Image" + File.separator + "disconnect.gif");
	ImageIcon connectChatIcon = new ImageIcon("Image" + File.separator + "connectChat.gif");
	
	JPanel contentPane = new JPanel();
	TotalPanel mainPane;
	
	JToolBar mainToolBar = new JToolBar();
		JButton saveButton = new JButton(saveIcon);
		JButton loadButton = new JButton(loadIcon);
		JToolBar.Separator toolBarSeparator = new JToolBar.Separator();
		JButton connectButton = new JButton(connectIcon);
		JButton connectChatButton = new JButton(connectChatIcon);
	JMenuBar mainMenuBar = new JMenuBar();
		JMenu systemMenu = new JMenu("系统");
			JMenuItem saveGameMI = new JMenuItem("保存游戏...", saveIcon);
			JMenuItem loadGameMI = new JMenuItem("加载游戏...", loadIcon);
			JSeparator separator = new JSeparator();
			JMenuItem exitGameMI = new JMenuItem("退出游戏", noIcon);
		JMenu netMenu = new JMenu("网络");
			JMenuItem connectMI = new JMenuItem("连接游戏玩家", connectIcon);
			JMenuItem connectChatMI = new JMenuItem("添加聊天", connectChatIcon);
			JMenuItem localIpMI = new JMenuItem("查看本机IP", noIcon);
			JSeparator separator2 = new JSeparator();
			JMenuItem playerColorChangeMI = new JMenuItem("交换先后手", noIcon);
		JMenu helpMenu = new JMenu("关于");
		JMenuItem aboutMI = new JMenuItem("关于我们");
		
	public JFrame inputIpFrame;
	public JFormattedTextField ipText;
	public String serverIp = "000.000.000.000";
    public String localip = null ;
    public String iptemp = null;
	public static JTabbedPane tablePane;

	//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>	

	//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	
	public MainFrame(String title){
		super(title);
		this.getContentPane().setBackground(Color.LIGHT_GRAY);
		toolBarSet();
		menuSet();
		panelSet();
		setContentPane(contentPane);
		//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

        //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		this.setSize(500, 760);
		this.setIconImage(frameIconImage);
		this.setExtendedState(JFrame.MAXIMIZED_BOTH);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);	
	}
	
	public void chatAndNetSet(){
		try{
			iptemp =  InetAddress.getLocalHost().toString();   
			for(int i=0;i<iptemp.length();i++){
			    if(iptemp.charAt(i)=='/'){
			    	localip = iptemp.substring(i+1);
			    }
			}
			ServerSocket serverSocket = new ServerSocket(5000);
			Socket connectToClient = null;
			
			while(true){
				//循环等待...监听...
				System.out.println("connectToClient is going to made");
				connectToClient = serverSocket.accept();
				System.out.println("connectToClient made");
				new ServerThread(connectToClient);
			}
		}catch(Exception e){
			System.out.println("exception in- private void chatAndNetSet:" + e.toString());
		}
	}
	private void toolBarSet(){
		contentPane.setLayout(new BorderLayout());
		saveButton.setToolTipText("保存游戏");		
		saveButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				save();
			}
		});

		loadButton.setToolTipText("加载游戏");	
		loadButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				new ReadChessManualFrame();
			}
		});

		connectButton.setToolTipText("连接玩家");		
		connectButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
                 connect();
			}
		});

		connectChatButton.setToolTipText("添加聊天");		
		connectChatButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				connectChat();
			}
		});

		
		mainToolBar.add(saveButton);
		mainToolBar.add(loadButton);
		mainToolBar.add(toolBarSeparator);
		mainToolBar.add(connectButton);
		mainToolBar.add(connectChatButton);
		contentPane.add(mainToolBar, BorderLayout.NORTH);
	}
	
	private void menuSet(){
		saveGameMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				save();
			}
		});
		systemMenu.add(saveGameMI);
		loadGameMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				load();
			}
		});
		systemMenu.add(loadGameMI);
		systemMenu.add(separator);
		exitGameMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				System.exit(0);
			}
		});
		systemMenu.add(exitGameMI);
		mainMenuBar.add(systemMenu);
		
		connectMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				connect();
			}
		});
		netMenu.add(connectMI);
		connectChatMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				connectChat();
			}
		});
		netMenu.add(connectChatMI);
		localIpMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				JOptionPane.showMessageDialog(null,"本地IP :     "+Net.getMyAdress());
			}
		});
		netMenu.add(localIpMI);
		netMenu.add(separator2);
		playerColorChangeMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				if(JOptionPane.showConfirmDialog(null,"交换先后手并重新开局?","交换先后手",0)==JOptionPane.YES_OPTION){
					mainPane.net.sendMessage("changewhofirst");      //发出请求信息
					mainPane.chessPane.iAgreeRestart = true;
				}
			}
		});
		netMenu.add(playerColorChangeMI);
		mainMenuBar.add(netMenu);
		
		aboutMI.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent ae){
				JOptionPane.showMessageDialog(null, "计算机0412班:\n" +
						"               全体同学", "关于我们", JOptionPane.INFORMATION_MESSAGE);
			}
		});
		helpMenu.add(aboutMI);
		mainMenuBar.add(helpMenu);
		
		this.setJMenuBar(mainMenuBar);
	}
	
	private void panelSet(){
		tablePane = new JTabbedPane();
		mainPane = new TotalPanel();
		contentPane.add(mainPane,BorderLayout.CENTER);
	}

	///////////////////////////////////////////////////////////////////////	
	//the method below is message handler(s).
	
	private void save(){
		JOptionPane.showMessageDialog(null, "save");
	}
	private void load(){
		JOptionPane.showMessageDialog(null, "load");
	}
	private void connect(){
		String ipStr=JOptionPane.showInputDialog(null,"请输入要连接的IP:","222.20.7.61");
		mainPane.net.connectServer(ipStr);
	}
	private void connectChat(){
		InputIpFrame();
	}
	//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
	private void InputIpFrame(){
		MaskFormatter maskFormatter;
		JButton okbutton = new JButton("确认");
		try{
			inputIpFrame = new JFrame("请输入IP...");
			maskFormatter = new MaskFormatter("###.###.###.###");
			ipText = new JFormattedTextField(maskFormatter);
			ipText.setValue("222.020.007.066");
			
			okbutton.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
				serverIp = ipText.getText();
				inputIpFrame.dispose();
				try{
					clientConnect(serverIp);
				}catch(IOException ie){
						ie.printStackTrace();
					}      
				}
			});

			JPanel ipPanel = new JPanel();
			inputIpFrame.setLayout(new FlowLayout());
			ipPanel.setLayout(new BorderLayout());
			ipPanel.add(ipText,BorderLayout.NORTH);
			ipPanel.add(okbutton,BorderLayout.SOUTH);
			inputIpFrame.getContentPane().add(ipPanel);
			inputIpFrame.setSize(200,100);
			inputIpFrame.setLocation(350,330);
			inputIpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			inputIpFrame.setVisible(true);
		}catch(Exception e){e.printStackTrace();}
	}
	private void clientConnect(String ip) throws IOException{
		String strip = ip;
		byte[] b = {strToNum(strip.substring(0,3)),strToNum(strip.substring(4,7)),strToNum(strip.substring(8,11)),strToNum(strip.substring(12,15))};
	 	new ClientThread(InetAddress.getByAddress(b),5000,serverIp);
	}
	
	private byte strToNum(String str){
		return (byte)(((int)str.charAt(0) - 48) * 100 +((int)str.charAt(1) - 48) * 10 + ((int)str.charAt(2) - 48)); 
	}
}

⌨️ 快捷键说明

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