2014-10-17 28 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中。

相關問題