보드의 파일에서 정수를 표시하고 누군가가 JButton을 클릭하여 누락 된 값을 입력 할 수있는 스도쿠 보드를 만들 필요가 있습니다.Java Swing (ActionListener 문제)
JPanel을 사용하여 보드를 표시하고 텍스트 파일을 개별 버튼에 인쇄했지만 addActionListener가 값이 누락 된 버튼을 선택하는 방법을 알아낼 수 없습니다. 비어있는 마지막 단추에서만 작동합니다. (빈 단추에는 0 값이 제공됩니다).
제 질문은 왜 마지막 빈 버튼 만 타겟팅되는지입니다. 총 6 개가 있지만 마지막 클릭 만 클릭하면 대화 상자가 나타납니다.
public class MyCustomeFrame extends JFrame {
private int[][] numbers;
private String[] nums;
JButton b1;
JButton b2;
JButton b3;
JButton b4;
private JPanel p2;
public MyCustomeFrame() {
// Create the border layout
setLayout(new BorderLayout(5, 5));
// Create a new panel for the buttons to be placed on
JPanel p1 = new JPanel();
// Create 3 buttons
b1 = new JButton("Load");
b2 = new JButton("Save");
b3 = new JButton("Check");
// Adds the 3 buttons to the panel
p1.add(b1);
p1.add(b2);
p1.add(b3);
// Create the event handlers for when the button is pressed
b1.addActionListener(new MyButtonHandler());
b2.addActionListener(new MyButtonHandler());
b3.addActionListener(new MyButtonHandler());
// Place the panel south in the window
add(p1, BorderLayout.SOUTH);
p2 = new JPanel();
// Define the grid parameters
p2.setLayout(new GridLayout(9, 9, 5, 5));
// Show the grid
add(p2, BorderLayout.CENTER);
int[][] numbers = new int[9][9];
int rowIdx = 0;
//This is where i read the input file located on my computer and place the numbers on the Sudoku board
try {
BufferedReader bReader = new BufferedReader(new FileReader(
"C:\\Users\\Derek\\Desktop\\input.txt"));
String line = bReader.readLine();
while (line != null) {
nums = line.split(",");
for (int i = 0; i < numbers[0].length; i++) {
numbers[rowIdx][i] = Integer.parseInt(nums[i]);
// This creates the individual buttons that are then placed on the board
if (numbers[rowIdx][i] >= 1) {
p2.add(new JButton(nums[i]));
} else {
//this is where I'm having the issue
b4 = new JButton(" ");
p2.add(b4);
b4.addActionListener(new MyButtonHandler());
}
}
rowIdx++;
line = bReader.readLine();
}
bReader.close();
} catch (FileNotFoundException g) {
System.out.println("File Not Found!");
} catch (IOException g) {
System.out.println("Something went wrong...Try Again");
g.printStackTrace();
}
}
class MyButtonHandler implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) {
System.out.println("Loading File...");
} else if (e.getSource() == b2) {
System.out.println("Saving File...");
try {
BufferedWriter bWriter = new BufferedWriter(new FileWriter(
new File("C:\\SudokuSave.txt"), true));
bWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} else if (e.getSource() == b3) {
System.out.println("Checking Solution...");
} else if (e.getSource() == b4) {
System.out.println("clicked");
JOptionPane.showInputDialog("Input a number between 1 - 9");
}
}
}
}
는 또한 ['의회 처리] 고려 (http://docs.oracle.com/javase/tutorial/uiswing/misc/action.html) "를 사용하여 구성 요소에서 기능과 상태를 분리합니다." – trashgod
@trashgod 확실히! 스도쿠 보드에있는 그리드의 경우, 이것은 유익한 것은 아니지만 표준 액션 (로드, 저장)에 대해서는 분명히 할 수 있습니다. 특히 동일한 기능이 있어야 할 때가 있습니다. Button과 MenuItem에서 사용되거나 I18N과 같은 것이 재생 될 때 ... – Marco13
['JDigit'] (http://stackoverflow.com/a/4151403/230513)은'AbstractAction'을 사용하는 예제입니다. – trashgod