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

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 max)
    			{
    				max = slot[k];
    			}
    		}

    		int m;
    		
    		for(m=0;m= 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 stub
		new JAVA();

	}

}


你可能感兴趣的:(JAVA__大作业_4_3X题目用户图形界面实现)