Java 数独游戏中用户输入的有效方法
我正在尝试一个数独游戏,为此,我使用了9个按钮,分别标记为1到9和81个文本字段,用于用户输入。我正在尝试从这9个按钮中获取输入。 现在,我应该如何编写代码,在81个文本字段中选择一个文本字段,以便用户只需单击按钮就可以输入任何编号为1到9的文本字段。 例如:假设我现在单击第二个按钮“如何编写代码”,以便用户可以在81个文本字段中选择任何文本字段来放置2。 一个选项是拖动所需文本字段上的按钮。但是有没有其他有效的方法来做到这一点呢。 第一种方法工作Java 数独游戏中用户输入的有效方法,java,Java,我正在尝试一个数独游戏,为此,我使用了9个按钮,分别标记为1到9和81个文本字段,用于用户输入。我正在尝试从这9个按钮中获取输入。 现在,我应该如何编写代码,在81个文本字段中选择一个文本字段,以便用户只需单击按钮就可以输入任何编号为1到9的文本字段。 例如:假设我现在单击第二个按钮“如何编写代码”,以便用户可以在81个文本字段中选择任何文本字段来放置2。 一个选项是拖动所需文本字段上的按钮。但是有没有其他有效的方法来做到这一点呢。 第一种方法工作 for(int i=0;i<9
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);
}
for(int i=0;i的思想是使用一个全局变量来保存上次单击按钮的文本或保存上次选择的文本字段
第一种方法:
添加全局文本字段变量:
private JTextFiled selectedTextField;
private String currentValue;
将此侦听器添加到文本字段创建中
textField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
setFocused(evt);
}
});
textField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
this.setText(currentValue);
}
});
在setfocused方法中:
private void setfocused(java.awt.event.FocusEvent evt) {
selectedTextField=(JTextField) evt.getComponent();
}
在按钮事件处理程序中,将selectedTextField中的文本设置为按钮上的数字
private void updateField(String newValue) {
selectedTextField.setText(newValue);
}
private void updateString(String newValue) {
currentValue=newValue;
}
第二种方法:
添加全局字符串变量:
private JTextFiled selectedTextField;
private String currentValue;
将此侦听器添加到文本字段创建中
textField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
setFocused(evt);
}
});
textField.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
this.setText(currentValue);
}
});
在按钮事件处理程序中,将字符串设置为按钮上的数字
private void updateField(String newValue) {
selectedTextField.setText(newValue);
}
private void updateString(String newValue) {
currentValue=newValue;
}
为什么使用按钮?使用81个文本字段和键盘键1-9输入数据并清除空格。@laune只是用户输入的另一种方式,以防用户键盘不工作。用户可以通过鼠标输入。用户可以通过任何一种方式输入文本字段,通过单击它来选择。光标键可用于选择邻居。我想我也看到了9个非常小的按钮出现在一个空白的字段中,当鼠标放在它上面或通过点击选择该字段时。@laune那么我有没有办法使用按钮输入文本字段`你能解释一下我如何创建一个本地文本字段来保存当前所选的文本字段吗。将这一行添加到你的类中:private JTextFild selectedTextField=new JTextFild();我可以在单击按钮时获取按钮的标签,然后在单击文本字段时在文本字段上设置相同的标签吗?是的,您也可以这样做。感谢您的帮助,我在获取字符串newValue中的按钮标签时使用了您的第二种方法。但是,尽管在特定单元格字段中设置了该值,但在所有单元格字段中都设置了该值我做错什么了吗?请帮忙