我正在尝试使用MVC模式构建我的第一个JAVA Swing应用程序。但我不会完全崩溃。特别是如何编码它。我在这个网站和其他网站上看了很多关于MVC的内容。 这是我读到的关于MVC的一件事: The MVC pattern and SWING
http://www.tutorialspoint.com/design_pattern/mvc_pattern.htm
我相当擅长开发,并希望从一开始就使用最佳实践。
我认为我对MVC模式的理解是你有一个 Controller 一个模型和一个 View 。 Controller 从 View 获取输入。然后 Controller 要求 Model 更改状态。获取 Model 的状态,并完成输入所需的每一项操作,例如将其存储到数据库或使用从数据库返回的内容更改 View 。
例如,如果按钮"添加地址"单击它将获取视图中文本字段的输入。 Controller 更新 Model 并将从 View 获取的数据保存到数据库中。如果按钮"搜索地址"单击它从视图中的文本字段获取输入搜索数据库,如果它获得数据,则更新模型并将其输出到视图
如果我想知道关于 MVC 的内容是对的,我该怎么编码呢?
是否可以使用我目前的代码对其进行解释?
如何从查看到控制器和模型的数据?
到目前为止,这是我的代码:
查看JFrame
public class WolffAddressManagerMVCJFrameVIEW extends javax.swing.JFrame {
/**
* Creates new form StoringAdresses1JFrame
*/
public WolffAddressManagerMVCJFrameVIEW() {
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
firstName = new javax.swing.JTextField();
lastName = new javax.swing.JTextField();
emailAdress = new javax.swing.JTextField();
searchEmailAdress = new javax.swing.JTextField();
removeEmailAddress = new javax.swing.JTextField();
street = new javax.swing.JTextField();
number = new javax.swing.JTextField();
additive = new javax.swing.JTextField();
zipCode = new javax.swing.JTextField();
city = new javax.swing.JTextField();
addAdress = new javax.swing.JButton();
removeAdress = new javax.swing.JButton();
searchAdress = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
showInputOutput = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Address Input Fields", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Trebuchet MS", 1, 14))); // NOI18N
jLabel1.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel1.setText("First Name:");
jLabel2.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel2.setText("Last Name:");
jLabel3.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel3.setText("Email Adress:");
jLabel4.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel4.setText("Street / Number / Add");
jLabel5.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel5.setText("Zipcode / City");
jLabel6.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel6.setText("Email Address:");
jLabel7.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
jLabel7.setText("Email Address:");
firstName.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
lastName.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
emailAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
street.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
number.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
additive.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
zipCode.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
city.setFont(new java.awt.Font("Trebuchet MS", 1, 11)); // NOI18N
addAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
addAdress.setText("Add Address");
ActionListener actionListener = new MainWindowActionListenerMVCVIEW(firstName, lastName, emailAdress, street, number, additive, zipCode, city, searchEmailAdress, removeEmailAddress);
addAdress.addActionListener(actionListener);
removeAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
removeAdress.setText("Remove Address");
removeAdress.setBackground(Color.RED);
removeAdress.addActionListener(actionListener);
searchAdress.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
searchAdress.setText("Search Address");
searchAdress.addActionListener(actionListener);
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(firstName)
.addComponent(lastName)
.addComponent(emailAdress)
.addComponent(searchEmailAdress)
.addComponent(removeEmailAddress)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(street, javax.swing.GroupLayout.PREFERRED_SIZE, 147, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(number, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(additive, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addGap(8, 8, 8))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(city)
.addContainerGap())))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(addAdress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(removeAdress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(searchAdress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(firstName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(lastName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(emailAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(street, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(number, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(additive, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(zipCode, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(city, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(41, 41, 41)
.addComponent(addAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(removeEmailAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(removeAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(searchEmailAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(18, 18, 18)
.addComponent(searchAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(203, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Show Input / Show Search", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Trebuchet MS", 1, 14))); // NOI18N
showInputOutput.setFont(new java.awt.Font("Trebuchet MS", 1, 14)); // NOI18N
jScrollPane1.setViewportView(showInputOutput);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jScrollPane1)
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
pack();
}
private javax.swing.JButton addAdress;
private javax.swing.JButton removeAdress;
private javax.swing.JButton searchAdress;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
public javax.swing.JTextArea showInputOutput;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField firstName;
private javax.swing.JTextField lastName;
private javax.swing.JTextField emailAdress;
private javax.swing.JTextField searchEmailAdress;
private javax.swing.JTextField removeEmailAddress;
private javax.swing.JTextField street;
private javax.swing.JTextField number;
private javax.swing.JTextField additive;
private javax.swing.JTextField zipCode;
private javax.swing.JTextField city;
}
查看ActionListener
public class MainWindowActionListenerMVCVIEW implements ActionListener {
public JTextField firstName, lastName, emailAddress, street, number, additive, zipCode, city, searchEmailAdress, removeEmailAddress;
public MainWindowActionListenerMVCVIEW(JTextField fName, JTextField lName, JTextField eAddress, JTextField streetName, JTextField houseNumber, JTextField add, JTextField zCode, JTextField cityName, JTextField searchEAdress, JTextField removeEAddress) {
this.firstName = fName;
this.lastName = lName;
this.emailAddress = eAddress;
this.street = streetName;
this.number = houseNumber;
this.additive = add;
this.zipCode = zCode;
this.city = cityName;
this.searchEmailAdress = searchEAdress;
this.removeEmailAddress = removeEAddress;
}
@Override
public void actionPerformed(ActionEvent e) {
firstName.getText();
lastName.getText();
emailAddress.getText();
street.getText();
number.getText();
additive.getText();
zipCode.getText();
city.getText();
searchEmailAdress.getText();
removeEmailAddress.getText();
System.out.println("test" + " " + firstName.getText()); // I put this in just te see if the *ActionListener* works
}
}
模型
public class WolffAddressManagerMVCMODEL {
public String firstName;
public String lastName;
public String emailAddress;
public String street;
public String number;
public String additive;
public String zipCode;
public String city;
}
控制器的
public class WolffAddressManagerMVCController {
public WolffAddressManagerMVCMODEL controllerModel;
public MainWindowActionListenerMVCVIEW controllerActionListener;
public WolffAddressManagerMVCJFrameVIEW controllerJFrame;
private Connection connect = null;
private final Statement statement = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
public ArrayList<String> queryResultSet = new ArrayList();
public void saveAddressToDataBase(MainWindowActionListenerMVCVIEW controller) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=password");
preparedStatement = connect.prepareStatement("SELECT email FROM dev.addresses WHERE email= '" + controller.emailAddress.getText() + "'");
resultSet = preparedStatement.executeQuery();
if(resultSet.next()) {
JOptionPane.showMessageDialog(null, "Email already exist", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE);
} else {
preparedStatement = connect.prepareStatement("INSERT INTO dev.addresses (firstname, lastname, email, street, housenumber, additve, zipcode, city) VALUES(?, ?, ?, ?, ?, ?, ?, ?)");
preparedStatement.setString(1, controller.firstName.getText());
preparedStatement.setString(2, controller.lastName.getText());
preparedStatement.setString(3, controller.emailAddress.getText());
preparedStatement.setString(4, controller.street.getText());
preparedStatement.setString(5, controller.number.getText());
preparedStatement.setString(6, controller.additive.getText());
preparedStatement.setString(7, controller.zipCode.getText());
preparedStatement.setString(8, controller.city.getText());
preparedStatement.executeUpdate();
}
} catch (ClassNotFoundException | SQLException | HeadlessException e) {
throw e;
} finally {
close();
}
}
public void searchAddress(String emailAddress) throws Exception {
try {
Class.forName("com.mysql.jdbc.Driver");
// Setup the connection with the DB
connect = DriverManager.getConnection("jdbc:mysql://localhost/dev?" + "user=root&password=fiat128slc");
preparedStatement = connect.prepareStatement("SELECT * FROM dev.addresses WHERE email= '" + emailAddress + "'");
resultSet = preparedStatement.executeQuery();
if(resultSet.isBeforeFirst()) {
while(resultSet.next()) {
queryResultSet.add(resultSet.getString("firstname"));
queryResultSet.add(resultSet.getString("lastname"));
queryResultSet.add(resultSet.getString("email"));
queryResultSet.add(resultSet.getString("street"));
queryResultSet.add(resultSet.getString("housenumber"));
queryResultSet.add(resultSet.getString("additve"));
queryResultSet.add(resultSet.getString("zipcode"));
queryResultSet.add(resultSet.getString("city"));
}
} else {
JOptionPane.showMessageDialog(null, "Email doesn't exist", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE);
}
} catch (ClassNotFoundException | SQLException | HeadlessException e) {
throw e;
} finally {
close();
}
}
答案 0 :(得分:0)
我编辑了我的最后一个答案。我想我走在正确的轨道上。在我的MVC项目中使用getter和setter。我现在有一个负责查询的班级。
控制器的
public class WolffAddressManagerMVCController {
private final WolffAddressManagerMVCMODEL model;
private final WolffAddressManagerQueriesMVCSERVICES query;
public WolffAddressManagerMVCController(WolffAddressManagerMVCMODEL model) {
this.query = new WolffAddressManagerQueriesMVCSERVICES();
this.model = model;
}
public void buildView() throws Exception {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(WolffAddressManagerMVCJFrameVIEW.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new WolffAddressManagerMVCJFrameVIEW().setVisible(true);
}
});
}
public void buildView2() throws Exception {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(WolffAddressManagerMVCUpdateJFrameVIEW.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new WolffAddressManagerMVCUpdateJFrameVIEW().setVisible(true);
}
});
}
public void addCredentials(WolffAddressManagerMVCJFrameVIEW view , WolffAddressManagerMVCMODEL model) throws Exception {
if(model.getEmailAddress().equals(model.getReTypeEmailAddress())) {
if(query.saveAddressToDataBase(view, model) == true) {
showCredentials(view);
}
else {
view.setTextArea(view.getTextArea() + "EMAIL ALREADY EXISTS\n\n");
}
} else {
JOptionPane.showMessageDialog(null, "Emailaddresses are not the same", "InfoBox: " + "Error", JOptionPane.INFORMATION_MESSAGE);
}
}
private void showCredentials(WolffAddressManagerMVCJFrameVIEW view) throws Exception {
DefaultListModel<String> jList = new DefaultListModel();
JList<String> list = new JList(jList);
model.setSearchResultArray(searchCredentials(model));
for(int i = 0; i < model.getSearchResultArray().size(); i++) {
jList.addElement(model.getSearchResultArray().get(i));
}
list.setModel(jList);
int size = list.getModel().getSize();
StringBuilder credentials = new StringBuilder();
for(int i = 0; i < size; i++) {
credentials.append(list.getModel().getElementAt(i)).append("\n");
}
view.setTextArea(view.getTextArea() + credentials.toString()+ "CREDENTIALS ADDED\n\n");
}
public void removeCredentials() throws Exception {
query.removeAddress(model.getEmailAddress());
}
public ArrayList<String> searchCredentials(WolffAddressManagerMVCMODEL model) throws Exception {
return query.searchAddress(model.getEmailAddress());
}
public void showSearchResult(WolffAddressManagerMVCJFrameVIEW view, WolffAddressManagerMVCMODEL model) {
DefaultListModel<String> jList = new DefaultListModel();
JList<String> list = new JList(jList);
try {
model.setSearchResultArray(searchCredentials(model));
} catch (Exception e) {
Logger.getLogger(MainWindowActionListenerSearchMVCVIEW.class.getName()).log(Level.SEVERE, null, e);
}
for(int i = 0; i < model.getSearchResultArray().size(); i++) {
jList.addElement(model.getSearchResultArray().get(i));
}
list.setModel(jList);
int size = list.getModel().getSize();
StringBuilder credentials = new StringBuilder();
for(int i = 0; i < size; i++) {
credentials.append(list.getModel().getElementAt(i)).append("\n");
}
view.setTextArea(view.getTextArea() + credentials.toString());
}
private void showUpdateResult(WolffAddressManagerMVCUpdateJFrameVIEW view, WolffAddressManagerMVCMODEL model) {
DefaultListModel<String> jList = new DefaultListModel();
JList<String> list = new JList(jList);
try {
model.getUpdateResultArray();
} catch (Exception e) {
Logger.getLogger(MainWindowActionListenerSearchMVCVIEW.class.getName()).log(Level.SEVERE, null, e);
}
for(int i = 0; i < model.getUpdateResultArray().size(); i++) {
jList.addElement(model.getUpdateResultArray().get(i));
}
list.setModel(jList);
int size = list.getModel().getSize();
StringBuilder credentials = new StringBuilder();
for(int i = 0; i < size; i++) {
credentials.append(list.getModel().getElementAt(i)).append("\n");
}
view.setTextArea(credentials.toString());
}
public void updateCredentials(WolffAddressManagerMVCUpdateJFrameVIEW view,WolffAddressManagerMVCMODEL model) throws Exception {
if(query.updateAddress(view, model) == true) {
showUpdateResult(view,model);
}
}
}
模型
public class WolffAddressManagerMVCMODEL {
private String updateEmailAddress;
private String companyName;
private String firstName;
private String lastName;
private String emailAddress;
private String reTypeEmailAddress;
private String street;
private String number;
private String additive;
private String zipCode;
private String city;
private JTextField jTextFieldUpdateEmailAddress;
private JTextField jTextFieldCompanyName;
private JTextField jTextFieldFirstName;
private JTextField jTextFieldLastName;
private JTextField jTextFieldEmailAddress;
private JTextField jTextFieldReTypeEmailAddress;
private JTextField jTextFieldStreetName;
private JTextField jTextFieldHouseNumber;
private JTextField jTextFieldAdditive;
private JTextField jTextFieldZipCode;
private JTextField jTextFieldCityName;
private JTextField jTextFieldSearchEmailAddress;
private JTextField jTextFieldRemoveEmailAddress;
private ArrayList<String> searchResultArray = null;
private ArrayList<String> updateResultArray = null;
public String getUpdateEmailAddress() {
return updateEmailAddress;
}
public void setUpdateEmailAddress(String updateEmailAddress) {
this.updateEmailAddress = updateEmailAddress;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public String getReTypeEmailAddress() {
return reTypeEmailAddress;
}
public void setReTypeEmailAddress(String reTypeEmailAddress) {
this.reTypeEmailAddress = reTypeEmailAddress;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getAdditive() {
return additive;
}
public void setAdditive(String additive) {
this.additive = additive;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public JTextField getJTextFieldUpdateEmailAddress() {
return jTextFieldUpdateEmailAddress;
}
public void setJTextFieldUpdateEmailAddress(JTextField updateEmailAddress) {
this.jTextFieldUpdateEmailAddress = updateEmailAddress;
}
public JTextField getJTextFieldCompanyName() {
return jTextFieldCompanyName;
}
public void setJTextFieldCompanyName(JTextField companyName) {
this.jTextFieldCompanyName = companyName;
}
public JTextField getJTextFieldFirstName() {
return jTextFieldFirstName;
}
public void setJTextFieldFirstName(JTextField fName) {
this.jTextFieldFirstName = fName;
}
public JTextField getJTextFieldLastName() {
return jTextFieldLastName;
}
public void setJTextFieldLastName(JTextField lastName) {
this.jTextFieldLastName = lastName;
}
public JTextField getJTextFieldEmailAddress() {
return jTextFieldEmailAddress;
}
public void setJTextFieldEmailAddress(JTextField emailAddress) {
this.jTextFieldEmailAddress = emailAddress;
}
public JTextField getJTextFieldReTypeEmailAddress() {
return jTextFieldReTypeEmailAddress;
}
public void setJTextFieldReTypeEmailAddress( JTextField reTypeEmailAddress) {
this.jTextFieldReTypeEmailAddress = reTypeEmailAddress;
}
public JTextField getJTextFieldStreet() {
return jTextFieldStreetName;
}
public void setJTextFieldStreet(JTextField streetName) {
this.jTextFieldStreetName = streetName;
}
public JTextField getJTextFieldHouseNumber() {
return jTextFieldHouseNumber;
}
public void setJTextFieldHouseNumber(JTextField houseNumber) {
this.jTextFieldHouseNumber = houseNumber;
}
public JTextField getJTextfieldAdditive() {
return jTextFieldAdditive;
}
public void setJTextFieldAdditive(JTextField additive) {
this.jTextFieldAdditive = additive;
}
public JTextField getJTextFieldZipCode() {
return jTextFieldZipCode;
}
public void setJTexFieldZipCode(JTextField zipCode) {
this.jTextFieldZipCode = zipCode;
}
public JTextField getJTextFieldCity() {
return jTextFieldCityName;
}
public void setJTextFieldCity(JTextField cityName){
this.jTextFieldCityName = cityName;
}
public JTextField getJTextFieldSearchEmailAddress(){
return jTextFieldSearchEmailAddress;
}
public void setJTextFieldSearchEmailAddress(JTextField searchEmailAddress) {
this.jTextFieldSearchEmailAddress = searchEmailAddress;
}
public JTextField getJTextFieldRemoveEmailAddress(){
return jTextFieldRemoveEmailAddress;
}
public void setJTextFieldRemoveEmailAddress(JTextField removeEAddress){
this.jTextFieldRemoveEmailAddress = removeEAddress;
}
public ArrayList<String> getSearchResultArray() {
return searchResultArray;
}
public void setSearchResultArray(ArrayList searchResultArray) {
this.searchResultArray = searchResultArray;
}
public ArrayList<String> getUpdateResultArray() {
return updateResultArray;
}
public void setUpdateResultArray(ArrayList searchResultArray) {
this.updateResultArray = searchResultArray;
}
}
这是我的一个ActionListener类,我总共有4个类在我的数据库上执行创建,读取,更新和删除。
public class MainWindowActionListenerAddMVCVIEW implements ActionListener {
WolffAddressManagerMVCMODEL model = new WolffAddressManagerMVCMODEL();
WolffAddressManagerMVCController controller = new WolffAddressManagerMVCController(model);
public MainWindowActionListenerAddMVCVIEW(JTextField fName, JTextField lName, JTextField eAddress, JTextField streetName, JTextField houseNumber, JTextField add, JTextField zCode, JTextField cityName) {
this.model.setJTextfiedFirstName(fName);
this.model.setJTextFieldLastName(lName);
this.model.setJTextFieldEmailAddress(eAddress);
this.model.setJTextFieldStreet(streetName);
this.model.setJTextFieldHouseNumber(houseNumber);
this.model.setJTextFieldAdditive(add);
this.model.setJTexFieldZipCode(zCode);
this.model.setJTextFieldCity(cityName);
}
@Override
public void actionPerformed(ActionEvent e) {
model.setFirstName(model.getJTextFieldFirstName().getText());
model.setLastName(model.getJTextFieldLastName().getText());
model.setEmailAddress(model.getJTextFieldEmailAddress().getText());
model.setStreet(model.getJTextFieldStreet().getText());
model.setNumber(model.getJTextFieldHouseNumber().getText());
model.setAdditive(model.getJTextfieldAdditive().getText());
model.setZipCode(model.getJTextFieldZipCode().getText());
model.setCity(model.getJTextFieldCity().getText());
try {
controller.saveAddressToDataBase(this);
} catch (Exception ex) {
Logger.getLogger(MainWindowActionListenerAddMVCVIEW.class.getName()).log(Level.SEVERE, null, ex);
}