家乡の战队实训项目博客二
家乡の战队:黄金点项目博客二
1.团队风采
组长:唐宇 16012020
队员:王松鹤 16012016
刘计 16012024
庞啸天 16012011
2.码云地址
https://gitee.com/wcnma/home_troops/tree/master
3. 团队分工
唐宇:团队组长,灵魂核心,领导组员完成java任务,是组员的导向标
评分: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表示关闭窗体退出程序;2、1、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表示关闭窗体退出程序;2、1、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表示关闭窗体退出程序;2、1、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中我们还需要继续学习,完善自身的不足,在日后的工作学习生活中有用武之地。