有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java帮助了解按下了哪个按钮编号

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import java.util.Random;
/**
 *
 */
public class GUI
{
    private static final int BTN_MAX = 8;


    private JFrame frame;
    private JPanel panel;
    private JPanel scores;
    private JButton[] buttons;
    private ImageIcon[] icons;

    private Random rand;

    private ImageIcon beach;
    private ImageIcon lips;
    private ImageIcon discoball;
    private ImageIcon flowers;
    private ImageIcon blank;




    /**
     *
     */
    public GUI()
    {        
        beach = new ImageIcon("beach.jpg");
        lips = new ImageIcon("lips.jpg");
        discoball = new ImageIcon ("discoball.jpg");
        flowers = new ImageIcon ("flowers.jpg");
        blank = new ImageIcon ("blank.jpg");

        buttons = makeButtons();
        rand = new Random();
        startingCondition();

        icons = new ImageIcon[] { beach, lips, discoball, lips, beach, flowers, discoball, flowers};


        makeFrame();
        makeMenuBar(frame);        
        frame.pack();
        frame.setVisible(true);


    }

    /**
     * Makes the frame for the gui, inclusive of adding all components.
     */
    private void makeFrame()
    {
        int horizGap = 25; // Using this for spaces between the grid layout components
        int vertGap = 25;  // Using this for the spaces between the grid layout componeents

        frame = new JFrame("Noughts and Crosses");
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(4,2));//set layout of frame to BorderLayout

        for (int i = 0; i < BTN_MAX; i++){
            contentPane.add(buttons[i]);
        }
    }

    /**
     * 
     */
    private void makeMenuBar(JFrame frame)
    {             
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);

        JMenu menu;
        JMenuItem item;

        menu = new JMenu("File");
        menubar.add(menu);

        item = new JMenuItem("Reset Entire Game");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e)
               { 
               }
            });
        menu.add(item);

        item = new JMenuItem("Reset This Game");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e) 
               { 
               }
            });
        menu.add(item);    

        item = new JMenuItem("Quit");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e) 
               { 
               }
            });
        menu.add(item);            

        frame.addWindowListener(new WindowAdapter()
        {
            public void windowClosing(WindowEvent e)
            {

            }
        });  

        menu = new JMenu("About");
        menubar.add(menu); 

        item = new JMenuItem("About The Game");
            item.addActionListener(new ActionListener() 
            {
               public void actionPerformed(ActionEvent e) 
               { 
               }
            });
        menu.add(item);
    }    


    /**
     * 
     */
    private JButton[] makeButtons()
    {
        final JButton button[] = new JButton[8];
        for (int i = 0; i < BTN_MAX; i++)
        {
            button[i] = new JButton("");
            button[i].setPreferredSize(new Dimension(200, 100));
            final int tmp = i;
            button[i].addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent e)
                {  
                    checkForPair();
                    takeGo();
                }
            });
        }
        return button;
    }

    /**
     * 
     */
    private void startingCondition()
    {
        for (int i = 0; i < 8; i++){
            buttons[i].setIcon(blank);
        }
    }


    /**
     * 
     */
    public int returnButtonNumber()
    {
        return 0;
        // need to tell which button in the array has been clicked
        //so i can send a value to the takeGo method
        //appropriatley
    }  

    public ImageIcon getIcon(int number)
    {
        return icons[number];
    }

    /**
     * 
     */
    public boolean checkForPair()
    {
        return false;
    }

    /**
     * 
     */
    public void takeGo()
    {
        int i = returnButtonNumber();

        buttons[i].setIcon(getIcon(i));
    }
}

您好,我想知道是否有人可以帮助我确定按下了哪个按钮编号,然后我可以将此值发送到另一个方法,以显示适当的图像,欢迎所有想法,但请您尽量避免完整的代码,因为我想尝试自己做,举个例子就好了

谢谢!


共 (3) 个答案

  1. # 1 楼答案

    为每个不同的操作使用不同的ActionListener实例。它很可能是同一个类,可能是用同一个参数化方法与其他东西构造的

    例如:

        addItem("Quit", quitCommand);
    ...
    private void addItem(String text, final Runnable command) {
        JMenuItem item = new JMenuItem(text);
        item.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) { 
                command.run();
            }
        });
        menu.add(item);  
    }
    
  2. # 2 楼答案

    你可以使用ActionEvent。getSource()与JButton[]按钮结合使用,以查找触发该操作的按钮的索引。我希望这足够神秘:)

  3. # 3 楼答案

    为了给你尽可能少的信息;-)。。。试着看看JButton。setActionCommand()

    此外,如果要对每个按钮使用相同的ActionListener,并检查哪个按钮被按下,则只需实例化ActionListener一次,并将其添加到每个按钮,而不是为每个按钮创建一个