im currenty wokring上一个读取包含数独谜题的文本的程序,然后程序解决它并显示所有解决方案。我差不多完成了,但遇到了一个问题。我有一个下一个按钮,当点击时,更新按钮文本和更新/获取下一个解决方案(程序保存每个解决方案)。这是我的代码:
class Gui extends JFrame{
int cnt = 1;
String[][] solution;
int nrRows, nrColumns;
SudokuContainer sc;
Gui(SudokuContainer sc, int nrRows, int nrColumns){
this.sc = sc;
this.nrRows = nrRows;
this.nrColumns = nrColumns;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
void finnishingVows(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void createBoard(SudokuContainer sc, int nrRows, int nrColumns) {
JPanel panel = new JPanel();
JLabel label;
JButton previousButton;
JButton nextButton;
previousButton = new JButton("Previous (" + cnt + "/" + sc.nrSolutions + ")");
nextButton = new JButton("Next (" + cnt + "/" + sc.nrSolutions + ")");
solution = sc.get(cnt);
nextButton.addActionListener(new Listener());
panel.setLayout(new GridLayout(nrRows*nrColumns,nrRows*nrColumns));
previousButton.addActionListener(new Listener2());
for(int i = 0; i <solution.length;i++){
for(int j = 0; j < solution[i].length; j++){
String values = " "+solution[i][j]+" ";
if(values.equals(" . ")) values = "";
label = new JLabel(values);
if((i+1)%2 != 0)
if(j == nrRows){
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); //FIKSER PÅ STREKENE
}
else if(j == nrColumns){
label.setBorder(BorderFactory.createMatteBorder(1, 3, 1, 1, Color.black));//FIKSER PÅ STREKENE
}
else
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
if((i+1)%2 == 0)
if(j == nrRows){
label.setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.black));
}
else if(j == nrColumns){
label.setBorder(BorderFactory.createMatteBorder(1, 3, 3, 1, Color.black));
}
else label.setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.black));
label.setFont(new Font("SansSerif", Font.BOLD, 32));
panel.add(label);
}
}
add(panel, BorderLayout.NORTH);
add(nextButton, BorderLayout.EAST);
add(previousButton, BorderLayout.WEST);
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
cnt++;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
}
class Listener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
cnt--;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
}
} //class Gui slutt
答案 0 :(得分:0)
正如其中一条评论中所述,在添加更多内容之前,您并未删除组件。如果您创建包含按钮和面板的类变量,则可以删除它们并添加新变量。以下是我的看法:
class Gui extends JFrame{
int cnt = 1;
String[][] solution;
int nrRows, nrColumns;
SudokuContainer sc;
private JPanel pan = null;
private JButton nButton = null;
private JButton pButton = null;
Gui(SudokuContainer sc, int nrRows, int nrColumns){
this.sc = sc;
this.nrRows = nrRows;
this.nrColumns = nrColumns;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
void finnishingVows(){
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
void createBoard(SudokuContainer sc, int nrRows, int nrColumns) {
JPanel panel = new JPanel();
JLabel label;
JButton previousButton;
JButton nextButton;
previousButton = new JButton("Previous (" + cnt + "/" + sc.nrSolutions + ")");
nextButton = new JButton("Next (" + cnt + "/" + sc.nrSolutions + ")");
solution = sc.get(cnt);
nextButton.addActionListener(new Listener());
panel.setLayout(new GridLayout(nrRows*nrColumns,nrRows*nrColumns));
previousButton.addActionListener(new Listener2());
for(int i = 0; i <solution.length;i++){
for(int j = 0; j < solution[i].length; j++){
String values = " "+solution[i][j]+" ";
if(values.equals(" . ")) values = "";
label = new JLabel(values);
if((i+1)%2 != 0)
if(j == nrRows){
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black)); //FIKSER PÅ STREKENE
}
else if(j == nrColumns){
label.setBorder(BorderFactory.createMatteBorder(1, 3, 1, 1, Color.black));//FIKSER PÅ STREKENE
}
else
label.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.black));
if((i+1)%2 == 0)
if(j == nrRows){
label.setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.black));
}
else if(j == nrColumns){
label.setBorder(BorderFactory.createMatteBorder(1, 3, 3, 1, Color.black));
}
else label.setBorder(BorderFactory.createMatteBorder(1, 1, 3, 1, Color.black));
label.setFont(new Font("SansSerif", Font.BOLD, 32));
panel.add(label);
}
}
if (pan != null) this.remove(pan);
if (pButton != null) this.remove(pButton);
if (nButton != null) this.remove(nButton);
pan = panel;
pButton = previousButton;
nButton = nextButton;
add(panel, BorderLayout.NORTH);
add(nextButton, BorderLayout.EAST);
add(previousButton, BorderLayout.WEST);
}
class Listener implements ActionListener{
public void actionPerformed(ActionEvent e){
cnt++;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
}
class Listener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
cnt--;
createBoard(sc,nrRows,nrColumns);
finnishingVows();
}
}
}