2015-11-06 33 views
1

我還是Java的初學者,這是我第一次嘗試使用MVC模型。到目前爲止,所有工作都成功了,並且我成功完成了兩個小例子。Java MVC按鈕不能調用函數

但是現在我在當前項目中出現了一個問題,按鈕單擊應該在數據庫中開始搜索。我已經測試了這一切,並調用方法來搜索我的主類作品中的數據庫,但試圖讓按鈕調用說funtion不會返回任何結果或錯誤。

筆者認爲:

import java.awt.FlowLayout; 
import javax.swing.JButton; 
import javax.swing.JFrame; 

public class View extends JFrame{ 

private static final long serialVersionUID = 1L; 

public static final String SEARCH = "SEARCH"; 

private JButton searchbutton = new JButton(); 

public View() { 
    this.setTitle("Betriebsnummersuche TBBBST"); 

    this.setDefaultCloseOperation(EXIT_ON_CLOSE); 

    this.setLayout(new FlowLayout()); 

    searchbutton.setText("Search"); 
    searchbutton.setActionCommand(View.SEARCH); 
    this.add(searchbutton); 

    this.setSize(600, 400); 
    setResizable(false); 
    //this.pack(); 
} 

public JButton getButton() { 
    return searchbutton; 
} 

} 

我的模型:

import java.io.IOException; 
import java.sql.SQLException; 
import java.util.Observable; 
import default.dbconnect.dao.impl.ResultsDaoImpl; 

public class Model extends Observable{ 

public void search() throws SQLException, IOException { 
    ResultsDaoImpl result1 = new ResultDaoImpl(); 
    result1.getResults(); 
} 
} 

我的控制器:

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.io.IOException; 
import java.sql.SQLException; 
import java.util.Observable; 
import java.util.Observer; 

public class Controller implements Observer, ActionListener{ 

private Model model; 
@SuppressWarnings("unused") 
private View view; 

public Controller(Model model, View view) { 
    this.model = model; 
    this.view = view; 

    model.addObserver(this); 

    view.getButton().addActionListener(this); 

    view.setVisible(true); 
} 

@Override 
public void actionPerformed(ActionEvent e) { 
    switch (e.getActionCommand()) { 
    case View.SEARCH: 
     try { 
      model.search(); 
     } catch (SQLException e1) { 
      System.out.println("A SQL-error occured: "); 
      e1.printStackTrace(); 
     } catch (IOException e1) { 
      System.out.println("An error occured: "); 
      e1.printStackTrace(); 
     } 
     break; 

    default: 
     System.out.println("Search error: " + e.getActionCommand()); 
     break; 
    } 

} 

@Override 
public void update(Observable o, Object arg) { 
    // TODO Auto-generated method stub 

} 

} 

和最後一個我公司主營:

import java.io.IOException; 
import java.sql.SQLException; 
import default.mvc.Controller; 
import default.mvc.Model; 
import default.mvc.View; 

public class Main { 

public static void main(String[] args) throws SQLException, IOException { 

    Model model = new Model(); 
    View view = new View(); 
    Controller controller = new Controller(model, view); 
} 

} 

有人可以告訴我我在做什麼錯嗎?就像我說的,搜索本身的工作,所以我假設我的錯誤有事情做與我的ActionListener我乙

編輯:代碼爲我ResultsDaoImpl類:

import java.io.IOException; 
import java.sql.ResultSet; 
import java.sql.SQLException; 
import java.util.ArrayList; 
import java.util.Collection; 
import de.drv.dsrv.betriebsnumemrsuchetbbbst.dao.ResultsDao; 
import de.drv.dsrv.betriebsnummersuchetbbbst.business.ResultsBean; 

public class ResultsDaoImpl extends AbstractDao implements ResultsDao { 
//Only show first 10 results, change later!!! 
private static final String AllResults = "SELECT BBSTBBNR, BBSTPLZ, BBSTNABEG FROM BP.TBBBST FETCH FIRST 10 ROWS ONLY"; 

public Collection<ResultsBean> getResults() throws SQLException, 
     IOException { 
    final Collection<ResultsBean> endresult = new ArrayList<ResultsBean>(); 

    ResultSet resultset = null; 
    try { 
     resultset = getResultset(AllResults); 

     // while loop for showing all data 
     while (resultset.next()) { 
      ResultsBean results = new ResultsBean(); 
      int resultid = resultset.getInt(1); 
      String resultplz = resultset.getString(2); 
      String resultname = resultset.getString(3); 
      ergebnis.add(results); 
      System.out.println("Results: " + resultid + " " + resultplz + " " + resultname); 
     } 

    } catch (SQLException e) { 

     e.printStackTrace(); 
     System.out.println("An error occurred processing SQL statement (getResults)"); 
    } catch (NullPointerException npe) { 
     System.out.println("NullPointerException"); 
    } finally { 

     closeConnection(resultset); 

    } 

    return endresult; 
} 

}

+0

嘗試在Controller.actionPerformed中的開關之前放置一個println,以確保它真的在那裏通過。 – StephaneM

+0

顯然它不會通過那裏。之前已經嘗試過(忘記提及),現在再次嘗試。所以它顯然不會調用actionPerformed函數。 – APL

+0

您應該將該println添加到問題中的代碼中,以顯示您已嘗試過它,並註釋它不會被調用。 – megaflop

回答

1

這部分有些問題:

ResultsDaoImpl result1 = new ResultDaoImpl();

請爲此提供代碼。調用堆棧是可以的。

+0

是的。當您嘗試查找錯誤時,請先將一些簡單的代碼寫入search()方法。 – zoigDev

+0

我編輯了問題並添加了我的ResultsDaoImpl類。 – APL

+0

在此之後添加一些System.out.println() resultset = getResultset(AllResults); 如果顯示它意味着您的ResultSet被複制並不好。 – zoigDev