我正在研究这段代码,可以在以下位置找到

http://pastebin.com/7bCFtUHL

基本上,我想添加一个清除方法(按钮),以解决数独问题。
我尝试过遍历每个单元格并将其设置为null的循环,但是我不确定如何准确地连接它。我也不确定我必须在哪个类中创建它,以便可以将其连接到具有其他按钮的GUI。

编辑:
这是我目前得到的明确方法

public void clearCells(){
    for (int y = 0; y < 9; y++) {
        for (int x = 0; x < 9; x++) {
            cells[y][x] = null;
            cells[y][x].setText("");
        }
    }
}


现在,我需要将其附加到另一个类的JButton中,这怎么可能?

我的清除按钮看起来像这样

JButton clear = new JButton("Clear");

clear.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {
        //Code
    }
    }
);


我需要在actionPerformed方法中添加什么代码才能将其与我的clearCells方法连接?

最佳答案

同样,我将clear方法的“实质”放在模型本身中。解决方案的一般形式是:

clear.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {
       myModel.clearCells();
  }
});


Model类将具有一个公共的void clearCells()方法,该方法遍历单元格并清除它们。



编辑1
注意:是的,我确实查看了您的pastebin代码链接,我看到的一个大问题是您的SwingSudokuBoard类扩展了SudokuBoard类,这是对继承的滥用,您应该改用组合。 SwingSudokuBoard类应包含SudokuBoard对象的实例并在其上调用方法。



编辑2
你问:


  我不确定我是否能完全理解你。您希望我在获得按钮的同一个类中拥有clear方法,但随后我无法调用单元格。我添加了x.clearCells();而x是什么?我的主类是SwingSudokuBoard.clearCells(); ?无论如何,如果我添加您所说的内容,该程序就会抱怨它希望clearCells方法和单元格是静态的。但是,如果将它们置于静态,则会得到NullPointerException。


我认为您需要使用“模型-视图-控件”(MVC)模式或它的缩写版本,也许是其中的一种,因为程序很小,因此您可以将视图与控件结合起来。我建议您有一个单独的模型类,这里可能是SudokuBoard类,然后是视图类,这里可能是SwingSudokuBoard类。您视图的控制方法(ActionListeners)将调用模型的clearCells()方法。并且不要在这里使用静态任何东西。



编辑3
你问:


  我认为有些话。型号:SudokuBoard;视图:SwingSudokuBoard;控制:SwingSudoKiller。那会如何?我将把ActionListener张贴在控件中。其他班级的情况如何?由于我假设清除方法位于要放在SudokuBoard中的模型中,但无法与那里的单元连接。


我不是专业人士,也没有接受过正式的编程培训,所以理论是我的弱点之一,但是我对MVC的解释是,视图会听取模型并在模型通知更改时进行更新,并且控件侦听视图,并通过通知模型来响应视图更改。这种精确的模式具有变化形式,不需要完全跟在字母后面,但是所有这些的关键是在您的代码中尽可能地分离出单独的关注点,以便“耦合”(之间的直接连接数类)为低或“松散”,而“内聚力”(处理相同问题的代码)为高或“紧”。

在您的程序中,就像您在做的一样,我再次使用匿名内部侦听器将视图和控件结合在一起。我要有一个视图/控件,它是SwingSudokuBoard类,将SudokuBoard类的一个实例保存为类字段,并在SudokuBoard字段上具有视图/控件的匿名侦听器调用方法。当我以前做过这种事情时,我通过给它提供一个SwingPropertyChangeSupport对象以及公共的addPropertyChangeListener(...)removePropertyChangeListener(...)方法来提供对模型观察的支持。这样,视图可以轻松响应模型中的更改。

您声明:


  由于我假设清除方法位于要放在SudokuBoard中的模型中,但无法与那里的单元连接。


我不确定您的意思。该模型保存单元格。也许您不是说模型所拥有的逻辑单元,而是视图所拥有的显示单元。该视图将向模型添加一个侦听器,并在收到有关模型更改的通知时,将向模型询问其数据,并使用该数据来更新可视化的单元格。



编辑4
例如:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class OverlySimpleModelView {

   private static void createAndShowGui() {
      Model model = new Model();
      ViewControl viewControl = new ViewControl(model);

      JFrame frame = new JFrame("OverlySimpleModelView");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(viewControl.getMainComponent());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ViewControl {
   private JPanel mainPanel = new JPanel();
   private JTextField number1Field = new JTextField(5);
   private JTextField number2Field = new JTextField(5);
   private JTextField productField = new JTextField(5);

   private Model model;

   public ViewControl(Model model) {
      this.model = model;
      model.addPropertyChangeListener(new MyPropChngListener());
      productField.setEditable(false);
      productField.setFocusable(false);

      mainPanel.add(number1Field);
      mainPanel.add(new JLabel(" * "));
      mainPanel.add(number2Field);
      mainPanel.add(new JLabel(" = "));
      mainPanel.add(productField);

      CalculateAction calculateAction = new CalculateAction("Calculate", KeyEvent.VK_C);
      mainPanel.add(new JButton(calculateAction));
      number1Field.addActionListener(calculateAction);
      number2Field.addActionListener(calculateAction);
      mainPanel.add(new JButton(new ClearAction("Clear", KeyEvent.VK_L)));
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   private class MyPropChngListener implements PropertyChangeListener {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
         number1Field.setText(String.valueOf(model.getNumber1()));
         number2Field.setText(String.valueOf(model.getNumber2()));
         productField.setText(String.valueOf(model.calculateProduct()));
      }
   }

   private class CalculateAction extends AbstractAction {

      public CalculateAction(String text, int keyCode) {
         super(text);
         putValue(MNEMONIC_KEY, keyCode);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         try {
            double number1 = Double.parseDouble(number1Field.getText());
            double number2 = Double.parseDouble(number2Field.getText());

            model.setNumber1(number1);
            model.setNumber2(number2);
         } catch (NumberFormatException e) {
            e.printStackTrace();
         }
      }
   }

   private class ClearAction extends AbstractAction {

      public ClearAction(String text, int keyCode) {
         super(text);
         putValue(MNEMONIC_KEY, keyCode); // to allow buttons a mnemonic letter
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         model.clear();
      }
   }
}

class Model {
   public static final String NUMBERS_CHANGED = "numbers changed";
   private double number1 = 0.0;
   private double number2 = 0.0;
   private SwingPropertyChangeSupport propChngSupport =
         new SwingPropertyChangeSupport(this);



   public double getNumber1() {
      return number1;
   }

   public double getNumber2() {
      return number2;
   }

   public void clear() {
      setNumber1(0.0);
      setNumber2(0.0);
   }

   // make number1 field a "bound" property, one that notifies listeners if it is changed.
   public void setNumber1(double number1) {
      Double oldValue = this.number1;
      Double newValue = number1;
      this.number1 = number1;
      propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
   }

   // ditto for the number2 field
   public void setNumber2(double number2) {
      Double oldValue = this.number2;
      Double newValue = number2;
      this.number2 = number2;
      propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
   }

   public double calculateProduct() {
      return number1 * number2;
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      propChngSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      propChngSupport.removePropertyChangeListener(listener);
   }
}


也许更好,因为它使用数字数组:

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;

public class OverlySimpleModelView {

   private static void createAndShowGui() {
      Model model = new Model(5);
      ViewControl viewControl = new ViewControl(model);

      JFrame frame = new JFrame("OverlySimpleModelView");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(viewControl.getMainComponent());
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class ViewControl {
   private JPanel mainPanel = new JPanel();
   private JTextField[] numberFields;
   private JTextField productField = new JTextField(5);

   private Model model;

   public ViewControl(Model model) {
      this.model = model;
      model.addPropertyChangeListener(new MyPropChngListener());
      productField.setEditable(false);
      productField.setFocusable(false);

      CalculateAction calculateAction = new CalculateAction("Calculate", KeyEvent.VK_C);

      numberFields = new JTextField[model.getNumberFieldsLength()];
      for (int i = 0; i < numberFields.length; i++) {
         numberFields[i] = new JTextField("0.0", 5);
         mainPanel.add(numberFields[i]);
         numberFields[i].addActionListener(calculateAction);

         if (i < numberFields.length - 1) {
            mainPanel.add(new JLabel(" + "));
         } else {
            mainPanel.add(new JLabel(" = "));
         }
      }
      mainPanel.add(productField);

      mainPanel.add(new JButton(calculateAction));
      mainPanel.add(new JButton(new ClearAction("Clear", KeyEvent.VK_L)));
   }

   public JComponent getMainComponent() {
      return mainPanel;
   }

   private class MyPropChngListener implements PropertyChangeListener {
      @Override
      public void propertyChange(PropertyChangeEvent evt) {
         for (int i = 0; i < numberFields.length; i++) {
            numberFields[i].setText(String.valueOf(model.getNumber(i)));
         }
         productField.setText(String.valueOf(model.calculateSum()));
      }
   }

   private class CalculateAction extends AbstractAction {

      public CalculateAction(String text, int keyCode) {
         super(text);
         putValue(MNEMONIC_KEY, keyCode);
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         try {
            double[] numbers = new double[numberFields.length];
            for (int i = 0; i < numbers.length; i++) {
               numbers[i] = Double.parseDouble(numberFields[i].getText());
            }

            model.setNumbers(numbers);
         } catch (NumberFormatException e) {
            e.printStackTrace();
         }
      }
   }

   private class ClearAction extends AbstractAction {

      public ClearAction(String text, int keyCode) {
         super(text);
         putValue(MNEMONIC_KEY, keyCode); // to allow buttons a mnemonic letter
      }

      @Override
      public void actionPerformed(ActionEvent evt) {
         model.clear();
      }
   }
}

class Model {
   public static final String NUMBERS_CHANGED = "numbers changed";
   private double[] numbers;
   private SwingPropertyChangeSupport propChngSupport =
         new SwingPropertyChangeSupport(this);

   public Model(int length) {
      numbers = new double[length];
   }

   public void setNumbers(double[] numbers) {
      double[] oldValue = this.numbers;
      double[] newValue = numbers;
      this.numbers = numbers;
      propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);

   }

   public double calculateSum() {
      double sum = 0.0;
      for (double number : numbers) {
         sum += number;
      }
      return sum;
   }

   public double getNumber(int i) {
      return numbers[i];
   }

   public int getNumberFieldsLength() {
      return numbers.length;
   }

   public void clear() {
      double[] newNumbers = new double[numbers.length];
      setNumbers(newNumbers);
   }

   public void addPropertyChangeListener(PropertyChangeListener listener) {
      propChngSupport.addPropertyChangeListener(listener);
   }

   public void removePropertyChangeListener(PropertyChangeListener listener) {
      propChngSupport.removePropertyChangeListener(listener);
   }
}

10-06 08:58