2014-10-17 51 views
0

我有三个不同的类:Main,WindowFrameDimetnions和ValidationOfNumbers。主要 - 调用WindowFrameDimetnions。它是主类 WindowFrameDimetnions - 调用(以及我试图调用)ValidationOfNumbers。这是为程序创建框架的类,窗格,框的标签和按钮。 ValidationOfNumbers - 是进行数字验证所有计算的人员。基本上这个类验证用户输入的数字在1..100,000的范围内。当按钮功能在另一个类中时如何使用ActionListener

目标: 我们的目标是通过使用一个ActionListener与ValidationOfNumbers WindowFrameDimetnions连接。

package BlueBlueMainFiles; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JTextField; 

public class WindowFrameDimentions extends JFrame{ 

    final static int WINDOW_WITH = 950;//Window with in pixel 
    final static int WINDOW_HEIGH = 650;//Window height in pixel 
    static JPanel  panel;//use to reference the panel 
    static JLabel  messageLabel;//use to reference the label 
    static JTextField textField;//use to reference the text field 
    static JButton  calcButton;//use to reference the button 

    public WindowFrameDimentions() { 
     // TODO Auto-generated constructor stub 
    } 

    public static void windowFrameDimentions(){ 
     //create a new window 
     JFrame window = new JFrame(); 

     //add a name to the window 
     window.setTitle("BLUE BLUE"); 

     //set the size of the window 
     window.setSize(WINDOW_WITH, WINDOW_HEIGH); 

     //specify what happens when the close button is pressed 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //BUILD THE PANEL AND ADD IT TO THE FRAME 
     buildPanel(); 

     //ADD THE PANEL TO THE FRAMES CONTENT PANE 
     window.add(panel); 

     //Display the window 
     window.setVisible(true); 
    } 

    public static void buildPanel(){ 
     //create a label to display instructions 
     messageLabel = new JLabel("Enter a Number from 1..100,000"); 

     //create a text field of 10 characters wide 
     textField = new JTextField(10); 

     //create panel 
     calcButton = new JButton("Calculate"); 


     //Add an action listening to the button. Currently, I can't make it work 


     //Create the a JPanel object and let the panel field reference it 
     panel = new JPanel(); 

     panel.add(messageLabel); 
     panel.add(textField); 
     panel.add(calcButton); 

    } 
} 

现在,这是其他代码:

package TheValidationFiles; 


public class ValidationOfNumbers { 

    static int MAX_NUMBER_TO_VAL = 10000000; 

    public static void GetValidationOfNumbers(boolean isTrue, String s) { 

      String[] numberArray = new String [MAX_NUMBER_TO_VAL]; 
      boolean numberMatching = false; 

      for (int i = 0; i < MAX_NUMBER_TO_VAL; i++){ 
        numberArray[i] = Integer.toString(i); 

        if (numberArray[i].equals(s)){ 
         System.out.println("The number you typed " + s + " Matches with the array value of: " + numberArray[i]); 
         System.exit(0); 
         break; 
        } 
        else{ 
         numberMatching = true; 
        } 
      } 
      if(numberMatching){ 
       ValidationOfFiles.ValidationOfFiles(s); 
      } 
    } 

} 
+0

我的答案有点晚,但对于那些有兴趣知道如何链接UI和逻辑的人可以看看我的解决方案。 – user3437460 2018-02-13 11:50:23

回答

0

你可以尝试做一个匿名AbstractAction:

panel.add(new JButton(new AbstractAction("name of button") { 
    public void actionPerformed(ActionEvent e) { 
     //do stuff here 
    } 
})); 
+0

我得到了以下错误: 异常线程 “main” 显示java.lang.NullPointerException \t在BlueBlueMainFiles.WindowFrameDimentions.buildPanel(WindowFrameDimentions.java:67) \t在BlueBlueMainFiles.WindowFrameDimentions.windowFrameDimentions(WindowFrameDimentions.java:45) at BlueBlueMainFiles.BlueBlueTheMain.main(BlueBlueTheMain.java:20) 拾取JAVA_TOOL_OPTIONS:-Djava.vendor =“Sun Microsystems Inc.” – joshua 2014-10-17 15:46:19

0

也希望这应该work..try进口包TheValidationFilesWindowFrameDimentions

然后代码为actionlistener

calcButton.addActionListener(new ActionListener(){ 
    public void actionPerformed(ActionEvent ae){ 
    ValidationOfNumbers von=new ValidationOfNumbers(); 
    von.GetValidationOfNumbers(boolean value,string); 
    }}); 
0

在我看来,你正试图实现类似于MVC模式的东西。在这种情况下,

  • 你的验证类将被视为模型(逻辑和数据)
  • 你镜框类充当视图(呈现/用户界面)
  • 主类充当Cnotroller (中间人模型和视图)

请注意,模型和视图不应该知道彼此的存在。他们通过控制器进行通信。

因此您的控制器(主类)应该持有视图(Frame类)和模型(Validation类)的参考:

//Your controller 
public class MainClass{ 
    private WindowFrameDimentions view; 
    private ValidationOfNumbers model; 
} 

现在关键的部分,以查看链接到你的控制器:因为你的观点 不处理的逻辑和实现,因此你不代码对于这个类按钮的动作侦听器的直接执行,而不是,只需添加一个方法来获得一个ActionListener:

//The view 
public class WindowFrameDimentions{ 
    private JButton calcButton; 
    private JTextField textField; //please use a better name for this 

    public WindowFrameDimentions(){ 
     //Initialize all other required attributes here.. 
     calcButton = new JButton("Calculate"); 
    } 

    //The controller will create a listener and add to calcButton 
    public void addCalcButtonListener(ActionListener listener){ 
     calcButton.addActionListener(listener) 
    } 

    //You need a getter for all the input fields such as your JTextFields 
    public String getInput(){ 
     textField.getText(); 
    } 
} 

为了您的验证类,它会只是一个简单的方法来验证这样的:

//The model 
public class ValidationOfNumbers{ 

    public ValidationOfNumbers(){ 
     //Initialize all other required attributes here.. 
    } 

    public boolean validationPassed(String input){ 
     //your validation code goes here.. 
    } 
} 

现在,所有3类连接在一起,你有:

//The controller 
public class MainClass{ 
    private WindowFrameDimentions view; 
    private ValidationOfNumbers model; 

    public static void main(String[] args){ 
     view = new WindowFrameDimentions(); 
     model = new ValidationOfNumbers(); 
     view.addCalcButtonListener(new ActionListener(){ 
      @Override 
      public void actionPerformed(ActionEvent e){ 
       //Now, we can use the Validation class here 
       if(model.validationPassed(view.getInput())){ //Linking Model to View 
        //If validation passes, do this 
       } 
       //Any other actions for calcButton will be coded here 
      } 
     }); 
    } 
} 

这是连接所有3类的总体思路。通常,在实现MVC时,我会有4个类而不是3个,而另外有1个类驱动代码。但在这个例子中,我使用Controller类来驱动代码。

另请注意,实际上应该扩展到JPanel而不是JFrame,然后将扩展类的实例添加到JFrame中。

相关问题