精华内容
下载资源
问答
  • java期末大作业

    2013-06-28 15:05:46
    这是我的期末作业题目,有没有人能做出来呀,我也在做
  • GUI+SQL+JAVA期末大作业

    2019-02-20 07:07:56
    GUI+SQL+JAVA期末大作业gui界面设计
  • java期末大作业_五子棋小游戏单机版,GUI界面,使用jdk1.8,可以直接运行。
  • Java 期末大作业.zip

    2019-06-08 14:49:30
    Java SSM期末大作业.zip
  • 图形化界面和数据库,提供一套解决方案,有助于期末JAVA大作业
  • JAVA期末大作业 中国跳棋

    千次阅读 2019-02-01 09:50:00
     本学期选了java选修,期末大作业面向GitHub编程写了个中国跳棋。代码中还有写小bug,不过懒得调了...    1.游戏介绍  中国跳棋,简称跳棋,是一种可以让二至六人一起玩的棋,棋盘为六角星形状,棋子一般分为...

      PS:这学期期末成绩差不多出完了,接下来会陆续把这学期课程中的代码陆续扔到这里来以便后人****,同时自己也留个纪念。

      本学期选了java选修,期末大作业面向GitHub编程写了个中国跳棋。代码中还有写小bug,不过懒得调了...

       

     1.游戏介绍

      中国跳棋,简称跳棋,是一种可以让二至六人一起玩的棋,棋盘为六角星形状,棋子一般分为六种颜色,每种颜色有1015枚棋子(或弹珠),每一位玩家占一个角,执一种颜色的棋子,如果玩的人数较,也能每人执两种颜色的棋子玩。

    游戏规则如下:

    先将一种颜色的棋子按规则全部放在一个角上,各人循顺时针或逆时针方向轮流走子,每次只能动一枚棋子,全部棋子先到达对角那一边的为赢家。棋子有两种走法:

    1)一枚棋子移动到相邻六格其中一格。

    2)“搭桥”:自己的棋子(A)同一条直线上还有一枚棋子(B),不限属于哪一方,大家之间没有任何棋子阻隔。如果B的另一方也没有棋子阻隔,A就可以跳到B的相反方向,而且和两枚棋子原来的距离一样。跳跃可以一直进行下去,直到没有前进的路径为止。如果有循环情况,可在任意一处停下。

    2.项目介绍

        1)本跳棋project支持两人、四人、六人跳棋对战

        2)具有计时功能,可显示当前时间,同时也可判断当前玩家是否超时

        3)游戏可暂停

        4)可保存当前局面,从本地打开已有文件(文件I/O

     3.游戏界面

      4.工程布局(太懒了直接把所有class写在了一个文件里)

     

       5.代码

      

      1 import java.awt.Color;
      2 import java.awt.Font;
      3 import java.awt.Image;
      4 import java.awt.event.ActionEvent;
      5 import java.awt.event.ActionListener;
      6 import java.awt.event.KeyEvent;
      7 import java.awt.event.MouseAdapter;
      8 import java.awt.event.MouseEvent;
      9 import java.io.File;
     10 import java.io.FileNotFoundException;
     11 import java.io.IOException;
     12 import java.io.PrintWriter;
     13 import java.text.SimpleDateFormat;
     14 import java.util.Date;
     15 import java.util.Scanner;
     16 
     17 import javax.imageio.ImageIO;
     18 import javax.management.MBeanOperationInfo;
     19 import javax.swing.ImageIcon;
     20 import javax.swing.JFileChooser;
     21 import javax.swing.JFrame;
     22 import javax.swing.JLabel;
     23 import javax.swing.JLayeredPane;
     24 import javax.swing.JMenu;
     25 import javax.swing.JMenuBar;
     26 import javax.swing.JMenuItem;
     27 import javax.swing.JOptionPane;
     28 
     29 
     30 
     31 public class ChineseChecker {
     32     
     33     public static MyFrame myFrame;
     34     public static int numberOfPlayers;    //游戏玩家的数量
     35     public static boolean numbersDone = false;//游戏玩家数量是否设定完毕
     36     public static boolean gameStatus = false;//游戏状态(是否暂停)
     37     public static char[] series = new char[6];//六种棋子
     38     public static int currentIndex = 0;//当前下标
     39     public static String pieceToMove;//将要移动的棋子
     40     public static int []piecePos;//记录棋子位置
     41     public static boolean isPieceToMoveAcquired = false;//是否已经选中将要移动的棋子
     42     public static boolean isTheStartingOfTheGame = false;//游戏是否开始
     43     public static boolean isPositionToGoToAcquired = false;//将要去的位置是否设定完毕
     44     public static int[] posToGoTo;
     45     public static boolean isANewGame = false;
     46     
     47     public static void main(String[] args) {
     48         // TODO Auto-generated method stub
     49         myFrame = new MyFrame();//实例化窗口
     50         myFrame.setSize(710, 870);//设置窗口大小
     51         myFrame.setVisible(true);//窗口可见
     52         Pieces pieces = new Pieces();//实例化棋子类
     53         
     54         /**等待设定游戏玩家数量**/
     55         while(!numbersDone){
     56             try {
     57                 Thread.sleep(500);
     58             } catch (InterruptedException e) {
     59                 // TODO Auto-generated catch block
     60                 e.printStackTrace();
     61             }
     62         }
     63         numbersDone = false;
     64         
     65         /*
     66          * 游戏分为双人对战,四人对战和六人对战
     67          */
     68         switch (numberOfPlayers) {
     69         case 2:
     70             series[0] = 'D';series[1] = 'A';
     71             break;
     72         case 4:
     73             series[0] = 'F';series[1] = 'B';series[2] = 'C';series[3] = 'E';
     74             break;
     75         case 6:
     76             series[0] = 'F';series[1] = 'A';series[2] = 'B';series[3] = 'C';series[4] = 'D';series[5] = 'E';
     77             break;
     78         default:
     79             break;
     80         } 
     81         //pieces.init();
     82         myFrame.init(series);
     83         initPlayers(series);
     84         
     85         pieces.init();
     86         
     87         /**一旦玩家数量设定完成,游戏开始**/
     88         isTheStartingOfTheGame = true;
     89         
     90         while (true) {
     91             Trace.isSkipped = false;
     92 
     93             Trace.nextStep();
     94             
     95             //展示当前玩家
     96             myFrame.showCurrent();
     97             myFrame.showCurrentPlayer();
     98             MyFrame.timer = new Timer();
     99             
    100 
    101             while (!isPositionToGoToAcquired) {
    102                 
    103                 if (MyFrame.timer.listen() <= 0) {//如果玩家超时
    104                     if (isPieceToMoveAcquired) //如果将要移动的的棋子设定过
    105                         PieceActionListener.clearPossiblePos();//清除之前的可能移动到的合法位置标记
    106                     pieceToMove = series[currentIndex] + "0";
    107                     piecePos = Pieces.getPiecePos("A0  ");
    108                     posToGoTo = piecePos ;
    109                     Services.msg("Player " + series[currentIndex] + " time exceeded!");//提示玩家超时
    110                     break;
    111                 }
    112                 if (Trace.isSkipped)//如果已经跳过了,就退出
    113                     break;
    114                 try {
    115                     Thread.sleep(0500);
    116                 } catch (InterruptedException e) {
    117                     // TODO Auto-generated catch block
    118                     e.printStackTrace();
    119                 }
    120             }
    121             if (Trace.isSkipped) continue;//如果已经跳过了,就到下一次循环
    122             isPositionToGoToAcquired = false;
    123             if (isANewGame) {
    124                 isANewGame = false;
    125                 continue;
    126             }
    127             /*记录轨迹 */
    128             Pieces.moveAward(pieceToMove, piecePos, posToGoTo);
    129             Trace.recordStep(pieceToMove, piecePos, posToGoTo);
    130 
    131             /*判断是否出现胜利者*/
    132             for (int order = 0; order < numberOfPlayers; order++) {
    133                 if (isWinner(series[order])) {
    134                     Services.msg("Player " + series[order] + " wins!");
    135                     System.exit(0);
    136                 }
    137             }
    138             Trace.step = Trace.step + 1;
    139         }
    140     }
    141 
    142     public static void setNumber(int number){//设定游戏玩家数量
    143         ChineseChecker.numbersDone = true;
    144         ChineseChecker.gameStatus = true;
    145         ChineseChecker.numberOfPlayers = number;
    146         ChineseChecker.myFrame.mNewGame.setEnabled(false);
    147     }
    148     
    149     public static void initPlayers(char[] players) {//初始化players
    150         for (char player : players) {
    151             if (player == 0)
    152                 return;
    153             int[][] initPositions = myFrame.zoneOfPlayer[player - 'A'];
    154             for (int index = 0; index < 10; index++) {
    155                 Pieces.cell[initPositions[index][0]][initPositions[index][1]] = "" + player + index + "  ";
    156             }
    157         }
    158     }
    159     
    160     public static boolean isWinner(char player) {//判断player是否已经获胜
    161         char opponentPlayer = player < 'D' ? (char) ((int) player + 3) : (char) ((int) player - 3);
    162         
    163         int[][] positionsBelongedToThePlayer = MyFrame.zoneOfPlayer[opponentPlayer - 'A'];
    164 
    165         for (int index = 0; index < 10; index++) {
    166             int[] position = positionsBelongedToThePlayer[index];
    167             if (Pieces.cell[position[0]][position[1]].charAt(0) != player)
    168                 return false;
    169         }
    170         return true;
    171     }
    172 
    173 }
    174 
    175 class MyFrame extends JFrame{
    176     
    177     public Image image;
    178     
    179     public ImageIcon imgBoard;
    180     public static JLayeredPane panel;
    181     public static JLabel lblBoard,lblPlayer,lblTime,lblCurrent;
    182     public JMenuBar menuBar;
    183     public JMenu mControl = new JMenu("control"),
    184                 mNewGame = new JMenu("New Game");
    185     public JMenuItem mTwoPlayers = new JMenuItem("Two players"),
    186                 mFourPlayers = new JMenuItem("Four players"),
    187                 mSixPlayers = new JMenuItem("Six players"),
    188                 mExit = new JMenuItem("Exit", KeyEvent.VK_X),
    189                 mPauseGame = new JMenuItem("Pause Game", KeyEvent.VK_P),
    190                 mOpen = new JMenuItem("Open",KeyEvent.VK_O), 
    191                         mSave = new JMenuItem("Save",KeyEvent.VK_S);
    192     public JLabel[][] pieces = new JLabel[6][10];
    193     public static final int[][][] zoneOfPlayer = {
    194             {{5, 1}, {5, 2}, {6, 2}, {5, 3}, {6, 3}, {7, 3}, {5, 4}, {6, 4}, {7, 4}, {8, 4}},
    195             {{1, 5}, {2, 6}, {2, 5}, {3, 7}, {3, 6}, {3, 5}, {4, 8}, {4, 7}, {4, 6}, {4, 5}},
    196             {{5, 13}, {6, 13}, {5, 12}, {7, 13}, {6, 12}, {5, 11}, {8, 13}, {7, 12}, {6, 11}, {5, 10}},
    197             {{13, 17}, {13, 16}, {12, 16}, {13, 15}, {12, 15}, {11, 15}, {13, 14}, {12, 14}, {11, 14}, {10, 14}},
    198             {{17, 13}, {16, 12}, {16, 13}, {15, 11}, {15, 12}, {15, 13}, {14, 10}, {14, 11}, {14, 12}, {14, 13}},
    199             {{13, 5}, {12, 5}, {13, 6}, {11, 5}, {12, 6}, {13, 7}, {10, 5}, {11, 6}, {12, 7}, {13, 8}}};
    200     public static Timer timer;
    201     public MyFrame(){
    202         
    203         imgBoard = new ImageIcon("Board.jpg");
    204         lblTime = new JLabel();
    205         lblTime.setForeground(Color.BLUE);
    206         lblTime.setFont(new Font("黑体", Font.BOLD, 20));
    207         lblTime.setBounds(50, 50, 280, 30);
    208         
    209         lblCurrent = new JLabel("当前玩家");
    210         lblCurrent.setFont(new Font("当前玩家", 20, 20));
    211         lblCurrent.setBounds(40, 80, 200, 30);
    212         
    213         lblBoard = new JLabel(imgBoard);
    214         lblBoard.setBounds(0, 20, 685, 800);
    215         
    216         lblPlayer = new JLabel(new ImageIcon());
    217         lblPlayer.setBounds(50, 100, 50, 50);
    218         
    219         panel = new JLayeredPane();
    220         panel.add(lblBoard,JLayeredPane.DEFAULT_LAYER);
    221         panel.add(lblPlayer, JLayeredPane.MODAL_LAYER);
    222         panel.add(lblTime, JLayeredPane.MODAL_LAYER);
    223         
    224         
    225         setLayeredPane(panel);
    226         
    227         mControl.setMnemonic(KeyEvent.VK_C);
    228         mControl.add(mOpen);
    229         mControl.add(mSave);
    230         mControl.add(mNewGame);
    231         mControl.add(mPauseGame);
    232         
    233         mNewGame.add(mTwoPlayers);
    234         mNewGame.add(mFourPlayers);
    235         mNewGame.add(mSixPlayers);
    236         
    237         menuBar = new JMenuBar();
    238         menuBar.add(mControl);
    239         setJMenuBar(menuBar);
    240         
    241         /**设置两个玩家、四个玩家或六个玩家**/
    242         mTwoPlayers.addActionListener(new ActionListener() {
    243             @Override
    244             public void actionPerformed(ActionEvent e) {
    245                 // TODO Auto-generated method stub
    246                 ChineseChecker.setNumber(2);
    247             }
    248         });
    249         mFourPlayers.addActionListener(new ActionListener() {
    250             @Override
    251             public void actionPerformed(ActionEvent e) {
    252                 // TODO Auto-generated method stub
    253                 ChineseChecker.setNumber(4);
    254             }
    255         });
    256         mSixPlayers.addActionListener(new ActionListener() {
    257             @Override
    258             public void actionPerformed(ActionEvent e) {
    259                 // TODO Auto-generated method stub
    260                 ChineseChecker.setNumber(6);
    261             }
    262         });
    263         /**暂停游戏**/
    264         mPauseGame.addActionListener(new ActionListener() {
    265             @Override
    266             public void actionPerformed(ActionEvent e) {
    267                 // TODO Auto-generated method stub
    268                 ChineseChecker.gameStatus = false;
    269             }
    270         });
    271         
    272         mOpen.addActionListener(new ActionListener() {//打开已保存过的游戏
    273             @Override
    274             public void actionPerformed(ActionEvent arg0) {
    275                 JFileChooser chooser = new JFileChooser();
    276                 chooser.showOpenDialog(null);
    277 
    278                 try {
    279                     File file = chooser.getSelectedFile();
    280                     if (file == null) {
    281                         throw new Exception("FileNotExisted");
    282                     }
    283                     Scanner scanner = new Scanner(file);
    284                     String[] temp = scanner.nextLine().split(",");
    285                     int num = Integer.parseInt(temp[0]);
    286                     char cur = temp[1].charAt(0);
    287                     int curIndex = Integer.parseInt(temp[2]);
    288                     int step = Integer.parseInt(temp[3]);
    289 
    290                     ChineseChecker.numbersDone = true;
    291                     if (!(ChineseChecker.numberOfPlayers == num) && !ChineseChecker.isTheStartingOfTheGame)
    292                         throw new Exception("PlayersNumberUnmatched");
    293                     ChineseChecker.numberOfPlayers = num;
    294                     System.out.println(num);
    295                     ChineseChecker.currentIndex = curIndex;
    296                     ChineseChecker.series[curIndex] = cur;
    297                     Trace.step = step;
    298                     //Diagram.getDiagram().init();
    299                     
    300                     while (!ChineseChecker.isTheStartingOfTheGame) {
    301                     }
    302                     while (scanner.hasNext()) {
    303                         in(scanner.nextLine());
    304                     }
    305                     ChineseChecker.myFrame.mNewGame.setEnabled(false);
    306                     ChineseChecker.isANewGame = true;
    307                     ChineseChecker.isPositionToGoToAcquired = true;
    308                 } catch (FileNotFoundException e) {
    309                     // e.printStackTrace();
    310                 } catch (Exception e) {
    311                     Services.msg("Import failed: " + e.getMessage());
    312                 }
    313             }
    314 
    315             public void in(String line) {
    316                 String[] temp = line.split(",");
    317                 String piece = temp[0] + "  ";
    318                 int i = Integer.parseInt(temp[1]), j = Integer.parseInt(temp[2]);
    319                 int player = piece.charAt(0) - 65;
    320                 int index = piece.charAt(1) - 48;
    321                 Pieces.pieces[player][index][0] = i;
    322                 Pieces.pieces[player][index][1] = j;
    323                 Pieces.cell[i][j] = piece;
    324                 int[] pos = {i, j};
    325                 ChineseChecker.myFrame.move(piece,pos);
    326             }
    327         });
    328         
    329         mSave.addActionListener(new ActionListener() {//保存当前游戏
    330             
    331             @Override
    332             public void actionPerformed(ActionEvent arg0) {
    333                 JFileChooser chooser = new JFileChooser();
    334                 chooser.showOpenDialog(null);
    335                 File file = chooser.getSelectedFile();
    336 
    337                 PrintWriter writer;
    338                 try {
    339                     writer = new PrintWriter(file);
    340                     writer.write(new Output().s);
    341                     writer.close();
    342                 } catch (FileNotFoundException e) {
    343                     // e.printStackTrace();
    344                 } catch (Exception e) {
    345                     Services.msg("Export failed!");
    346                 }
    347             }
    348 
    349             class Output {
    350                 String s = "";
    351 
    352                 Output() {
    353                     out();
    354                 }
    355 
    356                 void out() {
    357                     int num = ChineseChecker.numberOfPlayers;
    358                     System.out.println(num);
    359                     int[] position;
    360                     s += num;
    361                     char cur = ChineseChecker.series[ChineseChecker.currentIndex];
    362                     s += "," + cur;
    363                     int curIndex = ChineseChecker.currentIndex;
    364                     s += "," + curIndex;
    365                     int step = Trace.step;
    366                     s += "," + step;
    367                     char[][] player = {null, null, {'A', 'D'}, {'A', 'C', 'E'},
    368                             {'B', 'C', 'E', 'F'}, null,
    369                             {'A', 'B', 'C', 'D', 'E', 'F'}};
    370                     for (int i = 0; i < 10; i++) {
    371                         for (char j : player[num]) {
    372                             position = Pieces.getPiecePos(j - 65, i);
    373                             s += "\n" + j + i + "," + position[0] + "," + position[1];
    374                         }
    375                     }
    376                 }
    377             }
    378         });
    379         
    380         TimerThread timerThread = new TimerThread("Timer");
    381         timerThread.start();
    382         
    383     }
    384     
    385     public void showCurrent() {
    386         panel.add(lblCurrent, JLayeredPane.MODAL_LAYER);
    387     }
    388     
    389     public void move(String pieceToMove, int[] targetPosition) {
    390         char playerOnTheMove = pieceToMove.charAt(0);
    391         int index = pieceToMove.charAt(1) - '0';
    392         int x = Map.map[targetPosition[0]][targetPosition[1]][0];
    393         int y = Map.map[targetPosition[0]][targetPosition[1]][1];
    394 
    395         pieces[playerOnTheMove - 'A'][index].setBounds(x, y, 39, 39);
    396     }
    397     
    398     public void init(char[] playerSeries) {
    399         for (char player : playerSeries) {
    400             if (player == 0)
    401                 return;
    402             initPlayer(player);
    403         }
    404     }
    405 
    406     private void initPlayer(char player) {
    407         for (int pieceIndex = 0; pieceIndex < 10; pieceIndex++) {
    408             initPiece(player, pieceIndex);
    409         }
    410     }
    411 
    412     private void initPiece(char player, int pieceIndex) {
    413         //Image image = 
    414         Map myMap = new Map();
    415         int playerIndex = player - 'A';
    416         int[] pieceSeries = zoneOfPlayer[player - 'A'][pieceIndex];
    417         int x = myMap.map[pieceSeries[0]][pieceSeries[1]][0];
    418         int y = myMap.map[pieceSeries[0]][pieceSeries[1]][1];
    419 
    420         pieces[playerIndex][pieceIndex] = new JLabel(new ImageIcon("Piece_" + player + ".PNG"));
    421         pieces[playerIndex][pieceIndex]
    422                 .addMouseListener(new PieceActionListener("" + player + pieceIndex));
    423 
    424         pieces[playerIndex][pieceIndex].setBounds(x, y, 39, 39);
    425         panel.add(pieces[playerIndex][pieceIndex], JLayeredPane.MODAL_LAYER);
    426     }
    427 
    428     public static void showCurrentPlayer() {
    429         
    430         lblPlayer.setIcon(new ImageIcon("Piece_" + ChineseChecker.series[ChineseChecker.currentIndex] + ".PNG"));
    431         panel.repaint();
    432     }
    433     
    434     
    435     class TimerThread extends Thread {
    436         TimerThread(String name) {
    437             super(name);
    438         }
    439 
    440         @Override
    441         public void run() {
    442             while (true) {
    443                 lblTime.setText(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    444                 panel.repaint();
    445                 try {
    446                     Thread.sleep(0500);
    447                 } catch (InterruptedException e) {
    448                     e.printStackTrace();
    449                 }
    450             }
    451         }
    452     }
    453     
    454 }
    455 
    456 class Map {//map记录棋盘上每个点在myFrame中的坐标
    457     //map[i][j][0]表示点(i,j)在棋盘中的横坐标,map[i][j][1]表示点(i,j)在棋盘中的纵坐标
    458     public static int [][][] map = new int[18][18][2];
    459     public Map(){//计算各点坐标,并将其存入map中
    460         for (int i = 5; i <= 5; i++)    
    461             setSinglePosition(i, 1, 325 + 51 * (i - 5), 780 + 0 * (i - 5));
    462         for (int i = 5; i <= 6; i++)
    463             setSinglePosition(i, 2, 325 - 28 + 54 * (i - 5), 780 - 47);
    464         for (int i = 5; i <= 7; i++)
    465             setSinglePosition(i, 3, 325 - 28 * 2 + 54 * (i - 5), 780 - 47*2);
    466         for (int i = 5; i <= 8; i++)
    467             setSinglePosition(i, 4, 325 - 28 * 3 + 54 * (i - 5), 780 - 47*3);
    468         for (int i = 1; i <= 13; i++)
    469             setSinglePosition(i, 5, 0 + 28 * 0 + 54 * (i - 1), 780 - 47*4);
    470         for (int i = 2; i <= 13; i++)
    471             setSinglePosition(i, 6, 0 + 28 * 1 + 54 * (i - 2), 780 - 47*5);
    472         for (int i = 3; i <= 13; i++)
    473             setSinglePosition(i, 7, 0 + 28 * 2 + 54 * (i - 3), 780 - 47*6);
    474         for (int i = 4; i <= 13; i++)
    475             setSinglePosition(i, 8, 0 + 28 * 3 + 54 * (i - 4), 780 - 47*7);
    476         for (int i = 5; i <= 13; i++)
    477             setSinglePosition(i, 9, 0 + 28 * 4 + 54 * (i - 5), 780 - 47*8);
    478         for (int i = 5; i <= 14; i++)
    479             setSinglePosition(i, 10, 0 + 28 * 3 + 54 * (i - 5), 780 - 47*9);
    480         for (int i = 5; i <= 15; i++)
    481             setSinglePosition(i, 11, 0 + 28 * 2 + 54 * (i - 5), 780 - 47*10);
    482         for (int i = 5; i <= 16; i++)
    483             setSinglePosition(i, 12, 0 + 28 * 1 + 54 * (i - 5), 780 - 47*11);
    484         for (int i = 5; i <= 17; i++)
    485             setSinglePosition(i, 13, 0 + 28 * 0 + 54 * (i - 5), 780 - 47*12);
    486         for (int i = 10; i <= 13; i++)
    487             setSinglePosition(i, 14, 325 - 28 * 3 + 54 * (i - 10), 780 - 47*13);
    488         for (int i = 11; i <= 13; i++)
    489             setSinglePosition(i, 15, 325 - 28 * 2 + 54 * (i - 11),780 - 47*14);
    490         for (int i = 12; i <= 13; i++)
    491             setSinglePosition(i, 16, 325 - 28 * 1 + 54 * (i - 12), 780 - 47*15);
    492         for (int i = 13; i <= 13; i++)
    493             setSinglePosition(i, 17, 325 - 28 * 0 + 54 * (i - 13), 780 - 47*16);
    494     }
    495     public void setSinglePosition(int i,int j,double x,double y){
    496         map[i][j][0] = (int)x;
    497         map[i][j][1] = (int)y;
    498     }
    499 }
    500 
    501 class Pieces {//棋子类
    502     public static int [][][] pieces = new int [6][10][2];
    503     public static String [][] cell = new String[20][20];
    504     public final static String EMPTY_MARK = "**  ";
    505     public final static String OUTSIDE_MARK = "    ";
    506     public final static String SPACE_MARK = "  ";
    507     
    508     public Pieces(){
    509         
    510         for (int i = 1; i <= 17; i++) 
    511             for (int j = 1; j <= 17; j++) 
    512                 cell[i][j]= OUTSIDE_MARK;//初始化认为所有cell都在棋盘外
    513         
    514         /**将所有在棋盘内的cell标记为EMPTY_MARK**/
    515         for (int j = 1; j <= 4; j++) 
    516             for (int i = 5; i <= j + 4; i++)
    517                 cell[i][j]= EMPTY_MARK;
    518         for (int j = 5; j <= 8; j++) 
    519             for (int i = j - 4; i <= 13; i++)
    520                 cell[i][j]= EMPTY_MARK;
    521         for (int j = 9; j <= 13; j++) 
    522             for (int i = 5; i <= j + 4; i++)
    523                 cell[i][j]= EMPTY_MARK;
    524         for (int j = 14; j <= 17; j++) 
    525             for (int i = j - 4; i <= 13; i++)
    526                 cell[i][j]= EMPTY_MARK;
    527     }
    528     public void init(){//初始化
    529         for (int i = 1;i <= 17;++i){
    530             for (int j = 1;j <= 17;++j){
    531                 if (cell[i][j].charAt(0) >= 65 && cell[i][j].charAt(0) <= 70){
    532                     setPosition(cell[i][j],i,j);
    533                 }
    534             }
    535         }
    536     }
    537     
    538     public void setPosition(String s,int x,int y){
    539         int player = s.charAt(0) - 65;
    540         int index = s.charAt(1) -48;
    541         pieces[player][index][0] = x;
    542         pieces[player][index][1] = y;
    543     }
    544     
    545     public static void setPiecePos(String piece, int[] value) {
    546         int player = piece.charAt(0) - 65;
    547         int index = piece.charAt(1) - 48;
    548         pieces[player][index] = value;
    549     }
    550     
    551     public static int[] getPiecePos(String piece) {
    552         int player = piece.charAt(0) - 65;
    553         int index = piece.charAt(1) - 48;
    554         return new int[]{pieces[player][index][0], pieces[player][index][1]};
    555     }
    556     
    557     public static int[] getPiecePos(int player, int index) {
    558         return new int[]{pieces[player][index][0], pieces[player][index][1]};
    559     }
    560     
    561     public static void moveAward(String piece, int[] prevPos, int[] postPos) {//前进
    562         setPiecePos(piece, postPos);
    563         cell[prevPos[0]][prevPos[1]] = EMPTY_MARK;
    564         cell[postPos[0]][postPos[1]] = piece + SPACE_MARK;
    565     }
    566     
    567     public static class Next {
    568 
    569 
    570         public static int[][] getPossiblePosition(String pieceToMove) {
    571             /**
    572              * get all possible positions via adjacent hop/distant/consecutive hops.
    573              */
    574             int[] piecePosition = Pieces.getPiecePos(pieceToMove);
    575             int[][] possiblePosition = distantHopping(piecePosition);
    576 
    577             
    578             cell[piecePosition[0]][piecePosition[1]] = "    ";
    579             // apply Breadth-First-Search to accomplish consecutive hops
    580             for (int k = 0; possiblePosition[k][0] != 0; k++) {
    581                 int[][] recursion = distantHopping(possiblePosition[k]);
    582                 possiblePosition = Services.concatTwoArrays(possiblePosition, recursion);
    583             }
    584             possiblePosition = Services.concatTwoArrays(possiblePosition, adjacentHopping(piecePosition));
    585             
    586             cell[piecePosition[0]][piecePosition[1]] = pieceToMove;
    587             return possiblePosition;
    588         }
    589 
    590         public static int getPossiblePositionCount(String pieceToMove) {
    591             int count = 0;
    592             int[][] possiblePosition = getPossiblePosition(pieceToMove);
    593 
    594             for (int k = 0; possiblePosition[k][0] != 0; k++) {
    595                 count++;
    596             }
    597             return count;
    598         }
    599 
    600         private static int[][] adjacentHopping(int[] piecePosition) {
    601 
    602             int[][] possiblePositions = new int[100][2];
    603             int pointer = 0;
    604             int[] displacementAdjacent = {-1, 0, 1};
    605             // (-1,-1)(-1,0)(0,-1)(0,1)(1,0)(1,1)
    606 
    607             for (int i : displacementAdjacent) {
    608                 for (int j : displacementAdjacent) {
    609                     // check whether the adjacent position is empty
    610                     String current = cell[piecePosition[0] + i][piecePosition[1] + j];
    611                     if (current == EMPTY_MARK && i != -j) {
    612                         possiblePositions[pointer][0] = piecePosition[0] + i;
    613                         possiblePositions[pointer][1] = piecePosition[1] + j;
    614                         pointer++;
    615                     }
    616                 }
    617             }
    618             return possiblePositions;
    619         }
    620 
    621         private static int[][] distantHopping(int[] piecePosition) {
    622             int[][] possiblePos = new int[100][2];
    623             int[] displacement = {-1, 0, 1};
    624             // stores possible direction
    625 
    626             for (int x : displacement) {
    627                 for (int y : displacement) {
    628                     possiblePos = Services.concatTwoArrays(possiblePos,
    629                             distantHoppingForOneDirection(x, y, piecePosition, true));
    630                 }
    631             }
    632 
    633             return possiblePos;
    634         }
    635 
    636         public static boolean isPosInsideDiagram(String position) {
    637             return !(position == OUTSIDE_MARK || position == null);
    638         }
    639         
    640         private static int[][] distantHoppingForOneDirection(int x, int y,
    641                                                              int[] piecePos, boolean isDistantHoppingDisabled) {
    642             /*
    643              * x: indicates up or down moveAward in x direction. y: indicates up or down
    644              * moveAward in x direction
    645              */
    646             int[][] possiblePos = new int[100][2];
    647             int[] displacement = (isDistantHoppingDisabled)?new int[]{1}:new int[]{1, 2, 3, 4, 5, 6, 7, 8};
    648             // stores possible displacements of coordinates in each direction
    649             int pointer = 0;
    650             boolean isDeadDirection;
    651 
    652             for (int i : displacement) {
    653                 // avoid illegal direction
    654                 if (x * y == -1)
    655                     continue;
    656                 // avoid array index out of bound
    657                 boolean isiInside = (x == 0) || ((x == -1) ? piecePos[0] > 1 + 1 : piecePos[0] < 17 - 1);
    658                 boolean isjInside = (y == 0) || ((y == -1) ? piecePos[1] > 1 + 1 : piecePos[1] < 17 - 1);
    659                 boolean isInside = isiInside
    660                         && isjInside
    661                         && isPosInsideDiagram(cell[piecePos[0] + i * 2 * x - 1 * x][piecePos[1] + i* 2 * y - 1 * y])
    662                         && isPosInsideDiagram(cell[piecePos[0] + i * 2 * x][piecePos[1] + i * 2 * y]);
    663                 if (!isInside)
    664                     break;
    665                 boolean isAvailable = (isDeadDirection = !(cell[piecePos[0] + i * x][piecePos[1] + i * y] == EMPTY_MARK)
    666                         && cell[piecePos[0] + i * 2 * x][ piecePos[1] + i * 2 * y] == EMPTY_MARK);
    667 
    668                 label1:
    669                 if (isAvailable) {
    670                     // position between object position and hopped piece
    671                     // ought to be empty
    672                     for (int ii = i + 1; ii < 2 * i; ii++) {
    673                         if (cell[piecePos[0] + ii * x][piecePos[1] + ii * y] != EMPTY_MARK) {
    674                             isDeadDirection = true;
    675                             break label1;
    676                         }
    677                     }
    678                     possiblePos[pointer][0] = piecePos[0] + i * 2 * x;
    679                     possiblePos[pointer][1] = piecePos[1] + i * 2 * y;
    680                     pointer++;
    681                 }
    682 
    683                 if (isDeadDirection)
    684                     break;
    685             }
    686             return possiblePos;
    687         }
    688 
    689     }
    690 }
    691 
    692 class PieceActionListener extends MouseAdapter{//棋子事件侦听
    693     private final static ImageIcon imgPossiblePosition = new ImageIcon("Piece_Transparant.PNG");
    694     private static boolean isPieceSelected;
    695     private static JLabel[] lblPossible = new JLabel[50];
    696     private int[][] possiblePos = new int[50][2];
    697     private String piece;
    698     
    699     public PieceActionListener(String piece) {
    700         this.piece = piece + "  ";
    701     }
    702 
    703     public static void clearPossiblePos() {//清空可能到达的位置
    704         for (int index = 0; lblPossible[index] != null; index++) {
    705             ChineseChecker.myFrame.panel.remove(lblPossible[index]);
    706         }
    707         ChineseChecker.myFrame.repaint();
    708         isPieceSelected = false;
    709     }
    710 
    711     @Override
    712     public void mouseClicked(MouseEvent arg0) {//鼠标点击事件处理
    713         if (ChineseChecker.series[ChineseChecker.currentIndex] != piece.charAt(0))
    714             return;
    715         if (!ChineseChecker.gameStatus)
    716             return;
    717 
    718         clearPossiblePos();
    719         showPossiblePos();
    720         isPieceSelected = true;
    721         ChineseChecker.pieceToMove = piece.substring(0, 2);
    722         ChineseChecker.piecePos = new int[]{Pieces.pieces[piece.charAt(0) - 65][piece.charAt(1) - 48][0], 
    723                                              Pieces.pieces[piece.charAt(0) - 65][piece.charAt(1) - 48][1]};
    724         ChineseChecker.isPieceToMoveAcquired = true;
    725     }
    726 
    727     @Override
    728     public void mouseEntered(MouseEvent arg0) {//鼠标移动事件处理
    729         if (!ChineseChecker.gameStatus)
    730             return;
    731         if (ChineseChecker.series[ChineseChecker.currentIndex] != piece.charAt(0))
    732             return;
    733         if (isPieceSelected)
    734             return;
    735         showPossiblePos();
    736     }
    737 
    738     @Override
    739     public void mouseExited(MouseEvent arg0) {//鼠标挪开事件处理
    740         if (isPieceSelected)
    741             return;
    742         for (int k = 0; lblPossible[k] != null; k++) {
    743             ChineseChecker.myFrame.panel.remove(lblPossible[k]);
    744             ChineseChecker.myFrame.repaint();
    745         }
    746     }
    747 
    748     public void showPossiblePos() {//展示可能到达的位置
    749         possiblePos = Pieces.Next.getPossiblePosition(piece);
    750         for (int k = 0; k < possiblePos.length && possiblePos[k][0] != 0; k++) {
    751             lblPossible[k] = new JLabel(imgPossiblePosition);
    752             lblPossible[k].addMouseListener(new PossiblePositionListener(piece,
    753                     possiblePos[k]));
    754             ChineseChecker.myFrame.panel.add(lblPossible[k], JLayeredPane.MODAL_LAYER);
    755             int[] positionInCanvas = Map.map[possiblePos[k][0]][possiblePos[k][1]];
    756             lblPossible[k].setBounds(positionInCanvas[0], positionInCanvas[1], 39, 39);
    757         }
    758     }
    759 }
    760 
    761 class PossiblePositionListener extends MouseAdapter {
    762 
    763     public static String piece;
    764     public int[] position = new int[2];
    765     public final static String EMPTY_MARK = "**  ";
    766     public final static String OUTSIDE_MARK = "    ";
    767     public final static String SPACE_MARK = "  ";
    768     PossiblePositionListener(String piece, int[] position) {
    769         this.piece = piece;
    770         this.position = position;
    771     }
    772 
    773     @Override
    774     public void mouseClicked(MouseEvent arg0) {
    775         if (!ChineseChecker.gameStatus)
    776             return;
    777 
    778         Pieces.cell[Pieces.getPiecePos(piece)[0]][Pieces.getPiecePos(piece)[1]] = EMPTY_MARK;
    779         Pieces.cell[position[0]][position[1]] = piece + SPACE_MARK;
    780         
    781         ChineseChecker.myFrame.move(piece, position);
    782         PieceActionListener.clearPossiblePos();
    783         ChineseChecker.posToGoTo = position;
    784         ChineseChecker.isPositionToGoToAcquired = true;
    785     }
    786 
    787 }
    788 
    789 class Services {
    790 
    791     public static void msg(String content) {
    792         JOptionPane.showMessageDialog(null, content, "Chinese Checkers",
    793                 JOptionPane.INFORMATION_MESSAGE);
    794     }
    795 
    796     public static String in(String content) {
    797         return JOptionPane.showInputDialog(null, content, "Chinese Checkers",
    798                 JOptionPane.INFORMATION_MESSAGE);
    799     }
    800 
    801 
    802     public static int[][] concatTwoArrays(int[][] targetArray, int[][] temporaryArray) {
    803         /** combine two 2D arrays into one. */
    804         int pointer = 0;// point to the operating index of a1
    805         while (targetArray[pointer][0] != 0) {
    806             pointer++;
    807         }
    808         for (int j = 0; ; j++) {
    809             boolean isRepeated = false;
    810             if (temporaryArray[j][0] == 0)
    811                 break;
    812             for (int i = 0; i < pointer; i++)
    813                 if (temporaryArray[j][0] == targetArray[i][0] && temporaryArray[j][1] == targetArray[i][1]) {
    814                     isRepeated = true;
    815                     break;
    816                 }
    817             if (!isRepeated)
    818                 targetArray[pointer++] = temporaryArray[j];
    819         }
    820         return targetArray;
    821     }
    822 }
    823 
    824 class Timer {
    825 
    826     private long startingTime;
    827     private long interruptedTime;
    828     private boolean paused;
    829 
    830     public Timer() {
    831         this.startingTime = System.currentTimeMillis();
    832     }
    833 
    834     public long listen() {
    835         if (paused)
    836             return (30000 - interruptedTime + startingTime) / 1000;
    837         long cur = System.currentTimeMillis();
    838         return (30000 - cur + startingTime) / 1000;
    839     }
    840 
    841     public void pause() {
    842         this.interruptedTime = System.currentTimeMillis();
    843         this.paused = true;
    844     }
    845 }
    846 
    847 class Trace {
    848 
    849     public static final int MAX_STACK_SIZE = 1000;
    850     public static int step = 1;
    851     public static boolean isSkipped;
    852     public static String[] pieceStack = new String[MAX_STACK_SIZE];
    853     public static int[][] prevPosStack = new int[MAX_STACK_SIZE][2];
    854     public static int[][] postPosStack = new int[MAX_STACK_SIZE][2];
    855 
    856     public boolean isSkipped() {
    857         return isSkipped;
    858     }
    859     
    860     public static void recordStep(String piece, int[] prev, int[] post) {
    861         pieceStack[step] = piece;
    862         prevPosStack[step] = prev;
    863         postPosStack[step] = post;
    864     }
    865 
    866     public static void nextStep() {
    867         ChineseChecker.currentIndex= Trace.step % ChineseChecker.numberOfPlayers;
    868     }
    869 }

     

    转载于:https://www.cnblogs.com/mizersy/p/10344832.html

    展开全文
  • 用到最短路径算法,有详细注释、说明书,用户信息存于数据库中
  • JAVA期末大作业之学生信息管理简洁版系统

    万次阅读 多人点赞 2018-07-01 18:42:01
    一、界面化登录窗体,实现主要的界面布局//package newpackage;...import java.awt.*; import java.awt.event.*; import java.util.Vector; public class StudentFrame extends JFrame { //创建一个登录界面 Vect...

    一、界面化登录窗体,实现主要的界面布局

    //package newpackage;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Vector;
    
    public class StudentFrame extends JFrame {    //创建一个登录界面
          Vector<Student> v = new Vector<Student>();           //创建一个动态数组的对象
         // Student student;         
        StudentLogin student = new StudentLogin();     //创建一个atm对象
        int num;
        String num1;    
        Container pane;          //content pane是container的一个对象
        CardLayout cc = new CardLayout();    //卡片布局
        JLabel labTop = new JLabel("学生信息管理系统");     //创建了一个标签写着欢迎你
        JLabel labTop2 = new JLabel("  "); //创建了另外一个空标签
        JLabel labTop3 = new JLabel("  "); //创建了另外一个空标签
        JLabel labTop4 = new JLabel("  "); //创建了另外一个空标签
        JLabel labTop5 = new JLabel("  "); //创建了另外一个空标签
         
        JTextField txtName = new JTextField(15);           //名字输入控制在15个字符以内
        JPasswordField txtPassword = new JPasswordField(15);   //密码输入控制在15个字符以内
        JButton butOK = new JButton("  登录  ");                 //创建一个OK按钮
        JButton butexit = new JButton("  退出  ");   
        public StudentFrame() {                                  //登录窗体标题框内容
            super("用户登录");
            initalInterface();
        }   
        private void initalInterface() {    //初始化设置       
            //设置登录按钮的字体
            Font font = new Font("Dialog", Font.BOLD, 16);
            butOK.setFont(font);
           //设置退出按钮的字体
            Font font2 = new Font("Dialog", Font.BOLD, 16);
            butexit.setFont(font2);        
           //设置"学生信息管理系统"的字体
            Font font1 = new Font("Dialog", Font.BOLD, 30);
            labTop.setFont(font1);              
            //开始构建菜单
            //******************************************************
            JMenuBar menuBar;
            JMenu menu;
            JMenuItem menuItem;      
            //创建菜单条
            menuBar = new JMenuBar();
            this.setJMenuBar(menuBar);       
            //创建File菜单
            menu = new JMenu("文件");
            menuBar.add(menu);
            menuItem=new JMenuItem("打开");
            menu.add(menuItem);
            menu.addSeparator();
            menuItem = new JMenuItem("退出");     
            //给菜单项Exit添加活动事件处理程序
            menuItem.addActionListener(new java.awt.event.ActionListener() {
    
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                finish();
                System.exit(0);
            }
            });
            menu.add(menuItem);
            //创建Help菜单
            menu=new JMenu("帮助");
            menuBar.add(menu);
            menuItem=new JMenuItem("关于");
            menu.add(menuItem);
            //给菜单项About添加活动事件处理程序
            menuItem.addActionListener(new java.awt.event.ActionListener() {
    
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    aboutActionPerformed(evt);              
                }
            });
            //******************************************************
            //结束构建菜单
            
            //构建第一张卡片panLogin
            //*******************************************************
            JPanel panLogin = new JPanel();
            BoxLayout v = new BoxLayout(panLogin, BoxLayout.Y_AXIS);
            panLogin.setLayout(v);
            JPanel pt = new JPanel();
            panLogin.add(pt);
            pt = new JPanel();
            labTop.setForeground(Color.BLACK);
            pt.add(labTop);
            panLogin.add(pt);
            pt = new JPanel();
            pt.add(new JLabel("用户名:"));
            pt.add(txtName);
            panLogin.add(pt);
            pt = new JPanel();
            pt.add(new JLabel("密码:"));
            pt.add(txtPassword);
            panLogin.add(pt);
            //给OK按钮添加活动事件处理程序
            butOK.addActionListener(new java.awt.event.ActionListener() {
    
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    butOKActionPerformed(evt);
                }
            });
            pt = new JPanel();    
            pt.add(butOK);
            panLogin.add(pt);
            panLogin.add(new JPanel());
            //第一张卡片panLogin构建结束****************************************************
            
            //构建第二张卡片panOperation
            //********************************************************************
            JPanel panOperation = new JPanel();
            panOperation.setLayout(new BorderLayout());
            panOperation.add(labTop2, BorderLayout.NORTH);
            panOperation.add(new JLabel("  "), BorderLayout.SOUTH);      
            JLabel labTop7;   
            JPanel p2 = new JPanel();
            p2.setLayout(new GridLayout(5, 3));
            JButton bPut = new JButton("添加学生信息");
           JButton bGet = new JButton("");
            JButton bSearch = new JButton("查询学生信息");
            JButton bQuit = new JButton("退出");
            //给四个按钮分别添加活动事件处理程序
            bPut.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bPutActionPerformed(evt);
                }
            });
            bSearch.addActionListener(new java.awt.event.ActionListener() {
    
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bSearchActionPerformed(evt);
                }
            });
            bQuit.addActionListener(new java.awt.event.ActionListener() {
    
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    bQuitActionPerformed(evt);
                }
            });
            p2.add(new JLabel(" "));
            p2.add(new JLabel(" "));
            p2.add(new JLabel(" "));
    
            p2.add(new JLabel(" "));
            p2.add(bPut);
            p2.add(new JLabel(" "));
    
            p2.add(new JLabel(" "));
             p2.add(new JLabel(" "));
           // p2.add(bGet);
            p2.add(new JLabel(" "));
    
            p2.add(new JLabel(" "));
            p2.add(bSearch);
            p2.add(new JLabel(" "));
    
            p2.add(new JLabel(" "));
           // p2.add(bQuit);
            p2.add(new JLabel(" "));
    
            panOperation.add(p2, BorderLayout.CENTER);
            //第二张卡片panOperation构建结束***************************************************
    
            //在主窗口中添加前面构建的两张卡片panLogin和panOperation
            pane = getContentPane();
            pane.setLayout(cc);
            pane.add(panLogin, "login");
            pane.add(panOperation, "operation");
            //添加窗口关闭事件
            this.addWindowListener(new WindowAdapter() {
    
                //表示下面的方法是覆盖父类的方法
                @Override
                public void windowClosing(WindowEvent e) {
                    finish();
                }
            });
            setSize(550, 550);
            //让起始窗口停在屏幕的中央
            Toolkit kit = Toolkit.getDefaultToolkit();
            Dimension dim = kit.getScreenSize();
            setLocation(dim.width / 2 - 150, dim.height / 2 - 100);
            //禁止用户改变窗口的大小
            this.setResizable(false);
            setVisible(true);
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
        }  
        private void finish(){
            student.saveData();
        }
        //由菜单About调用的方法
        private void aboutActionPerformed(java.awt.event.ActionEvent evt){
             JOptionPane.showMessageDialog(this,"欢迎使用温娉哲开发的学生信息管理系统!","欢迎使用",JOptionPane.INFORMATION_MESSAGE);
        }         
        //分别由五个按钮活动事件处理程序调用的方法
        private void butOKActionPerformed(java.awt.event.ActionEvent evt) {
            String no = txtName.getText();
            String pw = new String(txtPassword.getPassword());
            if (student.check(no, pw)) {
                labTop.setForeground(Color.black);
                labTop.setText("欢迎使用");
                txtName.setText("");
                txtPassword.setText("");
                cc.show(pane, "operation");
            } else {
                labTop.setForeground(Color.red);
                labTop.setText("您的账号或密码错误,请重新输入!");
            }
        }
        public void bPutActionPerformed(java.awt.event.ActionEvent evt) {
             num = Integer.parseInt(JOptionPane.showInputDialog(this, "请添加学生学号:"));
            labTop2.setText("添加的学生学号为:" + num);
            
             num1 = String.format(JOptionPane.showInputDialog(this, "请添加学生姓名:"));           
            //num1 = student.getCurrent(). inputName(num1);
            labTop2.setText("添加的学生姓名为:" + num1);
        }
        public void bSearchActionPerformed(java.awt.event.ActionEvent evt) {
           
           num = Integer.parseInt(JOptionPane.showInputDialog(this, "请输入要查询的学生学号:"));
          // num = student.getCurrent().inputID(num);
            
          // num1 = student.getCurrent().getinformation();
             
           labTop2.setText("学号为" + num+"的学生姓名为"+num1);
        }
        private void bQuitActionPerformed(java.awt.event.ActionEvent evt) {
            labTop2.setText("");
            cc.show(pane, "login");
            txtName.requestFocusInWindow();
        }
        public static void main(String[] args) {
            StudentFrame student = new StudentFrame();
        }
    }

    界面效果如下:

    二、添加登录系统的账号密码信息

    //package newpackage;
    import java.io.*;
    import java.util.*;
    
    public class StudentLogin {
        Scanner sc = new Scanner(System.in);     //获取控制台输入
        Vector<Student> v = new Vector<Student>();           //创建一个动态数组的对象
        private Student atm;  
        //current是Account的对象,是Account类的实例化
          
        public StudentLogin() {
            try {     //异常捕获
                FileInputStream fi = new FileInputStream("accoun1t.txt");
                
                ObjectInputStream oi = new ObjectInputStream(fi);
                v = (Vector<Student>) oi.readObject();
            } catch (FileNotFoundException e) {
                v.add(new Student("admin", "1", 100));
                v.add(new Student("user", "2", 100));
                v.add(new Student("windy", "3", 100));
                v.add(new Student("jason", "4", 100));
            } catch (Exception e) {
                System.out.println("Error - " + e.toString());
            }
        }
    
        public Student getCurrent() {    //获取当前余额
            return atm;
        }
    
        public boolean check(String account, String pw) {     //检查账号,密码是否正确
            int i;    //定义了一个整型变量
            Student a;     
            for (i = 0; i < v.size(); i++) {
                a = v.get(i);
                if (a.check(account, pw)) {
                    atm = a;
                    return true;
                }
            }
           atm = null;
            return false;
        }
        public void saveData() {      //保存数据
            try {
                FileOutputStream fo = new FileOutputStream("account.txt");
                ObjectOutputStream oo = new ObjectOutputStream(fo);
                oo.writeObject(v);
                oo.close();
            } catch (IOException e) {
                System.out.println("Error -" + e.toString());
            }
    
        }
    }
    

    三、主要实现获取学生的学号和姓名,调用方法

    import java.io.*;
    public class Student implements Serializable{
    	private String no;   //账号
    	private String password;   //密码
    	private int information;	   //余额
            private int ID;	   //余额     
            private String name;//姓名
    	public Student(String accounts,String pw,int num) {   //
    		no=new String(accounts);
    		password=new String(pw);
    		information=(num>0?num:0);
    	}
    // 	public String getNo(){      //获取学号
    //	 	return no;
    // 	}
    	public int getinformation() {  //获取学生信息
    		return information;
    	}
    	public int inputID(int num) {   //获取学号
    		
    		return ID;
    	}       
            public String inputName(String num1) {   //获取学生姓名
    		return name;
    	}
    	public boolean setPassword(String pw) {     //修改密码
    		password=new String(pw);
    		return true;
    	}
    	public boolean check(String accounts,String pw) {     //核对密码
    		if(no.equals(accounts)&&password.equals(pw))
    			return true;
    		else
    			return false;	
    	}	
        int Add(int num) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
    }
    

    四、添加学生学号信息,通过查询学生学号检索出学生的姓名(没有使用数据库,大家懂,嘿嘿)

    package newpackage;
    
    import java.io.*;
    import java.util.*;
    
    public class FunctionFrame {
        StudentLogin student = new StudentLogin();     //创建一个student对象
        Scanner sc = new Scanner(System.in);    //创建一个sc对象    scanner获取控制台输入,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给scanner,作为扫描对象        system.in  是输入的意思
    
        public void inputNoPassword() {     //定义了一个无返回值的输入密码的方法
            String account, pw;      //定义了俩个字符串类型的变量
            boolean b;               //定义了一个布尔类型的变量
            do {                    //循环语句,
                System.out.print("Please enter account:");   //提示输入账号
                account = sc.next();        //sc是scanner类中的一个对象,此时表示从结果集中连续取值
                System.out.print("Please enter password:");   //提示输入密码
                pw = sc.next();               //查找并返回来自此扫描器的下一个完整标记
                b = student.check(account, pw);      //检查账号,密码是否正确
                if (b == false) {    //如果账号,密码不相等,提示请重新输入
                    System.out.println("您的账号或密码错误,请重新输入!");
                }
            } while (b == false);    
        }
    
        public void operate() {     //定义了一个返回值为空的操作方法
            int i;   //定义了一个整型变量
            do {                //设置了四个不同的功能
                System.out.println("please choose:");
                System.out.println("添加学生信息");
                System.out.println("");
                System.out.println("查询学生信息");
                System.out.println("");
                try {
                    i = sc.nextInt();
                    if (i == 1) {
                         inputID();
                      //  getMoney();
                    } else if (i == 2) {
                       // putID();
                    } else if (i == 3) {
                        search();
                    } else if (i == 4) {
                        break;
                    } else {
                        System.out.println("输入有误,请重新输入!");
                    }
                } catch (InputMismatchException e) {
                    System.out.println("输入有误\nPlease choose1-4:");
                    sc.nextLine();
                }
            } while (true);
        }
        private void inputID() {
            int i, num;
            System.out.print("Please enter ID : ");
            i = sc.nextInt();
            num = student.getCurrent().inputID(i);
            System.out.println("You have put into: " + num);
        } 
    
        private void search() {
            int num;
            num = student.getCurrent().getBalance();
            System.out.println("学生信息为: " + num);
        }
     
        public void start() {
            do {
                inputNoPassword();
                operate();
                System.out.print("你确定要退出吗? y/n");
                if (sc.next().equals("y")) {
                   student.saveData();
                    break;
                }
            } while (true);
        }
    
        public static void main(String[] args) {
            FunctionFrame atmNoFrame = new FunctionFrame();
            atmNoFrame.start();
        }
    }
    

    五、系统功能概要

    展开全文
  • 聊天系统,可以发文件,发消息,查看在线用户
  • 大四期末大作业,精心做的gui界面,带连通的数据库。界面精美,可移植性高。已经用这个交了好几份作业了,特地来造福人类
  • 第5题:音乐播放器 制作一个音频文件播放器,能够播放多种音频格式的文件。功能包括:【最大选题人数:5】 (1) 实现开始、暂停、停止等播放控制 (2) 维护一个或多个播放列表 (3) 能够从Internet下载歌曲并播放。...
  • 刚学完Java基础写一个通信录管理系统 通信录 提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录通信录前言一、通信录1.定义通信录成员2.定义通信录功能3.主函数实现总结 前言 第一次写...

    刚学完Java基础写一个通信录管理系统

    通信录

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


    前言

    第一次写求大神不要喷一个很简单得通信录,可能还有很多Bug,自己也是写着玩玩增加以下自己得代码量。


    一、通信录

    1.定义通信录成员

    这里的toString方法重写有所改变是为了之后可以
    更好地对字符串进行分割

    代码如下(示例):

    public class Person {
    	String name;
    	String Phone;
    	
    	public Person() {
    		
    	}
    	public Person(String name, String phone) {
    		super();
    		this.name = name;
    		Phone = phone;
    	}
    	public String getName() {
    		return name;
    	}
    	public void setName(String name) {
    		this.name = name;
    	}
    	public String getPhone() {
    		return Phone;
    	}
    	public void setPhone(String phone) {
    		Phone = phone;
    	}
    	@Override
    	public String toString() {
    		return  name + "," + Phone;
    	}
    	
    	
    }
    

    2.定义通信录功能

    代码如下(示例):

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Book {
    //读入数据
    	public void readData(ArrayList<Person> list) {
    		try {
    			BufferedReader br = new BufferedReader(new FileReader("联系人.txt"));
    			String line ;
    			while((line=br.readLine())!=null) {
    				Person p = new Person();
    				String[] datas = line.split(","); 
    				p.setName(datas[0]);
    				p.setPhone(datas[1]);
    				list.add(p);
    			}
    			br.close();
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    	}
    	//回写数据
    	public void writeData(ArrayList<Person> list) {
    		try {
    			BufferedWriter bw = new BufferedWriter(new FileWriter("联系人.txt"));
    			for(Person p:list) {
    				bw.write(p.toString());
    				bw.newLine();
    				bw.flush();
    			}
    			bw.close();
    		} catch (IOException e) {
    			// TODO 自动生成的 catch 块
    			e.printStackTrace();
    		}
    	}
    	//添加
    	public void add() {
    		ArrayList<Person> list = new ArrayList<Person>();
    		Scanner sc = new Scanner(System.in);
    		readData(list);
    		Person p = new Person();
    		System.out.println("请输入姓名:");
    		p.setName(sc.nextLine());
    		System.out.println("请输入手机号:");
    		p.setPhone(sc.nextLine());
    		list.add(p);
    		writeData(list);
    		System.out.println("添加成功!");
    	}
    	//删除
    	public void delete() {
    		ArrayList<Person> list = new ArrayList<Person>();
    		Scanner sc = new Scanner(System.in);
    		readData(list);
    		System.out.println("请输入姓名:");
    		String name = sc.nextLine();
    		for(Person p :list) {
    			if(p.getName().equals(name)) {
    				list.remove(p);
    				writeData(list);
    				System.out.println("删除成功!");
    				break;
    			}
    		}
    	}
    	//修改
    	public void alter() {
    		ArrayList<Person> list = new ArrayList<Person>();
    		Scanner sc = new Scanner(System.in);
    		readData(list);
    		System.out.println("请输入姓名:");
    		String name = sc.nextLine();
    		for(Person p :list) {
    			if(p.getName().equals(name)) {
    				System.out.println("请输入新的手机号");
    				p.setPhone(sc.next());
    				writeData(list);
    				System.out.println("修改成功!");
    				break;
    			}
    		}
    	}
    	//查找
    	public void find() {
    		ArrayList<Person> list = new ArrayList<Person>();
    		Scanner sc = new Scanner(System.in);
    		readData(list);
    		System.out.println("请输入姓名:");
    		String name = sc.nextLine();
    		for(Person p :list) {
    			if(p.getName().equals(name)) {
    		
    				System.out.println(p);
    				break;
    			}
    		}
    	}
    	//显示
    	public void show() {
    		ArrayList<Person> list = new ArrayList<Person>();
    		readData(list);
    		for(Person p : list) {
    			System.out.println(p);
    		}
    	}
    	//菜单
    	public void Menu() {
    		while(true) {
    			System.out.println("通信录菜单");
    			System.out.println("1.增加联系人");
    			System.out.println("2.删除联系人");
    			System.out.println("3.显示所有联系人");
    			System.out.println("4.修改联系人");
    			System.out.println("5.查询联系人");
    			int choice = new Scanner(System.in).nextInt();
    			switch(choice) {
    				case 1:
    					add();
    					break;
    				case 2:
    					delete();
    					break;
    				case 3:
    					show();
    					break;
    				case 4:
    					alter();
    					break;
    				case 5:
    					find();
    					break;
    				default:
    					System.exit(0);
    					break;
    			}
    		}
    	}
    }
    
    

    3.主函数实现

    public class Contacts {
    	public static void main(String[] args) {
    		Book book = new Book();
    		book.Menu();
    	}
    	
    }
    
    

    总结

    提示:这里对文章进行总结:
    例如:以上就是一个简单的通信录代码了可能会有点low但是也是自己写来练手的,不足之处希望大家能多多指教!

    展开全文
  • 使用javase、javafx、jdbc完成。 系统层次功能图 界面截图 下载地址

    使用javase、javafx、jdbc完成。
    在这里插入图片描述

    系统层次功能图
    在这里插入图片描述
    界面截图
    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    在这里插入图片描述

    下载地址

    展开全文
  • 学生信息管理系统包括了学生信息管理,学生选课管理,学生考勤管理,学生成绩管理系统。 功能描述: 登录界面包含了学生,教师,管理员。并且每个的权限不一样,管理员权限最大。 学生列表包含了学生的信息以及增...
  • 2、所用知识 总的来说,这次需要编写三个类 ①Server 作为服务器处理数据 ②GamePanel 游戏面板,主要的游戏逻辑由他实现 ③GameFrame 游戏界面,主要负责把GamePanel加上去,设置界面不可调整等微小工作 所用...
  • 这个学期学习了Java语言,期末大作业还是一个中规中矩的学生信息管理系统,主要运用的知识点有Java的数据库操作、文件操作、图形化界面编程设计以及Java绘图。现将代码全部开源,仅供大家参考。 先贴运行效果图吧: ...
  • java期末大作业
  • label3.setText("太"); } } else if(e.getSource() == Button2) { label1.setVisible(false); Text.setEditable(true); label3.setVisible(false); Button1.setEnabled(true); Text.setText(null); Jpanel.set...
  • Java SSM期末大作业

    2019-03-22 13:21:27
    Java SSM期末大作业。 这是一个SSM期末课程设计大作业。使用ssm技术+bootstrap快速搭建简洁美观的页面;希望可以帮助到大家,喜欢的请动动小手,点个赞。么么哒 南阳理工学院 SSM大作业 课程设计
  • Java电话薄期末大作业

    2021-01-07 20:34:30
    Java电话薄期末大作业 //主类 package phone; import java.io.*; import java.util.ArrayList; import java.util.Scanner; public class Phone { public static ArrayList<Person> list = new ArrayList&...
  • 内有程序源码,数据库,实验报告内有程序源码,适合JAVA WEB,数据库,多媒体大作业
  • java期末项目大作业源码

    万次阅读 2019-06-02 20:44:46
    上学期期末做的大作业 有很多功能没有实现 而且还丑 最后80分过关这门科目 现分享给比我还差的学渣 程序名:DVD租赁管理系统 功能:1.连接数据库 2.增 删 查看所有DVD库 3.直接通过if语句实现用户名密码登陆管理...
  • JAVA学生管理系统大作业 课程名称 JAVA编程基础 题 目: 学生信息管理系统 专 业 计算机软件 班级 计算机软件121班 学 号 8000612030 学生姓名 李俊楠 完成人数 1人 起讫日期 第十六周 至 第十八周 任课教师 龚根华 ...
  • java做的图像化界面,期末大作业,用Eclipse做的。希望对大家有用

空空如也

空空如也

1 2 3 4 5 ... 14
收藏数 262
精华内容 104
关键字:

java期末大作业

java 订阅