家鄉の戰隊實訓項目博客二

                                                                            家鄉の戰隊:黃金點項目博客二java

 1.團隊風采git

組長:唐宇      16012020web

隊員:王松鶴  16012016sql

           劉計     16012024數據庫

           龐嘯天 16012011服務器

 

2.碼雲地址多線程

https://gitee.com/wcnma/home_troops/tree/masterapp

 

3. 團隊分工socket

唐宇:團隊組長,靈魂核心,領導組員完成java任務,是組員的導向標
ide

評分:9

王松鶴:團隊技術擔當,高端技術人才,完成主要的java項目

評分:10

劉計:掌握了java的基礎知識,擅長與客戶交談,不管客戶都能憑三寸不爛之舌讓客戶滿意(固然咱們的產品也是棒棒的),完成展現項目

評分:7

龐嘯天:完成Java項目細節上的部分,團隊文案,完成博客

評分:8

 

4.代碼展現

 

項目要求:

 

阿超的課都是下午兩點鐘,這時班上很多的同窗都昏昏欲睡,爲了讓你們興奮起來,阿超讓同窗玩一個叫「黃金點」的遊戲:

 

N個同窗(N一般大於10),每人寫一個0~100之間的有理數 (不包括0或100),交給裁判,裁判算出全部數字的平均值,而後乘以0.618(所謂黃金分割常數),獲得G值。提交的數字最靠近G(取絕對值)的同窗獲得N分,離G最遠的同窗獲得-2分,其餘同窗得0分。記錄每一次遊戲每名同窗的數字和分數。在實訓一的基礎上,自學書中第9章,第14章。將黃金點遊戲開發成一個圖形界面的,在局域網內,全班同窗能夠一塊兒玩耍的小遊戲。提供用戶註冊登陸功能,與數據庫進行鏈接,能夠查看該用戶以往的記錄,根據同用以往的記錄設立初級場和高級場,讓用戶有更友好的體驗。

 代碼部分:

Arrary.java

package wsh;

public class Arrary {
	public static int[] arrary = new int[3];
}


Client.java

package wsh;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 * 
 * @author Administrator
 *
 */

public class Client {
    public static void main(String[] args) {
        new ClientFrame("客戶端");
    }
}

/*
 * 繼承Jframe類表示該類能夠建立一個窗口程序了,
 * 實現ActionListener:動做監聽,就是一個事件發送後應該執行什麼就要從新它的方法
 * 實現Runnable:實現多線程,該窗口是個客戶端窗口,要開啓一個線程接收顯示服務器發過來的信息
 */
class ClientFrame extends JFrame implements ActionListener, Runnable {

    private static final long serialVersionUID = 1L;

    private Socket socket;
    OutputStream os = null;
    BufferedWriter bw = null;
    
    JTextArea message;
    JScrollPane scroll;
    JTextField input;
    JButton submit;
    JPanel panel;

    String uname = "";
    // 聊天頁面
    JPanel chat;

    // 登陸頁面
    JPanel login;
    JTextField username;
    JButton ok;

    public ClientFrame(String name) {
        super(name);
    
        message = new JTextArea();
        message.setEditable(false);
        scroll = new JScrollPane(message);
        input = new JTextField(20);
        submit = new JButton("發送");
        panel = new JPanel();

        panel.add(input);
        panel.add(submit);
        chat = new JPanel(new BorderLayout());
        chat.add(scroll, "Center");
        chat.add(panel, "South");
        
        
        // 在建立客戶端窗體是就要把客戶端與服務端鏈接
        try {
            //127.0.0.1表示本機地址,地址好端口均可以改,這要看服務器那邊是什麼地址和端口
            socket = new Socket("127.0.0.1", 6666);
            os = socket.getOutputStream();
            bw = new BufferedWriter(new OutputStreamWriter(os));
            
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        
        /*    在建立C端窗體的時候也應該開啓一個線程接收顯示來自服務器的信息
         *     爲何要開啓一個線程呢?由於在這個窗體裏既要實現消息的發送,
         *     也要接收信息,並且兩個不能按順序進行,也互不干擾,因此開啓一個線程時時刻刻等着S端發來的信息
         */
        //接收信息
        new Thread(this).start();
        
        //提交按鈕工做監聽器,點擊提交時觸發
        submit.addActionListener(this);

        login = new JPanel();
        username = new JTextField(20);
        ok = new JButton("肯定");
        ok.addActionListener(this);
        JLabel label = new JLabel("請您選擇功能");
        label.setFont(new Font(Font.DIALOG, Font.BOLD, 28));
        label.setForeground(Color.red);
        login.add(label);
        login.add(username);
        login.add(ok);

        this.add(chat);
        this.add(login);

        this.setResizable(false);
        this.setBounds(400, 100, 400, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == submit) {
            // 若是點擊提交按鈕,則表示須要將信息發送出去。
            String str = null;
            //如下三行是建立發送時間的代碼
            Date date = new Date();
            SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
            String dateStr = format.format(date);
            
            str = uname+" "+dateStr+"  "+input.getText();
            
            try {
                bw.write(str);
                bw.newLine();
               System.out.println("客戶端發送了:"+str);
                bw.flush();
            } catch (IOException e1) {
                System.out.println("沒法發送,服務器鏈接異常!");
            }
            
//            System.out.println(str);
            
        } else if (e.getSource() == ok) {// 用戶登陸
            uname = username.getText();// 獲取文本框輸入的文本信息
            if ("".equals(uname)) {
                JOptionPane.showMessageDialog(this, "用戶名不能爲空!");
            } else {
                login.setVisible(false);
                chat.setVisible(true);
                this.setTitle(uname + " 的客戶端");
                this.add(chat);
            }
        }
    }

    //run方法裏面的是接受S端信息和將信息顯示的代碼
    @Override
    public void run() {
        BufferedReader br = null;
        InputStream is = null;
        String str = null;
        try {
            is = socket.getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            while (true) {
                str = br.readLine()+"\r\n";
                message.append(str);
            }
        } catch (IOException e) {
            System.out.println("沒法發送,服務器鏈接異常!");
        }
    }

}

DBHelper.java

package wsh;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

class DBHelper{
	public Connection connection;
	public PreparedStatement preparedStatement;
	
	public DBHelper() throws SQLException, ClassNotFoundException{
		Class.forName("org.mariadb.jdbc.Driver");
		connection = DriverManager.getConnection("jdbc:mariadb://localhost:3306/gd","test","123");
	}
	
	public ResultSet DB(String sql) throws ClassNotFoundException, SQLException{
		preparedStatement = connection.prepareStatement(sql);
	    ResultSet resultSet = preparedStatement.executeQuery();
	    return resultSet;
	}
	
	public void excuteSql(String sql) throws SQLException{
		preparedStatement = connection.prepareStatement(sql);
	    preparedStatement.executeUpdate();
	}
	
	public void downConn() throws SQLException{
		connection.close();
	}
	
}

Game.java

package wsh;

class Game {
	public void chuji() {
		shuru w = new shuru();
		w.game("初級場");
	}

	public void gaoji() {
		shuru w = new shuru();
		w.game("高級場");
	}
}

GoldenPoint.java

package wsh;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner; 
public class GoldenPoint {
public static void main(String[] args){
    GoldenPoint gd=new GoldenPoint();
    gd.goldPoint();
}
public void goldPoint(){
    HashMap<String,Double> inputMap=new HashMap<String,Double>();
    HashMap<String,Double> scoreMap=new HashMap<String,Double>();
    String name="";
    Double inputScore;
    int time;
    Double sum=0.0;
    Double aver=0.0;
    Scanner scan=new Scanner(System.in);
    for(int i=0;i<3;i++){
        System.out.println("請輸入第"+(i+1)+"個參加者的姓名:");
        name=scan.next();
        System.out.println("請輸入第一輪的數字:");
        System.out.println("數字範圍在0~100!");
        inputScore=scan.nextDouble();
        if (inputScore<0||inputScore>100)
        {System.out.println("輸入數字錯誤");
        break;}
        else
         inputMap.put(name, inputScore);
         scoreMap.put(name,(double) 0);
         sum+=inputScore;
    }
    aver=sum/3*0.618;
    System.out.println("aver="+aver);
    this.findWinner(inputMap, scoreMap, aver,3);
    this.show(scoreMap);
            
    
    System.out.println("遊戲結束");
}
public void findWinner(HashMap<String,Double> inputMap,HashMap<String,Double> scoreMap,Double aver,int renshu){    
    Double temp;
    Double temp0;
    List<String> latest=new ArrayList<String>();
    List<String> farthest=new ArrayList<String>();
    Iterator iter = inputMap.entrySet().iterator();
    Map.Entry entry = (Map.Entry) iter.next();
    Double input = (Double) entry.getValue();
    String key0 = (String) entry.getKey();
    latest.add(key0);
    farthest.add(key0);
    temp0=temp=Math.abs(aver-input);
    while (iter.hasNext()) {    
        entry = (Map.Entry) iter.next();
        String key = (String) entry.getKey();
        input = (Double) entry.getValue();
        Double temp1=Math.abs(aver-input);
        if(temp>temp1){
            temp=temp1;
             latest.clear();
             latest.add(key);
        }else if(temp==temp1){
            latest.add(key);
        }
        if(temp0<temp1){
            temp0=temp1;
            farthest.clear();
            farthest.add(key);}
        else if(temp0==temp1){
            farthest.add(key);
        }
    }
    iter = scoreMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry0 = (Map.Entry) iter.next();
        String key = (String) entry0.getKey();
        Double score =(Double) entry0.getValue();
        if(this.containList(key, latest)){
            score=score+renshu;
            scoreMap.put(key, score);
            }
        if(this.containList(key, farthest)){
            score=score-2;
            scoreMap.put(key, score);
        }
        }
}
public boolean containList(String str,List<String> list){
    for(int i=0;i<list.size();i++){
        if(str.equals(list.get(i))){
            return true;
        }
    }
    return false;
}
public void show(HashMap<String,Double> scoreMap){
    System.out.println("得分狀況:");
    Iterator iter = scoreMap.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry entry0 = (Map.Entry) iter.next();
        String key = (String) entry0.getKey();
        Double score =(Double) entry0.getValue();
        System.out.println(key+":"+score);
    }
}
}

 Login.java

package wsh;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

//1.定義Login類,
public class Login {

	// 1.在類中定義主函數
	public static void main(String[] args) {
		// 2.在主函數中,實例化Login類的對象,調用初始化界面的方法。
		Login login = new Login();
		login.initUI();

	}

	// 1.在類中定義初始化界面的方法;

	public void initUI() {
		// 3.在initUI方法中,實例化JFrame類的對象。
		JFrame frame = new JFrame();
		// 4.設置窗體對象的屬性值:標題、大小、顯示位置、關閉操做、佈局、禁止調整大小、可見、...
		frame.setTitle("Login");// 設置窗體的標題
		frame.setSize(400, 650);// 設置窗體的大小,單位是像素
		frame.setDefaultCloseOperation(3);// 設置窗體的關閉操做;3表示關閉窗體退出程序;二、一、0
		frame.setLocationRelativeTo(null);// 設置窗體相對於另外一個組件的居中位置,參數null表示窗體相對於屏幕的中央位置
		frame.setResizable(false);// 設置禁止調整窗體大小

		// 實例化FlowLayout流式佈局類的對象,指定對齊方式爲居中對齊,組件之間的間隔爲5個像素
		FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);
		// 實例化流式佈局類的對象
		frame.setLayout(fl);

		// 5.實例化元素組件對象,將元素組件對象添加到窗體上(組件添加要在窗體可見以前完成)。
		// 實例化ImageIcon圖標類的對象,該對象加載磁盤上的圖片文件到內存中,這裏的路徑要用兩個\
		ImageIcon icon = new ImageIcon("/root/圖片/b.gif");
		// 用標籤來接收圖片,實例化JLabel標籤對象,該對象顯示icon圖標
		JLabel labIcon = new JLabel(icon);
		// 設置標籤大小
		// labIcon.setSize(30,20);setSize方法只對窗體有效,若是想設置組件的大小隻能用
		Dimension dim = new Dimension(400, 300);
		labIcon.setPreferredSize(dim);
		// 將labIcon標籤添加到窗體上
		frame.add(labIcon);

		// 實例化JLabel標籤對象,該對象顯示"帳號:"
		JLabel labName = new JLabel("帳號:");
		// 將labName標籤添加到窗體上
		frame.add(labName);

		// 實例化JTextField標籤對象
		JTextField textName = new JTextField();
		Dimension dim1 = new Dimension(300, 30);
		// textName.setSize(dim);//setSize這方法只對頂級容器有效,其餘組件使用無效。
		textName.setPreferredSize(dim1);// 設置除頂級容器組件其餘組件的大小
		// 將textName標籤添加到窗體上
		frame.add(textName);

		// 實例化JLabel標籤對象,該對象顯示"密碼:"
		JLabel labpass = new JLabel("密碼:");
		// 將labpass標籤添加到窗體上
		frame.add(labpass);

		// 實例化JPasswordField
		JPasswordField textword = new JPasswordField();
		// 設置大小
		textword.setPreferredSize(dim1);// 設置組件大小
		// 添加textword到窗體上
		frame.add(textword);

		// 實例化JButton組件
		JButton button = new JButton();
		// 設置按鈕的顯示內容
		Dimension dim2 = new Dimension(100, 30);
		button.setText("登陸");

		JButton button2 = new JButton();
		Dimension dim3 = new Dimension(100, 30);
		button2.setText("註冊");

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String login = textName.getText();
				String password = textword.getText();
				try {
					denglu ss = new denglu(login, password);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		button2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String login = textName.getText();
				String password = textword.getText();
				try {
					zhuce aa = new zhuce(login, password);
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
		// 設置按鈕的大小
		button.setSize(dim2);
		frame.add(button);
		button.setSize(dim3);
		frame.add(button2);

		frame.setVisible(true);// 設置窗體爲可見
	}

}

  
Server.java

 

package wsh;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;


public class Server {
    //用於保存Socket的集合,也能夠說是把個C端與S端的一個鏈接通道保存起來
    //做用:服務器將接收到的信息發給集合裏的因此socket,也就是發給每一個C端
    public static List<Socket> list = new ArrayList<Socket>();

    public static void main(String[] args) {
        new ServerFrame("黃金點");
    }

}

/*
 * 繼承Jframe類表示該類能夠建立一個窗口程序了,
 * 實現ActionListener:動做監聽,在S端點擊「啓動服務器」是要執行的代碼
 * 實現Runnable:實現多線程,該窗口是個客戶端窗口,要開啓一個線程接收顯示服務器發過來的信息
 */
class ServerFrame extends JFrame implements ActionListener, Runnable {

    private static final long serialVersionUID = 1L;

    private ServerSocket serverSocket = null;

    private Socket socket;

    private JButton start;
    private JTextArea message;

    public ServerFrame(String name) {
        super(name);

        start = new JButton("黃金點");
        start.addActionListener(this);
        message = new JTextArea();
        message.setEditable(false);
        this.add(start, "North");
        this.add(message, "Center");

        this.setResizable(false);
        this.setBounds(0, 0, 400, 300);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == start) {
            // 若是點擊的按鈕是開始按鈕。則啓動服務器。
            try {
                serverSocket = new ServerSocket(6666);
                message.setText("服務器已啓動。。。");
                
                //單擊「啓動服務器」開啓一個線程用於獲取用戶上線的狀況
                new Thread(this).start();
                
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

    //獲取C端上線的狀況
    @Override
    public void run() {
        String address = null;
        int port = 0;
        //用一個死循環一直讓S端開啓接收C端的鏈接,將C端的IP和端口顯示在面板上
        //若是用循環的話就只能接收一次
        while (true) {
            try {
                socket = serverSocket.accept();
                Server.list.add(socket);
                address = socket.getInetAddress().getHostAddress();
                port = socket.getPort();
                message.append("\r\nip/" + address + ":" + port + "\t上線了");
                
               
            } catch (IOException e) {
//                System.out.println(address + ":" + port + "\t退出了服務器");
            }
        }
    }

}

  ServerReverse.java

package wsh;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.math.RoundingMode;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.DecimalFormat;

/**
 * 文件傳輸Server端
 * 
 */
public class ServerReverse extends ServerSocket {

	private static final int SERVER_PORT = 8899;

	private static DecimalFormat df = null;

	static {

		df = new DecimalFormat("#0.0");
		df.setRoundingMode(RoundingMode.HALF_UP);
		df.setMinimumFractionDigits(1);
		df.setMaximumFractionDigits(1);
	}

	public ServerReverse() throws Exception {
		super(SERVER_PORT);
	}

	public void load() throws Exception {
		while (true) {
			Socket socket = this.accept();
			new Thread(new Task(socket)).start();
		}
	}

	class Task implements Runnable {

		private Socket socket;

		public Task(Socket socket) {
			this.socket = socket;
		}

		@Override
		public void run() {
	

				DataInputStream in1 = new DataInputStream(socket.getInputStream());
				DataOutputStream out1 = new DataOutputStream(socket.getOutputStream());
				
					int w = in1.readInt();
					Arrary.arrary[0] = w;
					System.out.println(Arrary.arrary[0]);
					out1.writeUTF("請等待");
				
				System.out.println("輸入完畢!");
		
		
	}
	
		/**
		 * 入口
		 * 
		 * @param args
		 */
		public void main(String[] args) {
			try {
				ServerReverse server = new ServerReverse();
				server.load();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

  bl.java

package wsh;

class bl {

	public static int sc;


}

  
denglu.java

package wsh;

import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.swing.JFrame;
import javax.swing.JLabel;

public class denglu {

	public denglu(String name, String password) throws ClassNotFoundException, SQLException {
		// 默認輸入的是字符串,因此建議測試的時候輸入字符串

		String userID = name;

		String psw1 = password;
		// *表明的是全部列
		String sql2 = "select * from test where 用戶名 =" + "'" + userID + "'" + "and 密碼=" + "'" + psw1 + "'";
		// 每次新建一個對象,就是新建一個鏈接connection
		String sql3 = "select 勝場 from test";
		DBHelper dbHelper = new DBHelper();
		DBHelper DBcha = new DBHelper();
		ResultSet rs = DBcha.DB(sql3);
		while (rs.next()) {
			int number = rs.getInt(1);
			bl.sc= number;
			break;
		}
		// 在數據庫中查詢結果,若結果存在返回登陸成功
		ResultSet rsResultSet = dbHelper.DB(sql2);
		if (rsResultSet.next() == true) {
			nmb q = new nmb();
			q.game();
			dbHelper.downConn();
		} else {
	        JFrame f = new JFrame("");
	        f.setSize(400, 150);
	        f.setLocationRelativeTo(null);
	        f.setLayout(null);
	        JLabel l = new JLabel("登陸失敗,請檢查您的用戶名和密碼是否正確");
	        l.setForeground(Color.red);
	        l.setBounds(50, 50, 300, 30);

	        f.add(l);

	        f.setVisible(true);
			dbHelper.downConn();
		}

	}
}

  nmb.java

package wsh;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

class ButtonListener implements ActionListener {

	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		String str = button.getText();

	}
}

//1.定義Login類,
public class nmb {

	// 1.在類中定義主函數
	public void game() {
		// 2.在主函數中,實例化Login類的對象,調用初始化界面的方法。
		nmb game = new nmb();
		game.gameUI();

	}

	// 1.在類中定義初始化界面的方法;

	public void gameUI() {
		// 3.在initUI方法中,實例化JFrame類的對象。
		JFrame frame = new JFrame();
		// 4.設置窗體對象的屬性值:標題、大小、顯示位置、關閉操做、佈局、禁止調整大小、可見、...
		frame.setTitle("黃金點");// 設置窗體的標題
		frame.setSize(400, 650);// 設置窗體的大小,單位是像素
		frame.setDefaultCloseOperation(3);// 設置窗體的關閉操做;3表示關閉窗體退出程序;二、一、0
		frame.setLocationRelativeTo(null);// 設置窗體相對於另外一個組件的居中位置,參數null表示窗體相對於屏幕的中央位置
		frame.setResizable(false);// 設置禁止調整窗體大小

		// 實例化FlowLayout流式佈局類的對象,指定對齊方式爲居中對齊,組件之間的間隔爲5個像素
		FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);
		// 實例化流式佈局類的對象
		frame.setLayout(fl);

		// 5.實例化元素組件對象,將元素組件對象添加到窗體上(組件添加要在窗體可見以前完成)。
		// 實例化ImageIcon圖標類的對象,該對象加載磁盤上的圖片文件到內存中,這裏的路徑要用兩個\
		ImageIcon icon = new ImageIcon("/root/圖片/b.gif");
		// 用標籤來接收圖片,實例化JLabel標籤對象,該對象顯示icon圖標
		JLabel labIcon = new JLabel(icon);
		// 設置標籤大小
		// labIcon.setSize(30,20);setSize方法只對窗體有效,若是想設置組件的大小隻能用
		Dimension dim = new Dimension(400, 300);
		labIcon.setPreferredSize(dim);
		// 將labIcon標籤添加到窗體上
		frame.add(labIcon);
		JLabel ts = new JLabel("1.低級場 2.高級場");
		frame.add(ts);
		JLabel sc = new JLabel("您的勝場爲" + bl.sc + "若是您的勝場大於5咱們推薦您進入高級場");
		frame.add(sc);
		// 實例化JLabel標籤對象,該對象顯示"帳號:"
		JLabel labName = new JLabel("請選擇加入幾人場");
		// 將labName標籤添加到窗體上
		frame.add(labName);

		// 實例化JTextField標籤對象
		JTextField textName = new JTextField();
		Dimension dim1 = new Dimension(300, 30);
		// textName.setSize(dim);//setSize這方法只對頂級容器有效,其餘組件使用無效。
		textName.setPreferredSize(dim1);// 設置除頂級容器組件其餘組件的大小
		// 將textName標籤添加到窗體上
		frame.add(textName);

		// 實例化JButton組件
		JButton button = new JButton();
		// 設置按鈕的顯示內容
		Dimension dim2 = new Dimension(100, 30);
		button.setText("選擇");
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String ss = textName.getText();
				// String password = textword.getText();
				if (ss.equals("1")) {
					try {
						// denglu ss = new denglu(login, password);

						Game ss1 = new Game();
						ss1.chuji();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else if (ss.equals("2")) {
					try {
						// denglu ss = new denglu(login, password);

						Game ss1 = new Game();
						ss1.gaoji();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				} else {

					JFrame f = new JFrame("");
					f.setSize(200, 150);
					f.setLocationRelativeTo(null);
					f.setLayout(null);
					JLabel l = new JLabel("輸入錯誤");
					// 文字顏色
					l.setForeground(Color.red);
					l.setBounds(50, 50, 280, 30);

					f.add(l);

					f.setVisible(true);

				}

			}
		});

		// 設置按鈕的大小
		button.setSize(dim2);
		frame.add(button);

		frame.setVisible(true);// 設置窗體爲可見
	}

}

  send.java

package wsh;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.Socket;

public class send extends Socket {

	private static final String SERVER_IP = "127.0.0.1";
	// private static final String SERVER_IP = "服務器IP";
	private static final int SERVER_PORT = 8899;

	private Socket client;

	public send() throws Exception {
		super(SERVER_IP, SERVER_PORT);
		this.client = this;
	}

	public void sendFile(String ff) throws Exception {

		DataInputStream in = new DataInputStream(client.getInputStream());
		DataOutputStream out = new DataOutputStream(client.getOutputStream());
		out.writeUTF(ff);
		String z = in.readUTF();
		System.out.println(z);

	}

	/**
	 * 入口
	 * 
	 * @param args
	 */
	public void good(String ff) {
		try {
			send client = new send(); // 啓動客戶端鏈接
			client.sendFile(ff); // 傳輸文件
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

  shuru.java

package wsh;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import javax.swing.JPasswordField;
import javax.swing.JButton;

import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

//1.定義Login類,
public class shuru {

	// 1.在類中定義主函數
	public void game(String w) {
		// 2.在主函數中,實例化Login類的對象,調用初始化界面的方法。
		shuru game = new shuru();
		game.gameUI(w);

	}

	// 1.在類中定義初始化界面的方法;

	public void gameUI(String w) {
		// 3.在initUI方法中,實例化JFrame類的對象。
		JFrame frame = new JFrame();
		// 4.設置窗體對象的屬性值:標題、大小、顯示位置、關閉操做、佈局、禁止調整大小、可見、...
		frame.setTitle(w);// 設置窗體的標題
		frame.setSize(400, 650);// 設置窗體的大小,單位是像素
		frame.setDefaultCloseOperation(3);// 設置窗體的關閉操做;3表示關閉窗體退出程序;二、一、0
		frame.setLocationRelativeTo(null);// 設置窗體相對於另外一個組件的居中位置,參數null表示窗體相對於屏幕的中央位置
		frame.setResizable(false);// 設置禁止調整窗體大小

		// 實例化FlowLayout流式佈局類的對象,指定對齊方式爲居中對齊,組件之間的間隔爲5個像素
		FlowLayout fl = new FlowLayout(FlowLayout.CENTER, 10, 10);
		// 實例化流式佈局類的對象
		frame.setLayout(fl);

		// 5.實例化元素組件對象,將元素組件對象添加到窗體上(組件添加要在窗體可見以前完成)。
		// 實例化ImageIcon圖標類的對象,該對象加載磁盤上的圖片文件到內存中,這裏的路徑要用兩個\
		ImageIcon icon = new ImageIcon("/root/圖片/b.gif");
		// 用標籤來接收圖片,實例化JLabel標籤對象,該對象顯示icon圖標
		JLabel labIcon = new JLabel(icon);
		// 設置標籤大小
		// labIcon.setSize(30,20);setSize方法只對窗體有效,若是想設置組件的大小隻能用
		Dimension dim = new Dimension(400, 300);
		labIcon.setPreferredSize(dim);
		// 將labIcon標籤添加到窗體上
		frame.add(labIcon);
		JLabel ts = new JLabel("請輸入你的數字:");
		frame.add(ts);

		// 實例化JTextField標籤對象
		JTextField textName = new JTextField();
		Dimension dim1 = new Dimension(300, 30);
		// textName.setSize(dim);//setSize這方法只對頂級容器有效,其餘組件使用無效。
		textName.setPreferredSize(dim1);// 設置除頂級容器組件其餘組件的大小
		// 將textName標籤添加到窗體上
		frame.add(textName);

		// 實例化JButton組件
		JButton button = new JButton();
		// 設置按鈕的顯示內容
		Dimension dim2 = new Dimension(100, 30);
		button.setText("肯定");

		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				String shuzi = textName.getText();

				send ww;
				try {
					ww = new send();
					ww.good(shuzi);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});

		// 設置按鈕的大小
		button.setSize(dim2);
		frame.add(button);

		frame.setVisible(true);// 設置窗體爲可見
	}

}

  zhuce.java

package wsh;

import java.awt.Color;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class zhuce{
	public zhuce(String login,String password) throws ClassNotFoundException, SQLException{
		//默認輸入的是字符串,因此建議測試的時候輸入字符串

			System.out.println("用戶名:");
			Scanner scanner = new Scanner(System.in);
			//獲取用戶名
			String userID = login;
			System.out.println("密碼:");
			Scanner scanner2 = new Scanner(System.in);
			//獲取密碼
			String psw1 = password;
			//先檢查用戶名是否存在,若不存在則繼續註冊
			String sql2 = "select * from test where 用戶名 ="+"'"+userID+"'";
			DBHelper DBquery = new DBHelper();
			ResultSet rsSet =  DBquery.DB(sql2);
			if (rsSet.next()) {
				System.out.println("該用戶名已被註冊,請更換您的用戶名再進行註冊");
				DBquery.downConn();
				}
			else{
				String sql = "insert into test(用戶名,密碼,勝場) values ("+userID+","+psw1+","+0+")";
				DBHelper DBupdate = new DBHelper();
				DBupdate.excuteSql(sql);
				System.out.println("恭喜您註冊成功");

		        JFrame f = new JFrame("");
		        f.setSize(200, 150);
		        f.setLocationRelativeTo(null);
		        f.setLayout(null);
		        JLabel l = new JLabel("恭喜您註冊成功");
		        //文字顏色
		        l.setForeground(Color.red);
		        l.setBounds(50, 50, 280, 30);

		        f.add(l);

		        f.setVisible(true);
				//關閉connection的鏈接哦,防止泄露
				DBupdate.downConn();
				scanner.close();
				scanner2.close();
			}
		}
	
}
	

	

  

 

 運行截圖:

 

    

5.總結

一根筷子折得斷,四根筷子誰頂得住啊!雖然這是個相對複雜的項目,可是不要一我的扛,團隊的力量纔是最大的,短短几天就讓咱們明白了團隊的重要,也讓咱們明白了自身能力的不足,在Java中咱們還須要繼續學習,完善自身的不足,在往後的工做學習生活中有用武之地。

相關文章
相關標籤/搜索