在数独游戏中用户输入的有效方法

时间:2014-07-07 08:37:09

标签: java

我正在尝试一个数独游戏,为此我已经采取了9个按钮标记为1到9和81个文本字段供用户输入。我正在尝试从这九个按钮获取输入。    现在我应该如何编码以在81个文本字段中选择文本字段,以便用户只需按一下按钮就可以输入任何文本字段编号1到9。       例如:假设我现在点击第二个按钮我如何编码,以便用户可以选择81个文本字段中的任何文本字段来放置2。 一个选项是拖动所需文本字段上的按钮。但是有没有其他有效的方法来做到这一点。 第一种方法工作...

     for(int i=0;i<9;i++)
                {
                    for(int j=0;j<9;j++)
                    {
                    cellField[i][j].setHorizontalAlignment(JTextField.CENTER);
                  cellField[i][j].addFocusListener(new java.awt.event.FocusAdapter() {
                      public void focusGained(java.awt.event.FocusEvent evt) {

                          setFocused(evt);


                      }

                    private void setFocused(FocusEvent evt) {
                        // TODO Auto-generated method stub
                         selectedTextField=(JTextField) evt.getComponent();
                    }

                    });
                }
            }
        }

        public void updateString(String newValue) {
            selectedTextField.setText(newValue);
        }
    Action performed on one of the buttons..
    if (e.getSource() == b1)

            {
                String buttonText = b1.getLabel();
                updateString(buttonText);
            }

Transpareny code
private void createAndShowGUI() {
        // TODO Auto-generated method stub
        this.setLayout(null);
        setContentPane(new JLabel(new ImageIcon("G://Wallpapers//ni.jpg")));

        pan1 = new JPanel();
        pan1.setBackground(new Color(0, 0, 0, 35));//transparency of outer panel
        // pan1.setPreferredSize(new Dimension(400, 400));
        this.getContentPane().add(pan1);
        pan1.setBounds(400, 100, 400, 400);

        // 3 rows and 3 columns
        // in the layout for the
        for (int b = 0; b < 9; b++) {
            cellPanel[b] = new JPanel();
            GridLayout cell = new GridLayout(3, 3, 2, 2);
            cellPanel[b].setLayout(cell);
            // pan1.setLayout(null);

            cellPanel[b].setBackground(new Color(0, 0, 0, 2));//transparency of inner 9 panels
            pan1.add(cellPanel[b]);

            cellPanel[b].setBorder(BorderFactory.createCompoundBorder(
                    BorderFactory.createRaisedBevelBorder(),
                    BorderFactory.createLoweredBevelBorder()));

        }

        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                cellField[i][j] = new JTextField(1);
            }
        }//transparency of 81 text fields
        for (int x = 0; x < 3; x++) {
            for (int c = 0; c < 3; c++) {
                cellPanel[0].add(cellField[x][c]);
                cellField[x][c].setBackground(new Color(10, 175, 193, 25));
            }
        }
        // cellPanel 1
        for (int x = 0; x < 3; x++) {
            for (int c = 3; c < 6; c++) {
                cellField[x][c].setBackground(new Color(10, 175, 193, 25));
                cellPanel[1].add(cellField[x][c]);

            }
        }
        // cellPanel 2
        for (int x = 0; x < 3; x++) {
            for (int c = 6; c < 9; c++) {

                cellField[x][c].setBackground(new Color(10, 175, 193, 65)); // JTextField[3][3];
                cellPanel[2].add(cellField[x][c]);
                // cellField[x][c].setBackground(new Color(0,0,0,25));
            } // End inner for loop
        }
        // cellPanel 3
        for (int x = 3; x < 6; x++) {
            for (int c = 0; c < 3; c++) { // JTextField[][] cellField = new
                cellField[x][c].setBackground(new Color(10, 175, 193, 65));
                ; // JTextField[3][3];
                cellPanel[3].add(cellField[x][c]);
                // cellField[x][c].setBackground(new Color(0,0,0,25));

            } // End inner for loop
        }
        // cellPanel 4
        for (int x = 3; x < 6; x++) {
            for (int c = 3; c < 6; c++) { // JTextField[][] cellField = new
                cellField[x][c].setBackground(new Color(10, 175, 193, 65)); // JTextField[3][3];
                cellPanel[4].add(cellField[x][c]);
                // cellField[x][c].setBackground(new Color(0,0,0,25));
            } // End inner for loop

        } // cellPanel 5
        for (int x = 3; x < 6; x++) {
            for (int c = 6; c < 9; c++) {// JTextField[][] cellField = new
                                            // JTextField[3][3];
                cellField[x][c].setBackground(new Color(10, 175, 193, 65));
                cellPanel[5].add(cellField[x][c]);
                // cellField[x][c].setBackground(new Color(0,0,0,25));
            } // End inner for loop

        }
        // cellPanel 6
        for (int x = 6; x < 9; x++) {
            for (int c = 0; c < 3; c++) {// JTextField[][] cellField = new
                cellField[x][c].setBackground(new Color(10, 175, 193, 65));
                cellPanel[6].add(cellField[x][c]); // JTextField[3][3];

                // cellField[x][c].setBackground(new Color(0,0,0,25));
            }
        }
        // cellzPanel 7
        for (int x = 6; x < 9; x++) {
            for (int c = 3; c < 6; c++) {// JTextField[][] cellField = new
                // cellField[x][c]).setNullRepresentation("");
                // JTextField[3][3];
                cellField[x][c].setBackground(new Color(10, 175, 193, 65));
                cellPanel[7].add(cellField[x][c]);

            } // End inner for loop
        }
        // cellPanel 8
        for (int x = 6; x < 9; x++) {
            for (int c = 6; c < 9; c++) {
                cellPanel[8].add(cellField[x][c]);
                cellField[x][c].setBackground(new Color(10, 175, 193, 65));

            } // End inner for loop
        }
        setSize(600, 400);
        setVisible(true);

    }

1 个答案:

答案 0 :(得分:0)

我们的想法是拥有一个全局变量来保存最后一次单击按钮的文本或保存最后选择的文本字段。

第一种方法: 添加全局文本字段变量:

private JTextFiled selectedTextField;

将此侦听器添加到文本字段创建

 textField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            setFocused(evt);
        }
    });

以setfocused方式:

private void setfocused(java.awt.event.FocusEvent evt) {                                        
    selectedTextField=(JTextField) evt.getComponent();
}

在按钮事件处理程序中,将selectedTextField中的文本设置为按钮上的数字。

private void updateField(String newValue) {                                        
    selectedTextField.setText(newValue);
}

第二种方法:

添加一个全局String变量:

private String currentValue;

将此侦听器添加到文本字段创建

 textField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            this.setText(currentValue);
        }
    });

在按钮事件处理程序中,将字符串设置为按钮上的数字。

private void updateString(String newValue) {                                        
    currentValue=newValue;
}