向数组添加按钮

时间:2015-03-25 15:17:28

标签: java arrays swing

我有208个按钮,我想通过使用数组为它们分配数字。这些按钮在它们上面有不同的图像,当我输入相应的数字时,我希望它们出现在屏幕上,这代表屏幕上的按钮。可以忽略2D阵列。我知道我必须摆脱这个代码并为这个代码编写代码,但我无法弄清楚我必须做什么。

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);
            }

        }

我必须删除此代码,我认为并将其替换为该阵列的其他代码。

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)

                        )

1 个答案:

答案 0 :(得分:1)

看起来您想要创建按钮所包含的图像网格。如果是这样,那么只需创建你的JButton二维数组,不要给出按钮名称,并在for循环中创建你的JButtons,同时将它们添加到数组和GridLayout中 - 使用JPanel。然后,您可以使用相同的网格为每个按钮分配ImageIcons。

您的地图应该由文本文件保存,而不是在代码中进行硬编码,然后您可以将地图读取到自己的2D数组中,并将其用作创建和迭代按钮网格的基础。< / p>

如,

  // 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]);
     }
  }

在JButton的Action或ActionListener(此处记为someListener)中,您可以根据需要在按下按钮时更改按钮的状态,可能是其图标。< / p>