我怎样才能找到按钮的来源并使其工作? 打包游戏; 导入java.awt.event.ActionEvent; 导入java.awt.event.ActionListener; 导入javax.swing.JButton; 导入javax.swing.JOptionPane; 公共接口侦听器 { 静态模式游戏=新模式游戏(); InputGame game2=新的InputGame(); 静态类inst实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(“inst”.equals(例如getActionCommand())) { } } } 静态类播放实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(“play”.equals(例如getActionCommand())) { menu.mf.dispose(); PatternGame.gameStart(); } } } 静态类出口实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(“exit”.equals(例如getActionCommand())) { 系统出口(0); } } } 静态类输入实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(InputGame.numoClicks!=0) { InputGame.button=(JButton)e.getSource(); InputGame.button.setText(“X”); InputGame.numof--; } 其他的 { if(InputGame.checkCorrect()) { showConfirmDialog(null,“您想要另一种模式吗?”); PatternGame.order++; PatternGame.gameStart(); } 其他的 { showMessageDialog(null,“不正确!”); menu.start(); } } } } } 包装游戏; 导入java.awt.Dimension; 导入java.awt.GridLayout; 导入javax.swing.JButton; 导入javax.swing.JFrame; 公共类InputGame实现属性、侦听器{ 公共静态JFrame gf=新JFrame(); 公共静态JButton按钮=新JButton(); 公共静态内部高度=800; 公共静态整数宽度=600; 公共静态int gsize=4; 公共静态整数阶=1; 公共静态尺寸=新尺寸(高度、宽度); 公共静态菜单=新菜单(); 公共静态GridLayout Ggrid=新的GridLayout(gsize,gsize); 公共静态int numOfClicks=0; 公共静态int numCorrect=0; 公共静态int[][]输入=新int[][]{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; //公共静态线程d; 公共静态无效设置(){ gf.dispose(); gf=新的JFrame(); gf.设置位置(300100); gf.设置尺寸(尺寸); gf.可设置大小(假); gf.setLayout(Ggrid); gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 模式游戏。清除(); 空白(); gf.setVisible(真); 系统输出打印(NUMOF); } 公共静态void blank() { 对于(int a=0;a
您可以在父容器上调用getComponents(),无论是面板、框架还是窗口,然后检查它是否是button的实例,然后检查其上的文本集,如下所示:我怎样才能找到按钮的来源并使其工作? 打包游戏; 导入java.awt.event.ActionEvent; 导入java.awt.event.ActionListener; 导入javax.swing.JButton; 导入javax.swing.JOptionPane; 公共接口侦听器 { 静态模式游戏=新模式游戏(); InputGame game2=新的InputGame(); 静态类inst实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(“inst”.equals(例如getActionCommand())) { } } } 静态类播放实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(“play”.equals(例如getActionCommand())) { menu.mf.dispose(); PatternGame.gameStart(); } } } 静态类出口实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(“exit”.equals(例如getActionCommand())) { 系统出口(0); } } } 静态类输入实现ActionListener { 已执行的公共无效操作(操作事件e) { 如果(InputGame.numoClicks!=0) { InputGame.button=(JButton)e.getSource(); InputGame.button.setText(“X”); InputGame.numof--; } 其他的 { if(InputGame.checkCorrect()) { showConfirmDialog(null,“您想要另一种模式吗?”); PatternGame.order++; PatternGame.gameStart(); } 其他的 { showMessageDialog(null,“不正确!”); menu.start(); } } } } } 包装游戏; 导入java.awt.Dimension; 导入java.awt.GridLayout; 导入javax.swing.JButton; 导入javax.swing.JFrame; 公共类InputGame实现属性、侦听器{ 公共静态JFrame gf=新JFrame(); 公共静态JButton按钮=新JButton(); 公共静态内部高度=800; 公共静态整数宽度=600; 公共静态int gsize=4; 公共静态整数阶=1; 公共静态尺寸=新尺寸(高度、宽度); 公共静态菜单=新菜单(); 公共静态GridLayout Ggrid=新的GridLayout(gsize,gsize); 公共静态int numOfClicks=0; 公共静态int numCorrect=0; 公共静态int[][]输入=新int[][]{ {0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0} }; //公共静态线程d; 公共静态无效设置(){ gf.dispose(); gf=新的JFrame(); gf.设置位置(300100); gf.设置尺寸(尺寸); gf.可设置大小(假); gf.setLayout(Ggrid); gf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 模式游戏。清除(); 空白(); gf.setVisible(真); 系统输出打印(NUMOF); } 公共静态void blank() { 对于(int a=0;a,java,swing,jframe,jbutton,Java,Swing,Jframe,Jbutton,您可以在父容器上调用getComponents(),无论是面板、框架还是窗口,然后检查它是否是button的实例,然后检查其上的文本集,如下所示: package game; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; public interface Listener
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;
}
}
希望你明白
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
}
}