私はMVCパターンを使って最初のJAVA Swingアプリケーションを構築しようとしています。しかし、私はそれをかなり把握していません。特にそれをコード化する方法ではありません。私はこのサイトやMVCに関する他のサイトで多くを読んでいます。 これは私がMVCについて読んものの一つです: The MVC pattern and SWINGMVCパターンに従おうとしています
http://www.tutorialspoint.com/design_pattern/mvc_pattern.htm
私が開発にかなり新しいですし、ベストプラクティスを使用して、最初から始めたいです。私はMVCパターンについて理解考える何
あなたはコントローラモデルとビューを持っています。 コントローラは、ビューから入力を取得します。次にコントローラモデルに状態を変更するように依頼します。 のモデルから状態を取得し、データベースに格納するか、のビューをデータベースから取得したものに変更するなど、入力と関連付ける必要があります。
たとえば、「アドレスの追加」ボタンをクリックすると、ビューのテキストフィールドから入力が取得されます。 コントローラはモデルを更新し、得られたデータをビューからデータベースに保存します。 「検索Addres」ボタンをクリックすると、のテキストフィールドから入力が得られます。表示はデータベースを検索し、データが戻ったらモデルを更新し、ビューに出力します。
私が知りたいと思うものが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();
}
}
はい、java bean class –