如何获取按钮的来源并使其工作?

时间:2013-12-22 01:09:51

标签: java swing jframe jbutton

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


}

好的,这是我的代码。现在我想要程序完成的是程序显示空白屏幕(我已经完成)然后当用户点击其中一个按钮并在该按钮上放置一个X.然后在程序中点击6次后将它们放入输入数组的模式进行转换,这样我就可以将输入数组与模式数组进行比较,并能够告诉用户其是否正确并继续前进。这个程序现在做的是它显示空白屏幕,然后当按下按钮时它显示X.我得到它检查一个按钮,但我不知道如何检查所有这些。那么我怎样才能检查每个按钮的文本。

1 个答案:

答案 0 :(得分:2)

您可以在父容器上调用getComponents() - 无论是面板,框架还是窗口,然后检查它是否是按钮的实例,然后像这样检查其上的文本集:

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

希望你明白这个想法