我有一个java程序,可以为我创建sudokus的解决方案。然后将其打印到GUI,其中包含电路板本身,并在其下方有两个标签(当前解决方案编号和总解决方案)以及下一个按钮。这三个按钮效果很好。然而,sudokuboard让我非常头痛。我在一个方法中创建了一个方法,该方法返回一个面板,该面板包含符号框的面板,其中JLabel具有实际值。
我确实让面板打印到GUI,但它在一行并折叠。根本不是盒子! 它开始在按钮和标签上方对齐,然后当我按下下一个按钮时,它移动到顶部,下一个sudokuboard(仍然在折叠框架中)出现在下面。 我的代码中是否有一些我忽略的东西?到目前为止,我已经尝试过GridBagLayout,FlowLayout,GridLayout和BorderLayout。都具有相同的结果。 我把代码粘贴在下面。请问是否有不明确的事情。
JFrame frame;
JPanel sudokuPanel, buttonPanel;
GridBagConstraints c;
JLabel currentSolution, counterLabel;
JButton button;
/*
* printSolution takes a beholder-object with all the solutions, as well as
* the board created after reading the text-file. Then it creates the GUI
* with help from the createSudokuPanel-method. In the
* createSudokuPanel-method I use variables which I get from the
* solution-objects, such as all values in the solution. the rows and
* columns-variables are information read from the text-file, telling how
* mant rows and columns there are in each box. the convertValue-method
* takes a number higher than 9, up to 15, and converts it into letters
* (A...F)
*/
public void printSolutionsGUI(final SudokuBeholder beholder, Board board) {
frame = new JFrame();
buttonPanel = new JPanel();
final int xCoord = board.getSize();
final int yCoord = xCoord;
// final GridLayout gridTemp=new GridLayout(0, 1);
// final BorderLayOut borderL = new BorderLayout
final GridLayout grid = new GridLayout(xCoord, yCoord);
final GridLayout bottomLineGrid = new GridLayout(1, 3);
// GridBagLayout gridFrame = new GridBagLayout();
// c = new GridBagConstraints();
// xCoord + 1, yCoord
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 720);
frame.setVisible(true);
// frame.setLayout(gridTemp);
buttonPanel.setLayout(bottomLineGrid);
sudokuPanel = createSudokuPanel(grid, beholder);
// c.fill=GridBagConstraints.HORIZONTAL;
// c.gridx=0;
// c.gridy=0;
// c.fill=GridBagConstraints.REMAINDER;
// c.anchor=GridBagConstraints.NORTH;
frame.add(sudokuPanel, BorderLayout.NORTH);
counterLabel = new JLabel("Number of solutions: " + (Integer.toString(beholder.getSolutionCount())));
currentSolution = new JLabel("This is solution number: " + (Integer.toString(beholder.tempCount)));
counterLabel.setHorizontalAlignment(SwingConstants.CENTER);
currentSolution.setHorizontalAlignment(SwingConstants.CENTER);
button = new JButton("Next Solution");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.remove(sudokuPanel);
sudokuPanel = createSudokuPanel(grid, beholder);
frame.add(sudokuPanel, BorderLayout.NORTH);
currentSolution.setText("This is solution number: " + (Integer.toString(beholder.tempCount)));
}
});
buttonPanel.add(counterLabel);
buttonPanel.add(button);
buttonPanel.add(currentSolution);
// c.fill=GridBagConstraints.HORIZONTAL;
// c.gridx=0;
// c.gridy=1;
// c.gridwidth=GridBagConstraints.REMAINDER;
frame.add(buttonPanel, BorderLayout.SOUTH);
}
JPanel createSudokuPanel(GridLayout grid, SudokuBeholder beholder) {
BoardSolution b = beholder.get();
b.createBoxes();
JPanel sudokuPanel = new JPanel();
sudokuPanel.setLayout(grid);
ArrayList<Box> boxList = b.getBoxList();
JPanel[] panelArray = new JPanel[b.getSize()];
for (int i = 0; i < b.getSize(); i++) {
JPanel panel = new JPanel();
panel.setLayout(grid);
Box box = boxList.get(i);
for (int j = 0; j < b.rows; j++) {
for (int k = 0; k < b.columns; k++) {
String content = Integer.toString(box.boxValuesForGUI[j][k]);
if (Integer.parseInt(content) > 9) {
char c = convertValue(Integer.parseInt(content));
content = Integer.toString(Character.getNumericValue(c));
}
JLabel label = new JLabel(content);
label.setBorder(BorderFactory.createLineBorder(Color.black, 1));
label.setHorizontalAlignment(SwingConstants.CENTER);
panel.add(label);
}
}
panelArray[i] = panel;
}
for (int i = 0; i < panelArray.length; i++) {
JPanel panel = panelArray[i];
panel.setBorder(BorderFactory.createLineBorder(Color.black, 3));
sudokuPanel.add(panel);
}
return sudokuPanel;
}