JAVA__大作业_4_3X题目用户图形界面实现

来源:互联网 发布:python关键字怎么看 编辑:程序博客网 时间:2024/06/06 12:37

JAVA__大作业_4_3X题目用户图形界面实现

2014年12月1日



1:当前时间显示

2:八皇后

3:落球游戏

4:四子棋

// 个人原创,严禁抄袭  

code:

import java.awt.Button;import java.awt.Color;import java.awt.FlowLayout;import java.awt.BorderLayout;import java.awt.TextArea;import java.awt.Font;import java.awt.Frame;import java.awt.SystemColor;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;//监听器import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Scanner;import java.awt.*;import java.awt.event.*;import javax.swing.*;public class JAVA {JFrame mainWindow;JTextField textField;public JAVA(){mainWindow = new JFrame("JAVA_4X_TEXT");        mainWindow.setBounds(100,100,250,200);        mainWindow.setLayout(new GridLayout(4,2));        //mainWindow.setBounds(10,10,10,10);        JButton JB1 = new JButton("Text_1");        JButton JB2 = new JButton("Text_2");        JButton JB3 = new JButton("Text_3");        JButton JB4 = new JButton("Text_4");        JLabel j1 = new JLabel("Current Time:");        JLabel j2 = new JLabel("Eight Quees:");        JLabel j3 = new JLabel("Bean Machine:");        JLabel j4 = new JLabel("Connet Four:");        JB1.addActionListener(new creatNewWindow_1());        JB2.addActionListener(new creatNewWindow_2());        JB3.addActionListener(new creatNewWindow_3());        JB4.addActionListener(new creatNewWindow_4());                mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        textField = new JTextField();        textField.setBackground(new Color(245, 222, 179));textField.setForeground(SystemColor.textHighlight);        textField.setFont(new Font("幼圆", Font.BOLD, 21));        textField.setColumns(35);        textField.setText("Press any Button to choose text.");    //mainWindow.add(textField);        mainWindow.add(j1);        mainWindow.add(JB1);        mainWindow.add(j2);        mainWindow.add(JB2);        mainWindow.add(j3);        mainWindow.add(JB3);        mainWindow.add(j4);        mainWindow.add(JB4);        mainWindow.setVisible(true);      //  mainWindow.pack();}public class win_1{JFrame window_1;public win_1(){window_1 =new  JFrame("Current Date and Time");            window_1.setBounds(20,20,550,80);            window_1.setVisible(true);           // window_1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            //show time             JTextField time = new JTextField();                           time.setFont(new Font("Monospaced", Font.BOLD, 17));    time.setBackground(Color.GRAY);    time.setForeground(Color.WHITE);                        window_1.getContentPane().add(time);            final JTextField varTime = time;               Timer timeAction = new Timer(1000, new ActionListener() {                                public void actionPerformed(ActionEvent e) {                           long currentTime = System.currentTimeMillis();            //SimpleDateFormat form = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");            SimpleDateFormat formm = new SimpleDateFormat("MM");            SimpleDateFormat formmm = new SimpleDateFormat(" dd, yyyy HH:mm:ss");            Date date = new Date(currentTime);            String M=formm.format(date);            //String c=M.substring(beginIndex, endIndex);            int monnn=Integer.parseInt(M);            String Mon="";            switch(monnn)            {            case 1:Mon="January";break;            case 2:Mon="February";break;            case 3:Mon="March";break;            case 4:Mon="April";break;            case 5:Mon="May";break;            case 6:Mon="June";break;            case 7:Mon="July";break;            case 8:Mon="August";break;            case 9:Mon="September";break;            case 10:Mon="October";break;            case 11:Mon="November";break;            case 12:Mon="December";break;            }            String aaa="Current date and time is ";            String bbb=aaa+Mon+formmm.format(date);                    varTime.setText( bbb );                }                  });                        timeAction.start(); }}public class win_2{JFrame window_2;TextArea text = new TextArea(); public win_2(){window_2 =new  JFrame("Eight Quees Plus");            window_2.setBounds(20,20,500,500);            //window_2.setLayout(new FlowLayout());                        JPanel panel = new JPanel();           // panel.add(new JButton("Press to continue"));            window_2.add(panel,BorderLayout.SOUTH);                        text.setFont(new Font("Monospaced", Font.BOLD, 22));    text.setBackground(Color.GRAY);    text.setForeground(Color.WHITE);                        window_2.add(text,BorderLayout.CENTER);            window_2.setVisible(true);            DFS(1);}int n=8;int[] map=new int[10];int Case=1;public void DFS(int k){    int i,j,flag;    if(k==n+1)    {    text.append("The " + Case++ + " way:\n");        write_map();        return;    }    else    {        for(i=1;i<=n;++i)        {            map[k]=i;            flag=1;            for(j=1;j<k;++j)            {                if(map[j]==i||i-k==map[j]-j||i+k==map[j]+j)   //回溯                {                    flag=0;                    break;                }            }            if(flag==1)                DFS(k+1);        }    }}public void write_map(){    int i,j;    for(i=1; i<=n; ++i)    {    text.append("|");        for(j=1; j<=n; ++j)        {            if(map[i]!=j)//System.out.printf(" |");            text.append(" |");            else //System.out.printf("Q|");            text.append("Q|");        }        text.append("\n");    }}}    public class win_3{    JFrame window_3;    TextArea text2 = new TextArea();     public win_3(){window_3 =new  JFrame("Bean Machine");            window_3.setBounds(20,20,500,500);            window_3.setVisible(true);          //  window_3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            JLabel textf = new JLabel("Balls:");            JLabel textc = new JLabel("Slots:");            JTextField tex1 = new JTextField(10);            tex1.setText("0");            JTextField tex2 = new JTextField(10);            tex2.setText("0");            JPanel panel2 = new JPanel();            //panel2.add(new JButton("Press to continue"));            window_3.add(panel2,BorderLayout.SOUTH);                        text2.setFont(new Font("Monospaced", Font.BOLD, 17));    text2.setBackground(Color.GRAY);    text2.setForeground(Color.WHITE);            window_3.add(text2,BorderLayout.CENTER);            window_3.setVisible(true);            JButton OK = new JButton("Start");            panel2.add(textf);            panel2.add(tex1);            panel2.add(textc);            panel2.add(tex2);            panel2.add(OK);            OK.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e)            {            String a = tex1.getText();                String b = tex2.getText();                if("".equals(tex1.getText())){            JOptionPane.showMessageDialog(null,"Please Write Number!");            return;            };            if("".equals(tex2.getText())){            JOptionPane.showMessageDialog(null,"Please Write Number!");            return;            };                int aa=Integer.parseInt(a);                int bb=Integer.parseInt(b);                if(aa<=0 || bb<=0)                {                JOptionPane.showMessageDialog(null,"Error Number!");                return;                };                    make_game(aa,bb);            }            //make_game();            });            }    ////////////////////////////////////////////////////////////////////////////////////////////    public void make_game(int a,int b)    {    //Scanner scanner = new Scanner(System.in);    //System.out.println("Enter the number of balls to drop: ");    //int num = scanner.nextInt();    //System.out.println("Enter the number of slots in the bean machine: ");    //int slots = scanner.nextInt();    //System.out.println();    play(a,b);    }    public void play(int num,int slots)    {    int[] slot = new int[slots+1];    int k;    for(k=0;k<slots+1;k++)    slot[k] = 0;    int i;    //text2.append("|");    for(i=0;i<num;i++)    {    int j;    int count = 0;                 //记录下R的个数以计算出球最后的落点,R为0则落在最左边,R为1则在左边第二个,以此类推。    for(j=0;j<slots;j++)    {    if((int)((Math.random()*10) % 10) <= 4) //0-9模拟随机数  若随机数在0-4为L,5-9为R    {    //System.out.print("L");    text2.append("L");    }    else     {    //System.out.print("R");    text2.append("R");    count++;    }    }    slot[count]++;    //System.out.println();    text2.append("\n");    }    System.out.println();    int max = slot[0];                      //找出哪一个槽中球最多 以便作图    for(k=0;k<slots+1;k++)    {    if(slot[k] > max)    {    max = slot[k];    }    }    int m;        for(m=0;m<max;m++)                         //作图,作图的时候画出max行,max为单个槽中最大球数量。    {    int n;    text2.append("|");    for(n=0;n<slots+1;n++)    {    if(slot[n] >= max - m)             //有球画0,无球画空格。    {    //System.out.print("O");    text2.append("O|");    }    else    //System.out.print(" ");    text2.append(" |");    }    //System.out.println();    text2.append("\n");    }        }        ////////////////////////////////////////////////////////////////////////////////////////////}    public class win_4{    JFrame window_4;    TextArea text3 = new TextArea();     int start=0;    boolean p=true;    int p1=0;    int p2=0;    int map[][]=new int[6][7];    int judge;    public win_4(){window_4 =new  JFrame("Connet Four");            window_4.setBounds(20,20,450,350);            window_4.setVisible(true);           // window_4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);            JLabel textf = new JLabel("num:");            JTextField tex1 = new JTextField(10);            tex1.setText("0");            JPanel panel2 = new JPanel();            //panel2.add(new JButton("Press to continue"));            window_4.add(panel2,BorderLayout.SOUTH);                        text3.setFont(new Font("Monospaced", Font.BOLD, 20));    text3.setBackground(Color.GRAY);    text3.setForeground(Color.WHITE);            window_4.add(text3,BorderLayout.CENTER);            window_4.setVisible(true);            JButton SK = new JButton("Start");            JButton OK = new JButton("Next");            JButton BK = new JButton("End");            panel2.add(SK);            panel2.add(textf);            panel2.add(tex1);            panel2.add(OK);            panel2.add(BK);            SK.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e)            {            //start_game();            start=1;            p=true;            text3.append("Drop red disk at column (0-6):\n");            for(int i=0; i<6; ++i)                for(int j=0; j<7; ++j)                map[i][j]=0;                write_map();            }            });            OK.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent e)            {            if(start==0)            {            JOptionPane.showMessageDialog(null,"Please Press Start First!");                return;            }            //while(atart==1)            String a = tex1.getText();            if("".equals(tex1.getText())){            JOptionPane.showMessageDialog(null,"Please Write Number!");            return;            };                int aa=Integer.parseInt(a);                if(aa<0||aa>6)                {                JOptionPane.showMessageDialog(null,"Error Number!");                return;                };                   //cle                int poi,k,n=0;                poi=aa;                if(poi<0 || poi>6)        {                JOptionPane.showMessageDialog(null,"please input another point");        return;        }            ++n;            if(map[0][poi]>0)            {            JOptionPane.showMessageDialog(null,"Wrong position ,please input another point");                return;            }            for(k=5; map[k][poi]>0; --k);            map[k][poi]=p?1:2;            p=!p;            text3.setText("");            write_map();           // a=b=c=d=0;            judge=find_num(k,poi);            //if(judge>0)break;            if(judge==1)            {            //System.out.println("The red player won.");            text3.append("The red player won.\n END!\n");            JOptionPane.showMessageDialog(null,"The red player won!");            start=0;            return;            }            else if(judge==2)            {            //System.out.println("The yellow player won.");            text3.append("The yellow player won.\n END!\n");            JOptionPane.showMessageDialog(null,"The yellow player won!");            start=0;            return;            }            else if(n==42)            {            JOptionPane.showMessageDialog(null,"It ends in a draw.");            text3.append("\n END in a draw !\n");            start=0;                return;            }            if(p){            //System.out.print("Drop red disk at column (0-6):");            text3.append("Drop red disk at column (0-6):\n");            }            else {            //System.out.print("Drop yellow disk at column (0-6):");            text3.append("Drop yellow disk at column (0-6):\n");            }                //////////////////////////////////////////////////////////////////////////                //p1=1;                   // make_game(aa,bb);            }            });            BK.addActionListener(new ActionListener(){            public void actionPerformed(ActionEvent b)            {            start=0;                   // make_game(aa,bb);            }            });            }    /////////////////////////////////////////////////////////////////////////////////////////    int find_num(int x,int y)    {    int a,b,c,d;    int i,j;    a=b=c=d=0;        judge=map[x][y];        for(i=x,j=y; i<=5 && j<=6 && map[i][j]==map[x][y]; ++i,++j,++c);        if(c>=4)return judge;        for(i=x,j=y; i<=5 && map[i][j]==map[x][y]; ++i,++b);        if(b>=4)return judge;        for(i=x,j=y; i<=5 && j>=0 && map[i][j]==map[x][y]; ++i,--j,++a);        if(a>=4)return judge;        for(j=y; j>=0 && map[x][j]==map[x][y]; --j,++d);        for(j=y+1; j<=6 && map[x][j]==map[x][y]; ++j,++d);        if(d>=4)return judge;        return 0;    }        void write_map()    {    int i,j;        for(i=0; i<6; ++i)        {            for(j=0; j<7; ++j)            {                if(map[i][j]==0)//System.out.print("| ");                text3.append("| ");                else if(map[i][j]==1)//System.out.print("|R");                text3.append("|R");                else //System.out.print("|Y");                text3.append("|Y");            }           // System.out.println("|");            text3.append("|\n");        }        //System.out.println("-----------------------------");        text3.append("-----------------------------\n");    }    /////////////////////////////////////////////////////////////////////////////////////////}class creatNewWindow_1 implements ActionListener{        public void actionPerformed(ActionEvent e){            new win_1();        }    }class creatNewWindow_2 implements ActionListener{        public void actionPerformed(ActionEvent e){        new win_2();        }    }class creatNewWindow_3 implements ActionListener{        public void actionPerformed(ActionEvent e){        new win_3();        }    }class creatNewWindow_4 implements ActionListener{        public void actionPerformed(ActionEvent e){        new win_4();        }    }public static void main(String[] args) {// TODO Auto-generated method stubnew JAVA();}}


0 0
原创粉丝点击