作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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。我让它检查一个按钮,但我不知道如何检查所有按钮。那么我如何检查每个按钮中包含的文本。
最佳答案
您可以在父容器上调用 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
}
}
希望你明白
关于java - 我怎样才能获得按钮的来源并让它发挥作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20725180/
在 Play 1.x 中有很棒的 play Idealize(和 play eclipsify),它为您最喜欢的 IDE 中的 Play 项目准备了项目文件。 我看到这是在 Play 2.X 中删除的
我是一名优秀的程序员,十分优秀!