简体   繁体   English

如何获得按钮的来源并使它起作用?

[英]How can I get the source of the button and get this to work?

package game;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JOptionPane;


public interface Listeners 
{   
    static PatternGame game = new PatternGame();
    InputGame game2 = new InputGame();
    static class inst implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if ("inst".equals(e.getActionCommand()))
            {

            }
        }
    }
    static class play implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if ("play".equals(e.getActionCommand()))
            {
                menu.mf.dispose();
                PatternGame.gameStart();

            }
        }
    }
    static class exit implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if ("exit".equals(e.getActionCommand()))
            {
                System.exit(0);
            }
        }
    }
    static class input implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            if (InputGame.numOfClicks != 0)
            {
                InputGame.button = (JButton) e.getSource();
                InputGame.button.setText("X");
                InputGame.numOfClicks--;
            }
            else
            {
                if (InputGame.checkCorrect())
                {
                    JOptionPane.showConfirmDialog(null, "Would you like another pattern?");
                    PatternGame.order++;
                    PatternGame.gameStart();
                }
                else
                {
                    JOptionPane.showMessageDialog(null, "Incorrect!");
                    menu.start();
                }
            }

        }
    }
}
package game;

import java.awt.Dimension;
import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class InputGame implements Properties,Listeners{
    public static JFrame gf = new JFrame();
    public static JButton button = new JButton();
    public static int height = 800;
    public static int width = 600;
    public static int gsize = 4;
    public static int order =1;
    public static Dimension size = new Dimension(height,width);
    public static menu Menu = new menu();
    public static GridLayout Ggrid = new GridLayout(gsize,gsize);
    public static int numOfClicks =0;
    public static int numCorrect=0;
    public static int[][] input = new int[][]{
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0},
        {0,0,0,0}

    };

    //public static Thread d;


    public static void setup() {
        gf.dispose();
        gf = new JFrame();
        gf.setLocation(300,100);
        gf.setSize(size);
        gf.setResizable(false);
        gf.setLayout(Ggrid);
        gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        PatternGame.clear();
        blank();
        gf.setVisible(true);
        System.out.print(numOfClicks);

    }

    public static void blank()
    {
        for (int a =0;a<4;a++)
        {
            for (int b =0;b<4;b++)
            {
                button = new JButton("");
                button.setActionCommand("");
                button.addActionListener(new input());
                gf.add(button);
            }
        }
    }
    public static void input()
    {
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                String x = button.getText();

                if (x.equals("X"))
                {
                    input[a][b] = 1;
                }
                else if (x.equals(""))
                {
                    input[a][b] = 0;
                }
                System.out.println(input[a][b]);

            }
        }
    }
    public static boolean checkCorrect()
    {
        input();
        for (int a=0;a<4;a++)
        {
            for(int b=0;b<4;b++)
            {
                if (order == 1)
                {
                    if (handlebars[a][b] == 1)
                    {
                        JButton button = new JButton("X");
                        InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                    }
                }
                if (order == 2)
                {
                    if (ys[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                    }
                }
                if (order == 3)
                {
                    if (spaceShip[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                    }
                }
                if (order == 4)
                {
                    if (flock[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                    }
                }
                if (order == 5)
                {
                    if (percent[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                    }
                }
                if (order == 6)
                {
                    if (square[a][b] == 1)
                    {
                    JButton button = new JButton("X");
                    InputGame.numOfClicks++;

                    }
                    else
                    {
                        JButton button = new JButton("");
                    }
                }
            }
        }
        return false;
    }


}

Ok, this is my code. 好的,这是我的代码。 Now what I want the program to accomplish is for the program to display the blank screen(which I have done) then when the user clicks one of the buttons and it places a X on that button. 现在,我希望程序完成的工作是让程序显示空白屏幕(我已经完成),然后当用户单击按钮之一并将其放置在该按钮上时,它会显示一个X。 Then after 6 clicks for the program to translate the pattern they put in to the input array, so I can compare the input array to the pattern array, and be able to tell the user if its correct or not and move on. 然后,单击6次以使程序将它们放入的模式转换为输入数组,这样我就可以将输入数组与模式数组进行比较,并能够告诉用户其正确与否并继续前进。 What this program does now is it displays the blank screen then when a button is pressed it displays the X. I got it to check for one button but I don't know how to check for all of them. 该程序现在要做的是显示空白屏幕,然后按下按钮时显示X。我让它检查一个按钮,但是我不知道如何检查所有按钮。 So how could I check each button for the text it holds. 因此,我该如何检查每个按钮的文本。

You can call getComponents() on the parent container- whether panel, frame or window and then check if it is an instance of button, then check the text set on it like this: 您可以在父容器(面板,框架或窗口)上调用getComponents(),然后检查它是否是button的实例,然后检查在其上设置的文本,如下所示:

Component[] comps = parent.getComponents();
for(Component c:comps){
    if(c instanceof JButton){
        JButton btn = (JButton) c;
        String text = btn.getText();
        //Use text for whatever, add it to an array or something
    }
}

Hope you get the idea 希望你明白

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM