⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 workmain.java

📁 五子连珠游戏
💻 JAVA
字号:

import javax.swing.*;
import java.io.*;

import java.awt.*;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.swing.*;
public class workmain extends JFrame implements ActionListener{
	/*该类为主类,主要的界面和菜单都在这里处理,该界面的布局为BorderLayout,


	 */
			private static final int WIDTH=575;
			private static final int HEIGHT=400;
			
			Container content;                                //面板

			
			nameScores playerName[]=new nameScores[5];        //类nameScores用来记录之前的最高分
			                                                  //数据从文件scores.txt获取    
			
			//布局的西部,主要贴图像,由于在centerjpanel可能要改变图像
			//所以也将forPicture作为参数传给centerjpanel
			JLabel forPicture;
			private void west()                          //
			{
				forPicture=new JLabel(new ImageIcon("r-1.gif"));
				content.add(forPicture,BorderLayout.WEST);
			}
			
			
            //布局的东部,主要贴图像,由于在centerjpanel可能要改变图像
			//所以也将forPicture作为参数传给centerjpanel
			JLabel nowPicture;
			private void east()
			{
				nowPicture=new JLabel(new ImageIcon("s-2.gif"));
				content.add(nowPicture,BorderLayout.EAST);
			}
			
			
			//界面中间部分 ,主要为类centerJPanel,其中参数textField2
			//用来改变当前分数,forPicture,nowPicture用来改变两侧图像
			//playerName为最高分的排行。score为之前的最高分,传入便于比较
			centerJPanel centerjpanel;  
			int score;              //当前的分数,由于分数改变在centerjpanel
			                        //所以必须将其作为参数传给centerjpanel
			private void center()
			{
				
				textField2=new JTextField(strnow);     
				centerjpanel=new centerJPanel(textField2,forPicture,nowPicture,score,playerName);
				content.add(centerjpanel);
			}
				
			
	
			/*界面的北部northPanel
			 * 的布局为GridLayout(1,3),第1格放一个JPanel(text1Panel),text1Panel包含
			 * JTextField(textField1)用来显示之前的最高
			 * 分,第2格放一个JPanel(nextColorPanel),nextColorPanel的布局为GridLayout(1,5)
			 * 里面放置一个显示next的JLabel(nex),三个jform类,一个显示Color的JLabel(col),
			 * 第3格放一个JPanel(text2Panel),text2Panel包含JTextField(textField2)用来显示
			 * 当前分数。
			 */
			JPanel northPanel;
			JPanel nextColorPanel;
			JPanel text1Panel,text2Panel;     
			JTextField textField1,textField2;  //最高分和当前分
			String strmax,strnow;             //最高分和当前分
			JLabel nex,col;
			private void north()                                      //上面的内容:包括两个分
			{                                                           //数显示框,和下次出现球的...
				northPanel=new JPanel();                               
				northPanel.setLayout(new GridLayout(1,3));
				strmax=""+score;
				textField1=new JTextField(strmax);
				textField1.setHorizontalAlignment(JTextField.CENTER);
				textField1.setEditable(false);                           // 以前的最高分数
				textField1.setColumns(6);                            //有效果
				text1Panel=new JPanel();
				text1Panel.setLayout(new FlowLayout());
				text1Panel.add(textField1);
				northPanel.add(text1Panel);
				
				nextColorPanel=new JPanel();
				nextColorPanel.setLayout(new GridLayout(1,5));
				nex=new JLabel();
				nex.setText("Next");
				nextColorPanel.add(nex);
				for(int i=0;i<3;i++)
				{
					nextColorPanel.add(centerjpanel.next[i]);
				}
				col=new JLabel();
				col.setText("Colors");
				nextColorPanel.add(col);
				northPanel.add(nextColorPanel);
				
				
				
				//textField2=new JTextField(strnow);                           //现在分数
				textField2.setEditable(false);
				textField2.setHorizontalAlignment(JTextField.CENTER);
				textField2.setColumns(6);                            //
				text2Panel=new JPanel();
				text2Panel.setLayout(new FlowLayout());
				text2Panel.add(textField2);
				northPanel.add(text2Panel);
				
				content.add(northPanel,BorderLayout.NORTH);
			}
			
			
			
			//创建菜单
			private Menu menu1,menu2;                         //菜单项"File",“Options“
			CheckboxMenuItem item1,item2;             //菜单“Next”,"Show statistics"的类型
			private void creatmenu1()                             //菜单1“File”
			{
				MenuItem item;
				menu1=new Menu("File");		
				item=new MenuItem("New");                       //New
				item.addActionListener(this);
				menu1.add(item);
				
		        //menu3=new Menu();
				//CheckboxMenuItem item2;
				item=new MenuItem("Show high scores");          //Show higt scores
				item.addActionListener(this);
				menu1.add(item);
				
				menu1.addSeparator();                          //分割线
				
				item=new MenuItem("Exit");                    //Exit 
				item.addActionListener(this);
				menu1.add(item);
			}
			private void creatmenu2()                               //菜单2“Options”
			{
				menu2=new Menu("Options");				
				item1=new CheckboxMenuItem("Next",true); 
				//Next
				//item1.addActionListener(this);
				menu2.add(item1);
				item2=new CheckboxMenuItem("Show statistics");          //Show statistics
				//item2.addActionListener(this);
				menu2.add(item2);
				
				item1.addItemListener(                                    //添加监听,菜单的类型不
						new java.awt.event.ItemListener(){                //同监听就不同吧!
							public void itemStateChanged(ItemEvent e)    //这个从网上找的资料
							{
								item1_Click(e);
							}
							
						});
				item2.addItemListener(                                     //与上雷同
						new java.awt.event.ItemListener(){
							public void itemStateChanged(ItemEvent e)
							{
								item2_Click(e);
							}
							
						});
			}             
			public void item1_Click(ItemEvent e)                          //菜单“next”事件处理函数。
			{
				boolean key;
				key=item1.getState();
				if(key)
				{
					for(int i=0;i<3;i++)
						centerjpanel.deal(i, centerjpanel.next[i].getcolor());
					    centerjpanel.menukey=key;                          //由于起初的构思上的缺陷
				}                                                   //必须给类centerjpanelmenukey值
				                                                     //用来决定是否显示next的小球
				                                                      //nemukey的值为false时为显示
				                                                      //否则为不显示
				else                                              
				{
					for(int i=0;i<3;i++)
					centerjpanel.deal(i, 7);
					centerjpanel.menukey=false;
				}
				
			};
			public void item2_Click(ItemEvent e)           //菜单"Show statistics"的处理函数
			{                                                         
				boolean key;
				key=item2.getState();
				if(key)
				{
					centerjpanel.showNum(key);

				}
				else
			    {
					
					centerjpanel.menukey=false;
				}
				
			};
			public void actionPerformed(ActionEvent event)//菜单其他项的处理函数
			{
				String menuName;
				menuName=event.getActionCommand();
				if(menuName.equals("Exit"))
					System.exit(0);
				else if(menuName.equals("New"))
				{
					centerjpanel.initJPanel();	   //重新初始化某些数值
				}
				else if(menuName.equals("Show high scores"))//显示之前的分数排行
				{
					JOptionPane.showMessageDialog(null,
                            "1."+playerName[0].getName()+"    "+playerName[0].getScore()+'\n'+
                            "2."+playerName[1].getName()+"    "+playerName[1].getScore()+'\n'+
                            "3."+playerName[2].getName()+"    "+playerName[2].getScore()+'\n'+
                            "4."+playerName[3].getName()+"    "+playerName[3].getScore()+'\n'+
                            "5."+playerName[4].getName()+"    "+playerName[4].getScore()+'\n',
                            "Hello",
                           JOptionPane.OK_OPTION);
				}
					
			}
			
			
			//数据的载入,之前分数排行的载入
			public  void inputFile() throws ClassNotFoundException,IOException//载入函数
			{
			    File inFile=new File("scores.txt");
			    FileInputStream inFileStream=new FileInputStream(inFile);
			    ObjectInputStream inObjectStream=new ObjectInputStream(inFileStream);
                for(int i=0;i<5;i++)
                {
                	playerName[i]=(nameScores)inObjectStream.readObject();
                }
                inObjectStream.close();
			}
			public  void outputFile() throws ClassNotFoundException,
			IOException                                                  //保存函数,本函数不用,
			{                                                            //主要用来初始化排行
			    File outFile=new File("scores.txt");                     //数据保存文件
			    FileOutputStream outFileStream=new FileOutputStream(outFile);
			    ObjectOutputStream outObjectStream=new ObjectOutputStream(outFileStream);
                for(int i=0;i<5;i++)
                {
                	outObjectStream.writeObject(playerName[i]);
                }
                outObjectStream.close();
			}
			
			
			
			 //构造函数
			public workmain ()                                  
			{	
				setTitle("Color linez");                      //窗口的参数
				setSize (WIDTH,HEIGHT);
				setResizable(false);                          //大小不能改变
				
				/*for(int i=0;i<5;i++)//用于初始化文件,既之前的分数纪录
				{
					playerName[i]=new nameScores();
				}
				for(int i=0;i<5;i++)
				{
					playerName[i]=new nameScores("empty",0);
				}
				playerName[0]=new nameScores("empty",100);
				try{
					
					outputFile();//throws ClassNotFoundException,IOException
				}catch(IOException e){
					System.out.println("11yichang");
				
				}catch (ClassNotFoundException e){}/*
				/*/
				
				try{
					
				inputFile();//throws ClassNotFoundException,IOException;
				}catch(IOException e){
					System.out.println("1yichang");
					System.exit(0);
				}catch (ClassNotFoundException e){
					System.out.println("2yichang");
					System.exit(0);
				}
				score=playerName[0].getScore();               //之前的最高分数
				strnow=""+0;
				content=getContentPane();                         //获取主要面板
				content.setLayout(new BorderLayout(5,5));  
				content.setBackground(new Color(106,168,254));           //布局,背景为黑色
				
				MenuBar menuBar=new MenuBar();                   //菜单
				setMenuBar(menuBar);
				
				west();                                          //界面的各个部分
				east(); 
				center();
				north();     
				creatmenu1();                                     //菜单
				creatmenu2();
				menuBar.add(menu1);
				menuBar.add(menu2);
				setVisible(true);
				setDefaultCloseOperation(EXIT_ON_CLOSE);		
			}
			
			public static void main(String[] args) throws IOException {
				
				new workmain();
				
				
			}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -