java吧 关注:1,237,776贴子:12,707,595
  • 7回复贴,共1

【求助】一时兴起想写个2048,但是遇到了个BUG

取消只看楼主收藏回复

二楼放代码,气死我了,两天了


IP属地:湖南1楼2017-04-24 12:36回复
    public class Jframe extends JFrame implements KeyListener {
    JButton[][] **s = new JButton[4][4]; // 定义二维数组来存储行列值
    ArrayList<JButton> als = new ArrayList<JButton>(); // 创建一个集合来存储16个按钮
    int i = 16; // 剩余方块数
    int score = 0; // 分数
    int num = 0; // 分数的一半
    int l1, l2, l3, l4; // 定义标记
    Random random = new Random(); // 定义一个随机数
    private JPanel contentPane;
    JButton button0;
    JButton button1;
    JButton button2;
    JButton button3;
    JButton button4;
    JButton button5;
    JButton button6;
    JButton button7;
    JButton button8;
    JButton button9;
    JButton button10;
    JButton button11;
    JButton button12;
    JButton button13;
    JButton button14;
    JButton button15;
    private JTextField textField;
    JLabel lblTips;
    /**
    * Launch the application.
    */
    public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
    public void run() {
    try {
    Jframe frame = new Jframe();
    frame.setVisible(true);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });
    }
    /**
    * 窗口布局
    */
    public Jframe() {
    setTitle("2048");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 500, 550);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);
    setResizable(false);
    JLabel label = new JLabel("\u5206\u6570");
    label.setBounds(348, 13, 30, 15);
    contentPane.add(label);
    button0 = new JButton("");
    button0.setFont(new Font("楷体", Font.PLAIN, 30));
    button0.setBounds(43, 57, 100, 100);
    contentPane.add(button0);
    button1 = new JButton("");
    button1.setFont(new Font("楷体", Font.PLAIN, 30));
    button1.setBounds(143, 57, 100, 100);
    contentPane.add(button1);
    button2 = new JButton("");
    button2.setFont(new Font("楷体", Font.PLAIN, 30));
    button2.setBounds(242, 57, 100, 100);
    contentPane.add(button2);
    button3 = new JButton("");
    button3.setFont(new Font("楷体", Font.PLAIN, 30));
    button3.setBounds(342, 57, 100, 100);
    contentPane.add(button3);
    button4 = new JButton("");
    button4.setFont(new Font("楷体", Font.PLAIN, 30));
    button4.setBounds(43, 158, 100, 100);
    contentPane.add(button4);
    button5 = new JButton("");
    button5.setFont(new Font("楷体", Font.PLAIN, 30));
    button5.setBounds(143, 158, 100, 100);
    contentPane.add(button5);
    button6 = new JButton("");
    button6.setFont(new Font("楷体", Font.PLAIN, 30));
    button6.setBounds(242, 158, 100, 100);
    contentPane.add(button6);
    button7 = new JButton("");
    button7.setFont(new Font("楷体", Font.PLAIN, 30));
    button7.setBounds(342, 158, 100, 100);
    contentPane.add(button7);
    button8 = new JButton("");
    button8.setFont(new Font("楷体", Font.PLAIN, 30));
    button8.setBounds(43, 259, 100, 100);
    contentPane.add(button8);
    button9 = new JButton("");
    button9.setFont(new Font("楷体", Font.PLAIN, 30));
    button9.setBounds(143, 259, 100, 100);
    contentPane.add(button9);
    button10 = new JButton("");
    button10.setFont(new Font("楷体", Font.PLAIN, 30));
    button10.setBounds(242, 259, 100, 100);
    contentPane.add(button10);
    button11 = new JButton("");
    button11.setFont(new Font("楷体", Font.PLAIN, 30));
    button11.setBounds(342, 259, 100, 100);
    contentPane.add(button11);
    button12 = new JButton("");
    button12.setFont(new Font("楷体", Font.PLAIN, 30));
    button12.setBounds(43, 360, 100, 100);
    contentPane.add(button12);
    button13 = new JButton("");
    button13.setFont(new Font("楷体", Font.PLAIN, 30));
    button13.setBounds(143, 360, 100, 100);
    contentPane.add(button13);
    button14 = new JButton("");
    button14.setFont(new Font("楷体", Font.PLAIN, 30));
    button14.setBounds(242, 360, 100, 100);
    contentPane.add(button14);
    button15 = new JButton("");
    button15.setFont(new Font("楷体", Font.PLAIN, 30));
    button15.setBounds(342, 360, 100, 100);
    contentPane.add(button15);
    textField = new JTextField();
    textField.setEditable(false);
    textField.setFont(new Font("楷体", Font.PLAIN, 15));
    textField.setText("0");
    textField.setBounds(376, 10, 66, 21);
    contentPane.add(textField);
    textField.setColumns(10);
    lblTips = new JLabel("Tips:\u6309\u4E0A\u3001\u4E0B\u3001\u5DE6\u3001\u53F3\u8FDB\u884C\u6E38\u620F");
    lblTips.setBounds(141, 470, 201, 15);
    contentPane.add(lblTips);
    this.Store();
    this.Randomizer();
    this.Randomizer();
    textField.addKeyListener(this); // 注册键盘监听事件
    }


    IP属地:湖南3楼2017-04-24 12:37
    回复
      public void Store() { // 将16个按钮存储到集合
      als.add(button0);
      als.add(button1);
      als.add(button2);
      als.add(button3);
      als.add(button4);
      als.add(button5);
      als.add(button6);
      als.add(button7);
      als.add(button8);
      als.add(button9);
      als.add(button10);
      als.add(button11);
      als.add(button12);
      als.add(button13);
      als.add(button14);
      als.add(button15);
      for (int i = 0; i < als.size(); i++) { // 将集合中的16个按钮赋值给二维数组
      **s[i / 4][i % 4] = als.get(i);
      }
      }
      public void Randomizer() { // 随机生成2
      boolean flag = true;
      if (i > 0) {
      while (flag) {
      int num = random.nextInt(4);
      int num1 = random.nextInt(4);
      JButton ** = **s[num][num1]; // 获得随机按键位置
      String str = **.getText(); // 获得该按钮的数字
      if (str.equals("")) {
      **.setText("2");
      i--; // 成功创建一个2,则总数-1
      l1 = l2 = l3 = l4 = 0;
      break;
      }
      }
      }
      }
      @Override
      public void keyTyped(KeyEvent e) {
      // TODO Auto-generated method stub
      }
      @Override
      public void keyPressed(KeyEvent e) {
      int code = e.getKeyCode();
      // 向上
      if (code == KeyEvent.VK_UP || code == KeyEvent.VK_W) {
      for (int y = 1; y < 4; y++) { // Y轴
      for (int x = 0; x < 4; x++) { // X轴
      String str = **s[y][x].getText();
      if (str.equals("")) { // 如果是空,则不需要动
      continue;
      }
      for (int y1 = y - 1; y1 >= 0; y1--) {
      String str1 = **s[y1][x].getText(); // 获取当前元素的上一个 元素
      if (!str1.equals("")) { // 如果上一个元素不为空,则判定是否和自身相等
      if (str1.equals(str)) { // 若两个元素相等,则合并
      **s[y][x].setText(""); // 自身变成空
      num = Integer.parseInt(str1);
      score += num * 2; // 分数
      textField.setText(String.valueOf(score));
      **s[y1][x].setText(String.valueOf(num * 2)); // 合并后的值给上一位
      i++; // 剩余方块总数+1
      }
      } else { // 如果上一个元素为空,则直接移动
      **s[y][x].setText("");
      **s[y1][x].setText(str);
      }
      }
      }
      }
      l1 = 1;
      Randomizer();
      }


      IP属地:湖南4楼2017-04-24 12:37
      回复
        下面的我就不放了,反正是重复的,心累


        IP属地:湖南5楼2017-04-24 12:38
        回复
          就是我一按up或者w键,它就在随即位置上生成随机个数的2


          IP属地:湖南6楼2017-04-24 12:39
          回复
            简直就像我现在的不高兴表情,一会儿生成一个2,一会儿生成好几个2


            IP属地:湖南7楼2017-04-24 12:40
            回复
              求各位大佬教教我怎么改


              IP属地:湖南8楼2017-04-24 12:41
              回复
                求助啊!大佬们!


                IP属地:湖南10楼2017-04-25 10:11
                回复