Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向数组中添加按钮_Java_Arrays_Swing - Fatal编程技术网

Java 向数组中添加按钮

Java 向数组中添加按钮,java,arrays,swing,Java,Arrays,Swing,我有208个按钮,我想使用数组为它们分配数字。这些按钮上面有不同的图像,我希望当我把相应的数字放在屏幕上时,它们会出现在屏幕上,代表屏幕上的按钮。可以忽略二维阵列。我知道我必须去掉这段代码,为数组编写代码来代替这段代码,但我不知道我必须做什么 import java.awt.*; import java.awt.event.*; import javax.imageio.ImageIO; import javax.swing.*; import java.lang.Object; publ

我有208个按钮,我想使用数组为它们分配数字。这些按钮上面有不同的图像,我希望当我把相应的数字放在屏幕上时,它们会出现在屏幕上,代表屏幕上的按钮。可以忽略二维阵列。我知道我必须去掉这段代码,为数组编写代码来代替这段代码,但我不知道我必须做什么

import java.awt.*;
import java.awt.event.*;

import javax.imageio.ImageIO;
import javax.swing.*;

import java.lang.Object;

public class CBallMaze extends JFrame implements ActionListener
{
    private JButton Jbuttonoption1;
    private JButton Jbuttonoption2;
    private JButton Jbuttonoption3;
    private JButton Jbuttonblank1;
    private JButton Jbuttonup;
    private JButton Jbuttonblank2;
    private JButton Jbuttonleft;
    private JButton Jbuttonblank3;
    private JButton Jbuttonright;
    private JButton Jbuttonblank4;
    private JButton Jbuttondown;
    private JButton Jbuttonblank5;
    private JButton Jbuttonexit;
    private JButton Jbuttonact;
    private Icon iconAct;
    private JButton Jbuttonrun;
    private Icon iconRun;
    private JButton Jbuttonreset;
    private Icon iconReset;
    private Icon iconCompassnorth;
    private Icon iconCompasseast;
    private Icon iconCompasssouth;
    private Icon iconCompasswest;
    private JButton JButtoncompass;
    private JPanel JPanelSouth;
    private JPanel JPanelSouthButtons;
    private JPanel JPanelSouthSlider;
    private JPanel JPaneleast;
    private JPanel JPaneleastoptionbutton;
    private JPanel JPaneleastmiddle;
    private JPanel JPaneleastcompass;
    private JPanel JPaneleasttextfields; 
    private JTextField JTextField1;
    private JLabel JLabelTextField1;
    private JTextField JTextField2;
    private JLabel JLabelTextField2;
    private JTextField JTextField3;
    private JLabel JLabelTextField3;
    private JPanel JPanelnorth;
    private JButton[] JGridButton = new JButton[208];
    private JPanel JPanelnorthwest;
    private JMenuBar JMenuBar1;
    private JMenu scenarioMenu;
    private JMenu editMenu;
    private JMenu controlsMenu;
    private JMenu helpMenu;
    private JMenuItem exitItem;
    private JMenuItem fontItem;
    private JMenuItem foreColor;
    private JMenuItem backColor;
    private JMenuItem helpItem;
    private JMenuItem aboutItem;
    private JSlider JSlider1;
    private JTextField JSliderTextfield;
   /* private ImageIcon[] sandimage = new ImageIcon[1];
    private ImageIcon[] whiteimage = new ImageIcon[2];
    private ImageIcon[] goldenball = new ImageIcon[4];
    private ImageIcon[] sandstone = new ImageIcon[3];*/
    private Icon sandimage;
    private Icon whiteimage;
    private Icon goldenball;
    private Icon sandstone;
    private Icon greenfoot;

    private int nPosition = 15;

   /* int [][] map1 = {  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4},
                      {2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2},
                      {2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2},
                      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                      {2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2},
                      {2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2},
                      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                      {2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2},
                      {2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2},
                      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
                      {2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2},
                      {2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2},
                      {3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} };*/

    int [] map1 = {  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
                     2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
                     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                     2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
                     3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };

    public static void main(String[] args)
    {

        CBallMaze frame = new CBallMaze();
        frame.setIconImage(Toolkit.getDefaultToolkit().getImage("res/greenfoot.jpg"));
        frame.setTitle("CBallMaze");
        frame.setSize(775, 650);
        frame.createGUI();
        frame.setVisible(true);
        JMenuBar menuBar1 = new JMenuBar();

        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e)
        {
            System.err.println("Couldn't use the system "+"look and feel: "+e);
        }

        JMenu fileMenu = new JMenu("Scenario");
        fileMenu.setMnemonic(KeyEvent.VK_F);
        menuBar1.add(fileMenu);

        JMenu fileMenu0 = new JMenu("Edit");
        fileMenu0.setMnemonic(KeyEvent.VK_F);
        menuBar1.add(fileMenu0);

        JMenu fileMenu1 = new JMenu("Controls");
        fileMenu1.setMnemonic(KeyEvent.VK_F);
        menuBar1.add(fileMenu1);

        JMenu fileMenu2 = new JMenu("Help");
        fileMenu2.setMnemonic(KeyEvent.VK_F);
        menuBar1.add(fileMenu2);

        frame.setJMenuBar(menuBar1);
        frame.setSize(775,650);
        frame.setVisible(true);


    }


    private void createGUI()
    {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        Container window = getContentPane();
        window.setLayout(new BorderLayout() );
        centreWindow();

        JPanelnorth = new JPanel();
        JPanelnorth.setPreferredSize(new Dimension(630, 600));
        JPanelnorth.setBackground(Color.white);
        JPanelnorth.setLayout(new GridLayout(13,16));
        window.add(JPanelnorth,BorderLayout.CENTER);

        try 
        {
            iconAct = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/Act.jpg")));
            iconRun = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/Run.jpg")));
            iconReset = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/Reset.jpg")));
            iconCompassnorth = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/north.jpg")));
            iconCompasseast = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/east.jpg")));
            iconCompasssouth = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/south.jpg")));
            iconCompasswest = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/west.jpg")));
            sandimage = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/sand.jpg")));
            whiteimage = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/white32x32.jpg")));
            goldenball = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/sand60x60.png")));
            sandstone = new ImageIcon(Toolkit.getDefaultToolkit().createImage(CBallMaze.class.getResource("images/sandstone.jpg")));
        }
        catch (Exception e)
        {
            System.err.println("Baby Icon ImageIcon "+e);
        } 


        for( int nCount= 0; nCount < 208; nCount++ )
        {
            JGridButton[nCount] = new JButton(""); 
            JPanelnorth.add(JGridButton[nCount]);
            JGridButton[nCount].setBorderPainted(false);
            if((nCount >= 0 && nCount<=15) 
                    || (nCount >= 48 && nCount <= 63)
                    || (nCount >= 96 && nCount <= 111)
                    || (nCount >= 144 && nCount <= 159)
                    || (nCount >= 193 && nCount <= 208)
                    || (nCount == 17) || (nCount == 33)
                    || (nCount == 21) || (nCount == 37)
                    || (nCount == 25) || (nCount == 41)
                    || (nCount == 66) || (nCount == 82)
                    || (nCount == 70) || (nCount == 86)
                    || (nCount == 75) || (nCount == 91)
                    || (nCount == 113) || (nCount == 129)
                    || (nCount == 117) || (nCount == 133)
                    || (nCount == 124) || (nCount == 140)
                    || (nCount == 162) || (nCount == 178)
                    || (nCount == 166) || (nCount == 182)

                    )
            {
                JGridButton[nCount].setIcon(sandimage);
            }
            else 
            {
                JGridButton[nCount].setBackground(Color.white);
            }
            if(nCount == 192)
            {
                JGridButton[nCount].setIcon(sandstone);
            }
            if(nCount == 15)
            {
                JGridButton[nCount].setIcon(goldenball);
            }

        }


        JPaneleast = new JPanel();
        JPaneleast.setPreferredSize(new Dimension(145, 600));
        JPaneleast.setBackground(Color.lightGray);
        window.add(JPaneleast,BorderLayout.EAST);

        JPanelSouth = new JPanel();
        JPanelSouth.setPreferredSize(new Dimension(705, 50));
        JPanelSouth.setBackground(Color.lightGray);
        window.add(JPanelSouth,BorderLayout.SOUTH);


        JPaneleasttextfields = new JPanel();
        JPaneleasttextfields.setPreferredSize(new Dimension(145,100));
        JPaneleasttextfields.setBackground(Color.lightGray);
        JPaneleasttextfields.setLayout(new GridLayout(3,2));
        JPaneleast.add(JPaneleasttextfields);

        JLabelTextField1 = new JLabel("Option:");
        JLabelTextField1.setPreferredSize(new Dimension(65,33));
        JPaneleasttextfields.add(JLabelTextField1);

        JTextField1 = new JTextField();
        JTextField1.setPreferredSize(new Dimension(80 ,33));
        JPaneleasttextfields.add(JTextField1);

        JLabelTextField2 = new JLabel("Square:");
        JLabelTextField2.setPreferredSize(new Dimension(65,33));
        JPaneleasttextfields.add(JLabelTextField2);

        JTextField2 = new JTextField();
        JTextField2.setPreferredSize(new Dimension(80 ,33));
        JPaneleasttextfields.add(JTextField2);

        JLabelTextField3 = new JLabel("Direction:");
        JLabelTextField3.setPreferredSize(new Dimension(65,33));
        JPaneleasttextfields.add(JLabelTextField3);

        JTextField3 = new JTextField();
        JTextField3.setPreferredSize(new Dimension(80 ,33));
        JPaneleasttextfields.add(JTextField3);

        JPaneleastmiddle = new JPanel();
        JPaneleastmiddle.setPreferredSize(new Dimension(160, 100));
        JPaneleastmiddle.setBackground(Color.gray);
        JPaneleastmiddle.setLayout(new GridLayout(3,3) );
        window.add(JPaneleastmiddle,BorderLayout.NORTH);
        JPaneleast.add(JPaneleastmiddle); 

        Jbuttonblank1 = new JButton("");
        JPaneleastmiddle.add(Jbuttonblank1);
        Jbuttonblank1.addActionListener(this);
        Jbuttonblank1.setPreferredSize(new Dimension(80, 30));
        Jbuttonblank1.setEnabled(false);

        Jbuttonup = new JButton("^");
        JPaneleastmiddle.add(Jbuttonup);
        Jbuttonup.addActionListener(this);
        Jbuttonup.setPreferredSize(new Dimension(80, 30));

        Jbuttonblank2 = new JButton("");
        JPaneleastmiddle.add(Jbuttonblank2);
        Jbuttonblank2.addActionListener(this);
        Jbuttonblank2.setPreferredSize(new Dimension(80, 30));
        Jbuttonblank2.setEnabled(false);

        Jbuttonleft = new JButton("<");
        JPaneleastmiddle.add(Jbuttonleft);
        Jbuttonleft.addActionListener(this);
        Jbuttonleft.setPreferredSize(new Dimension(80, 30));

        Jbuttonblank3 = new JButton("");
        JPaneleastmiddle.add(Jbuttonblank3);
        Jbuttonblank3.addActionListener(this);
        Jbuttonblank3.setPreferredSize(new Dimension(80, 30));
        Jbuttonblank3.setEnabled(false);

        Jbuttonright = new JButton(">");
        JPaneleastmiddle.add(Jbuttonright);
        Jbuttonright.addActionListener(this);
        Jbuttonright.setPreferredSize(new Dimension(80, 30));

        Jbuttonblank4 = new JButton("");
        JPaneleastmiddle.add(Jbuttonblank4);
        Jbuttonblank4.addActionListener(this);
        Jbuttonblank4.setPreferredSize(new Dimension(80, 30));
        Jbuttonblank4.setEnabled(false);

        Jbuttondown = new JButton("v");
        JPaneleastmiddle.add(Jbuttondown);
        Jbuttondown.addActionListener(this);
        Jbuttondown.setPreferredSize(new Dimension(80, 30));

        Jbuttonblank5 = new JButton("");
        JPaneleastmiddle.add(Jbuttonblank5);
        Jbuttonblank5.addActionListener(this);
        Jbuttonblank5.setPreferredSize(new Dimension(80, 30));
        Jbuttonblank5.setEnabled(false);

        JPaneleastoptionbutton = new JPanel();
        JPaneleastoptionbutton.setPreferredSize(new Dimension(160, 60));
        JPaneleastoptionbutton.setBackground(Color.gray);
        JPaneleastoptionbutton.setLayout(new GridLayout(2,2) );
        JPaneleast.add(JPaneleastoptionbutton);

        Jbuttonoption1 = new JButton("Option1");
        JPaneleastoptionbutton.add(Jbuttonoption1);
        Jbuttonoption1.addActionListener(this);
        Jbuttonoption1.setPreferredSize(new Dimension(80, 30));

        Jbuttonoption2 = new JButton("Option2");
        JPaneleastoptionbutton.add(Jbuttonoption2);
        Jbuttonoption2.addActionListener(this);
        Jbuttonoption2.setPreferredSize(new Dimension(80, 30));

        Jbuttonoption3 = new JButton("Option3");
        JPaneleastoptionbutton.add(Jbuttonoption3);
        Jbuttonoption3.addActionListener(this);
        Jbuttonoption3.setPreferredSize(new Dimension(80, 30));

        Jbuttonexit = new JButton("Exit");
        JPaneleastoptionbutton.add(Jbuttonexit);
        Jbuttonexit.addActionListener(this);
        Jbuttonexit.setPreferredSize(new Dimension(80, 30));

        JPaneleastcompass = new JPanel();
        JPaneleastcompass.setPreferredSize(new Dimension(100,100));
        JPaneleastcompass.setBackground(Color.lightGray);
        JPaneleast.add(JPaneleastcompass);


        JPanelSouthButtons = new JPanel();
        JPanelSouthButtons.setPreferredSize(new Dimension(270, 30));
        JPanelSouthButtons.setBackground(Color.gray);
        JPanelSouthButtons.setLayout(new GridLayout(1,3) );
        JPanelSouth.add(JPanelSouthButtons);

        Jbuttonact = new JButton("Act");
        JPanelSouthButtons.add(Jbuttonact);
        Jbuttonact.setIcon(iconAct);
        Jbuttonact.addActionListener(this);
        Jbuttonact.setPreferredSize(new Dimension(40, 30));

        Jbuttonrun = new JButton("Run");
        JPanelSouthButtons.add(Jbuttonrun);
        Jbuttonrun.setIcon(iconRun);
        Jbuttonrun.addActionListener(this);
        Jbuttonrun.setPreferredSize(new Dimension(40, 30));


        Jbuttonreset = new JButton("Reset");
        JPanelSouthButtons.add(Jbuttonreset);
        Jbuttonreset.setIcon(iconReset);
        Jbuttonreset.addActionListener(this);
        Jbuttonreset.setPreferredSize(new Dimension(40, 30));


        JPanelSouthSlider = new JPanel();
        JPanelSouthSlider.setPreferredSize(new Dimension(200, 30));
        JPanelSouthSlider.setBackground(Color.BLUE);
        JPanelSouthSlider.setLayout(new GridLayout(1,3));
        JPanelSouth.add(JPanelSouthSlider);

        JSlider1 = new JSlider(JSlider.HORIZONTAL,200,2000,1000);
        JSlider1.setPreferredSize(new Dimension(200, 30));
        JPanelSouthSlider.add(JSlider1);

        JButtoncompass = new JButton();
        JPaneleastcompass.add(JButtoncompass);
        JButtoncompass.setPreferredSize(new Dimension(100,100));
        JButtoncompass.setIcon(iconCompassnorth);


    }

    public void centreWindow()
    {    
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      Dimension frameSize = getSize();
      if (frameSize.height > screenSize.height)
      {
        frameSize.height = screenSize.height;
      }
      if (frameSize.width > screenSize.width)
      {
        frameSize.width = screenSize.width;
      }
      setLocation((screenSize.width - frameSize.width) / 2, 
                  (screenSize.height - frameSize.height) / 2);
     }

    public void gameEnd()
    {
        if(nPosition == 193)
        {   

        }
        else
        {

        }
    }

    public void Option1()
    {
        JTextField1.setText("Option1");
    }

    public void Option2()
    {
        JTextField1.setText("Option2");
    }

    public void Option3()
    {
        JTextField1.setText("Option3");
    }
    public void actionPerformed(ActionEvent event)
    {
        Object source = event.getSource();
        gameEnd();

        if(source == Jbuttonoption1)
        {
            Option1();
        }

        if(source == Jbuttonoption2)
        {
            Option2();
        }

        if(source == Jbuttonoption3)
        {
            Option3();
        }

        if(source == Jbuttonup)
        {
            JButtoncompass.setIcon(iconCompassnorth);
            JTextField3.setText("N");
            JGridButton[nPosition -16].setIcon(goldenball);
            JGridButton[nPosition].setIcon(sandimage);
            //JTextField2.set
            nPosition = nPosition -16;
        }

        if(source == Jbuttonright)
        {
            JButtoncompass.setIcon(iconCompasseast);
            JTextField3.setText("E");
            JGridButton[nPosition +1].setIcon(goldenball);
            JGridButton[nPosition].setIcon(sandimage);
            nPosition = nPosition +1;
        }

        if(source == Jbuttondown)
        {
            JButtoncompass.setIcon(iconCompasssouth);
            JTextField3.setText("S");
            JGridButton[nPosition +16].setIcon(goldenball);
            JGridButton[nPosition].setIcon(sandimage);
            nPosition = nPosition +16;
        }

        if(source == Jbuttonleft)
        {
            if(nPosition <= 0)
            {

            }
            else{
            JButtoncompass.setIcon(iconCompasswest);
            JTextField3.setText("W");
            JGridButton[nPosition -1].setIcon(goldenball);
            JGridButton[nPosition].setIcon(sandimage);
            nPosition = nPosition -1;}

        }

        if(source == Jbuttonoption1)
        {

        }

        if(source == Jbuttonoption2)
        {

        }

        if(source == Jbuttonoption3)
        {

        }

        if(source == Jbuttonexit)
        {
            System.exit(EXIT_ON_CLOSE);
        }
    }
}





for( int nCount= 0; nCount < 208; nCount++ )
        {
            JGridButton[nCount] = new JButton(""); 
            JPanelnorth.add(JGridButton[nCount]);
            JGridButton[nCount].setBorderPainted(false);
            if((nCount >= 0 && nCount<=15) 
                    || (nCount >= 48 && nCount <= 63)
                    || (nCount >= 96 && nCount <= 111)
                    || (nCount >= 144 && nCount <= 159)
                    || (nCount >= 193 && nCount <= 208)
                    || (nCount == 17) || (nCount == 33)
                    || (nCount == 21) || (nCount == 37)
                    || (nCount == 25) || (nCount == 41)
                    || (nCount == 66) || (nCount == 82)
                    || (nCount == 70) || (nCount == 86)
                    || (nCount == 75) || (nCount == 91)
                    || (nCount == 113) || (nCount == 129)
                    || (nCount == 117) || (nCount == 133)
                    || (nCount == 124) || (nCount == 140)
                    || (nCount == 162) || (nCount == 178)
                    || (nCount == 166) || (nCount == 182)

                    )
            {
                JGridButton[nCount].setIcon(sandimage);
            }
            else 
            {
                JGridButton[nCount].setBackground(Color.white);
            }
            if(nCount == 192)
            {
                JGridButton[nCount].setIcon(sandstone);
            }
            if(nCount == 15)
            {
                JGridButton[nCount].setIcon(goldenball);
            }

        }
import java.awt.*;
导入java.awt.event.*;
导入javax.imageio.imageio;
导入javax.swing.*;
导入java.lang.Object;
公共类CBallMaze扩展JFrame实现ActionListener
{
私有JButton Jbuttonoption1;
私有JButton Jbuttonoption2;
私有JButton Jbuttonoption3;
私有JButton Jbuttonblank1;
私有JButton Jbuttonup;
私有JButton Jbuttonblank2;
私有JButton Jbuttonleft;
私有JButton Jbuttonblank3;
私有JButton Jbuttonright;
私有JButton Jbuttonblank4;
私人JButton jbuttonown;
私有JButton Jbuttonblank5;
私有JButton Jbuttonexit;
私有JButton Jbuttonact;
私人图标图标;
私有JButton Jbuttonrun;
私人图标图标图标;
私有JButton Jbuttonreset;
私人图标图标;
私人图标iconCompassnorth;
私人图标图标比较;
私人图标iconCompasssouth;
私人图标iconCompasswest;
私人JButton JButtoncompass;
私人JPanel JPanelSouth;
私人JPanel JPanelSouthButtons;
私人JPanel JPanelSouthSlider;
私人JPanel JPANELST;
私有JPanel JPaneleastoption按钮;
私人JPanel JPANELEASTMIDLE;
私人JPanel JPanelessCompass;
私有JPanel JPaneleasttextfields;
专用JTextField JTextField1;
专用JLabel JLabelTextField1;
专用JTextField JTextField2;
专用JLabel JLabelTextField2;
私有JTextField JTextField3;
专用JLabel JLabelTextField3;
私人JPanel JPanelnorth;
私有JButton[]JGridButton=新JButton[208];
私人JPanel JPanelnorthwest;
私人JMenuBar JMenuBar1;
私有JMenu场景菜单;
私有JMenu编辑菜单;
私人JMenu控制菜单;
私人JMenu帮助菜单;
私人物品出口;
私人物品;
私人项目前景色;
私人物品背景色;
私人项目帮助项目;
私人物品;
私人JSlider JSlider1;
私有JTextField-JSliderTextfield;
/*私有图像图标[]sandimage=新图像图标[1];
私有图像图标[]白色图像=新图像图标[2];
私有ImageIcon[]goldenball=新ImageIcon[4];
私有图像图标[]砂岩=新图像图标[3]*/
私人图标沙盘图像;
私人图标白色图像;
私人偶像戈登堡;
私人标志砂岩;
私人标志绿足;
私人位置=15;
/*int[]map1={{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4},
{2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2},
{2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2},
{2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2},
{2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2},
{3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} };*/
int[]map1={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,
2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
2, 1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2,
2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 1, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
公共静态void main(字符串[]args)
{
CBallMaze帧=新的CBallMaze();
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(“res/greenfoot.jpg”);
框架。设置标题(“CBallMaze”);
框架设置尺寸(775650);
frame.createGUI();
frame.setVisible(true);
JMenuBar menuBar1=新的JMenuBar();
尝试
{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
捕获(例外e)
{
System.err.println(“无法使用系统”+“外观:”+e);
}
JMenu fileMenu=新JMenu(“场景”);
fileMenu.setMnemonic(KeyEvent.VK_F);
menuBar1.add(文件菜单);
JMenu fileMenu0=新JMenu(“编辑”);
fileMenu0.setMnemonic(KeyEvent.VK_F);
menuBar1.add(fileMenu0);
JMenu fileMenu1=新JMenu(“控件”);
fileMenu1.setMnemonic(KeyEvent.VK_F);
menuBar1.add(fileMenu1);
JMenu fileMenu2=新JMenu(“帮助”);
fileMenu2.set助记符(KeyEvent.VK_F);
menuBar1.add(fileMenu2);
frame.setJMenuBar(menuBar1);
框架设置尺寸(775650);
frame.setVisible(true);
}
私有void createGUI()
{
setDefaultCloseOperation(
if((nCount >= 0 && nCount<=15) 
                        || (nCount >= 48 && nCount <= 63)
                        || (nCount >= 96 && nCount <= 111)
                        || (nCount >= 144 && nCount <= 159)
                        || (nCount >= 193 && nCount <= 208)
                        || (nCount == 17) || (nCount == 33)
                        || (nCount == 21) || (nCount == 37)
                        || (nCount == 25) || (nCount == 41)
                        || (nCount == 66) || (nCount == 82)
                        || (nCount == 70) || (nCount == 86)
                        || (nCount == 75) || (nCount == 91)
                        || (nCount == 113) || (nCount == 129)
                        || (nCount == 117) || (nCount == 133)
                        || (nCount == 124) || (nCount == 140)
                        || (nCount == 162) || (nCount == 178)
                        || (nCount == 166) || (nCount == 182)

                        )
  // first read in the text file and fill the map 2D int array

  for (int row = 0; row < btnGrid.length; row++) {
     for (int col = 0; col < btnGrid[row].length; col++) {
        int value = map[row][col]; // holds value for icons array
        Icon icon = icons[value]; // an array of ImageIcons
        btnGrid[row][col] = new JButton(icon);
        btnGrid[row][col].addActionListener(someListener);
        btnPanel.add(btnGrid[row][col]);
     }
  }