Create a ViewGetBook class that extends View<ControllerGetBook> .The ViewGetBook shows the text field called t (where the user can type text) and a button. Use a grid layout manager to position the two components.The user can type in the text field t the name of a library user. When the user then clicks on the button, the action listener of the button must read the name of the library user that was typed in the text field (using the getText method of the text field) and must call the getBook method of the controller with that user name as argument. The getBook method of the controller returns a string as result which must then be displayed back to the user using a message dialog (using the showMessageDialog method of the JOptionPane class). The update method of the ViewGetBook class does nothing, because the ViewGetBook class does not graphically display any data from the library (the model). 完成符合以上要求的java代码
时间: 2023-07-15 17:12:45 浏览: 88
applet-program-that-Uses-three-text-boxes.rar_The Program
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class ViewGetBook extends View<ControllerGetBook> {
private JTextField t;
private JButton button;
public ViewGetBook(ControllerGetBook controller) {
super(controller);
setLayout(new GridLayout(2, 1));
// create text field
t = new JTextField();
add(t);
// create button
button = new JButton("Get Book");
add(button);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String userName = t.getText();
String book = getController().getBook(userName);
JOptionPane.showMessageDialog(null, book);
}
});
}
@Override
public void update() {
// do nothing
}
}
```
阅读全文