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

📄 mainframe.java

📁 。使用Java语言来开发一个简单的游戏一直以来是我的想法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.BorderLayout;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class MainFrame extends JFrame implements KeyListener    //主程序
{
	//声明并创建布局管理器
    BorderLayout borderLayout1 = new BorderLayout();
    KeyListenerDemo  key;
    //游戏界面中菜单栏、菜单项的声明及创建
    JMenuBar jMenuBar1 = new JMenuBar();
    JMenu jMenu1 = new JMenu();
    JMenuItem jMenuItem1 = new JMenuItem();
    JMenuItem jMenuItem2 = new JMenuItem();
    JMenuItem jMenuItem3 = new JMenuItem();
    JMenuItem jMenuItem4 = new JMenuItem();
    JMenu jMenu2 = new JMenu();
    JMenu jMenu3 = new JMenu();
    JMenu jMenu4 = new JMenu();
    //菜单中登记单选框的声明及创建
    ButtonGroup buttonGroup = new ButtonGroup();
    JCheckBoxMenuItem jCheckBoxMenuItem1 = new JCheckBoxMenuItem();
    JCheckBoxMenuItem jCheckBoxMenuItem2 = new JCheckBoxMenuItem();
    JCheckBoxMenuItem jCheckBoxMenuItem3 = new JCheckBoxMenuItem();
   
   
    //返回按钮
   JButton button;
	
    //图形处理,画图类对象声明
    DrawPanel drawPanel;
    //帮助信息Dialog窗口对象声明
    帮助信息 dialog;
    //声明SuperPlate盘子类对象数组
    SuperPlate superPlate[];
    int plate_num;      //保存当前游戏指定难度的盘子数   
    //声明三个SuperPlate对象,用于提取vector数组中末端(最上方)盘子对象以便操作
    SuperPlate sp1,sp2,sp3;    
    //设置3个vector数组用来存取盘子SuperPlate的对象,代表3个柱子上盘子
    Vector s1,s2,s3;          
    //表示当前控制对象是哪个柱子,值为true的表示被选中
    boolean b1,b2,b3;
    
    int d = 20;                       //表示相邻盘子之间的距离基值
    int press_num=0;                  //统计移动盘子次数
    int num,winNum;					  //游戏胜利时,最右侧杆上盘子数			  
    boolean chooseLever = false;    //游戏开始参数:是否选择游戏难度
    //-------------------------------------------------------------------
    public MainFrame()
    {
    	//设置内容面板布局方式
        getContentPane().setLayout(borderLayout1);
        //添加键盘事件监听器
        addKeyListener(this);	
        
        //创建帮助信息Dialog窗口对象
        dialog = new 帮助信息();
        //创建画图类对象
        drawPanel = new DrawPanel(this);
       // KeyListenerDemo  key=new KeyListenerDemo();
      //  drawPanel.setVisible(false);
         button=new JButton();
        // button.setText("返回");
	    
        this.setJMenuBar(jMenuBar1);   //添加菜单栏
        jMenu1.setText("设定");
        jMenu2.setText("游戏等级");
        jMenu3.setText("帮助");
        jMenu4.setText("返回");
        //设置等级单选框
        jCheckBoxMenuItem1.setText("初级");
        jCheckBoxMenuItem2.setText("中级");
        jCheckBoxMenuItem3.setText("高级");
        //为等级单选框添加事件监听器
        jCheckBoxMenuItem1.addActionListener(new JCheckBoxMenuItem1_actionAdapter());     
        jCheckBoxMenuItem2.addActionListener(new JCheckBoxMenuItem2_actionAdapter());
        jCheckBoxMenuItem3.addActionListener(new JCheckBoxMenuItem3_actionAdapter());
        
        buttonGroup.add(jCheckBoxMenuItem1);
        buttonGroup.add(jCheckBoxMenuItem2);
        buttonGroup.add(jCheckBoxMenuItem3);
        
        //设置菜单栏中,其他菜单项
        jMenuItem1.setText("开始");
        jMenuItem2.setText("游戏信息");
        jMenuItem3.setText("结束");
        jMenuItem4.setText("返回主菜单");
        //为菜单栏中的其他菜单项添加事件监听器
        jMenuItem1.addActionListener(new JMenuItem1_actionAdapter());
        jMenuItem3.addActionListener(new JMenuItem3_actionAdapter());
        jMenuItem2.addActionListener(new JMenuItem2_actionAdapter());
        jMenuItem4.addActionListener(new JMenuItem4_actionAdapter());
  
        //向菜单栏中添加菜单项
        jMenu1.add(jMenuItem1);
        jMenu1.addSeparator();
        jMenu1.add(jMenuItem3);
        jMenu2.add(jCheckBoxMenuItem1);
        jMenu2.add(jCheckBoxMenuItem2);
        jMenu2.add(jCheckBoxMenuItem3);
        jMenu3.add(jMenuItem2);
        jMenu4.add(jMenuItem4);
        
        jMenuBar1.add(jMenu1);
        jMenuBar1.add(jMenu2);
        jMenuBar1.add(jMenu3);
        jMenuBar1.add(jMenu4);
        //创建Vector对象s1、s2、s3
        s1 = new Vector();;
        s2 = new Vector();
        s3 = new Vector();
        
        //向内容面板中加入画图面板
        getContentPane().add(drawPanel, java.awt.BorderLayout.CENTER);
       // getContentPane().add(button,java.awt.BorderLayout.NORTH);
         this.setBounds(180,150,400,400);       //设置界面大小
        //setVisible(true);		   //设置为可见
    }
    
  /*  public static void main(String[] args)       
    {
        MainFrame mainframe = new MainFrame();
        mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } 
  */ 
    //-----------------------------------------------------------------
    //内布类用于处理菜单中,"开始"菜单项的动作
    class JMenuItem1_actionAdapter implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
            if(chooseLever == true)                   //若已选定等级情况
            {
                drawPanel.choose = false;
            }
            else                                      //若未选定等级情况
            {

                drawPanel.choose = true;
            }
            drawPanel.start = true;
            System.out.println("点开始了 ");
            drawPanel.setVisible(true);
        }
    }    
    //内布类用于处理菜单中,"帮助信息"菜单项的动作
    class JMenuItem2_actionAdapter implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)    //菜单项“帮助信息”动作
        {
        	dialog.setVisible(true);
        }
    } 
    //内布类用于处理菜单中,"关闭"菜单项的动作
    class JMenuItem3_actionAdapter implements ActionListener 
    {
        public void actionPerformed(ActionEvent e)    //菜单项“关闭”动作
        {
       String m="确定要退出游戏吗?";
       int ok=JOptionPane.showConfirmDialog(null,m,"确认",JOptionPane.YES_NO_OPTION,
                                              JOptionPane.QUESTION_MESSAGE);
       if(ok==JOptionPane.YES_OPTION)
       { 
           setVisible(false);
            System.exit(0);
       }
       
     
           
        }
    }
    
       //内布类用于处理菜单中,"返回"菜单项的动作
      
    class JMenuItem4_actionAdapter implements ActionListener 
    {
       public void actionPerformed(ActionEvent e)    
        {
        	System.out.println("返回");
        	 setVisible(false);
        	// key.setVisible(true);
        }
    }    
    //------------------------------------------------------------------
    //内布类用于处理菜单中,游戏难度选择"初级"单选框的动作
    class JCheckBoxMenuItem1_actionAdapter implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
        	s1.removeAllElements();
            s2.removeAllElements();
            s3.removeAllElements();
            plate_num=3;          //初级:设定盘子数为3
            drawPanel.setPlateNum(plate_num); //向drawPanel传递参数
            chooseLever = true;   //已选择等级chooseLever值设置为true
            drawPanel.choose = false;
            
            //初始化SuperPlate对象并赋值
            superPlate = new SuperPlate[plate_num];     
            for(int x=0;x<3;x++)
            {
                superPlate[x] = new SuperPlate(100,290-x*d,50-x*d/2);
                //将盘子加入到vector的数组s1中,相当于加到柱1上
                s1.addElement(superPlate[x]);           
            }
            getactionZhu();   //调用getactionZhu()方法,
            
            //调用DrawPanel对象drawPanel的数据更新方法
            drawPanel.updata();                         
            jCheckBoxMenuItem2.setVisible(true);
            jCheckBoxMenuItem3.setVisible(true);
        }
    }
    //内布类用于处理菜单中,游戏难度选择"中级"单选框的动作
    class JCheckBoxMenuItem2_actionAdapter implements ActionListener 
    {
        public void actionPerformed(ActionEvent e) 
        {
        	s1.removeAllElements();
            s2.removeAllElements();
            s3.removeAllElements();
            plate_num=4;              //中级:设定盘子数为4
            drawPanel.setPlateNum(plate_num);     //向drawPanel传递参数
            chooseLever = true;       //已选择等级chooseLever值设置为true
            drawPanel.choose = false;
            
            //初始化SuperPlate对象并赋值
            superPlate = new SuperPlate[plate_num];
            for(int x=0;x<4;x++)
            {
                superPlate[x] = new SuperPlate(100,290-x*d,50-x*d/2);
                //将盘子加入到vector的数组s1中,相当于加到柱1上
                s1.addElement(superPlate[x]);
       
            }
            getactionZhu();

⌨️ 快捷键说明

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