【问题标题】:Beginner: JButton/Gridlayout (Similar to Minesweeper)初学者:JButton/Gridlayout(类似于扫雷)
【发布时间】:2013-04-06 05:48:20
【问题描述】:

我是 Java 初学者,最终我想为我正在为高级项目制作的机器人创建代码。我计划让机器人以指定的模式建立多米诺骨牌,然后将其击倒。我首先需要编写一个程序,允许我选择要放置在网格上的多米诺骨牌。然后我计划让程序为 Arduino 打印一个新程序。

作为测试和学习,我想用 JButtons 制作一个 20x40 的网格。然后,我想单击几个 Jbutton,然后将 Jbutton 值添加到新数组中。前任。我单击第 1、5、30 和 799 个按钮。然后程序会将这些添加到一个新数组中,其中array[0]=1array[2]=5; 等。

我花了很多时间反复试验并在网上寻找以下代码: 现在的问题是它似乎正在跳过 Buttongrid 方法(?)。如果我将方法设为 public static void main (String [] args){,那么动作监听器就不起作用了。

再说一次,我才刚刚开始,所以如果有很多事情是错误的,我不会感到惊讶。 请看看它并帮助我找出我必须解决的问题。 谢谢

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class ButtonGrid extends JFrame implements ActionListener  {
static int clicked[]=new int[800];  
static JButton button[]=new JButton[800];
static int x;
static int count=0;
int value;
ActionListener listen;
    public  ButtonGrid() { 
        JFrame frame= new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        GridLayout grid=new GridLayout(20,40);
        frame.setLayout(grid);
    //fill clicked with 0s////////////////////////////////////
            for (int c=0;c<=10;c++){
        clicked[c]=0;}
            for(x=0;x<800;x++){  
           button[x]= new JButton();
           button[x].setActionCommand(Integer.toString(x));
           frame.add(button[x]);
           button[x].addActionListener(this);
            }}
            public void actionPerformed (ActionEvent e){


            while(count<11){
                int newvalue=value;
                 value=Integer.parseInt( e.getActionCommand());  
                 if(value!=newvalue){
                    clicked[count]=this.value;
                    count=count+1;
                    System.out.println("Found");
                 }
                 else{
                     newvalue=value;
                     System.out.println("Looking...");}}
            }   public static void main(String [] args){
                        ButtonGrid b=new ButtonGrid();
                        if (count>10){
                        for (int t=0;t<=11;t++){
                            System.out.println(clicked[t]);
                                }
                            }
                        }
            }

【问题讨论】:

  • 你的实际源代码是这样格式化的吗?那会让我头疼...

标签: java swing jbutton grid-layout minesweeper


【解决方案1】:

这是因为你没有在主类中调用你的 ButtonGrid 框架,像这样:

 public static void main(String[] args) {
        System.out.println("this prints and no window pops up. it skipped       the grid/frame code");
        ButtonGrid b = new ButtonGrid();
    }

【讨论】:

  • 非常感谢。做到了。对此,我真的非常感激。我的新问题是 while 循环的结构,它将前 10 个单击的按钮值添加到 int 数组 clicked[] 然后被打印。循环卡在 else/listening... 区域,永远不会让我有机会单击另一个按钮。我更新了上面的代码。
  • 我不知道你想在你的 while 循环中做什么,但很明显你的代码将永远循环,因为:1-int newvalue = value; 2-if (value != newvalue) 这将是false永远来自等式1,因此它永远不会执行,并将转到else 语句newvalue = value;,其中您没有更改count,所以它将是=0
  • 这是你卡住的地方,所以请准确告诉我你想使用的算法:)
  • 这就是我想要发生的事情。首先该值变为新值,因此循环重复。然后我想如果我按下一个按钮,value 会突然变成 32。然后 value 将不等于 newvalue 并且该值将被添加到数组 clicked[0] 因为 count = 0。然后计数变为 1。如果我在循环中按下另一个按钮。说 330,然后单击 [1]=330,因为 count =1 且 value=330。如果我之后不按任何内容, value = newvalue 所以它会跳过。我现在看到循环是如此之快,我必须单击第 1 行和第 3 行之间的按钮才能工作。有什么建议吗?
  • 所以你需要在 Thread 内创建这个循环,线程将允许你改变值,因为在循环内值永远不会改变。
猜你喜欢
  • 2019-06-05
  • 1970-01-01
  • 2013-05-19
  • 1970-01-01
  • 2021-04-06
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多