我创建了一个使用 MVC 架构版本的程序。该代码的目的是抓取网页列表的 h1 标题并将结果返回到 JTable。
到目前为止,我的程序运行良好。它按照我想要的方式返回结果,但直到最后它才会更新表格。我希望它在结果出现时更新表格。我想以一种在我刚刚学习时考虑到最佳实践原则的方式来做到这一点。
我假设要根据需要更新它,我将不得不稍微更改我的代码。我不确定动态更新 GUI 的最佳方式(线程、观察者,还是其他东西?)。我什至不确定“这段代码应该放在我的 MVC 模式中的哪个位置?”这个问题。说得通?
无论如何,这是我的观点:
public class SearchView extends JFrame{
//Components
private JLabel selectElementLabel = new JLabel("Element Selector:");
private JTextField selectElement = new JTextField("h1");;
private JComboBox<String> selectLocale;
private DefaultTableModel tableModel = new DefaultTableModel();
private JTable resultTable = new JTable(tableModel);
private JLabel statusLabel;
private JButton runButton = new JButton("Run");
private JButton clearButton = new JButton("Clear");
private SearchModel s_model;
//Constructor
public SearchView(SearchModel model) {
//Set the Logic here(model)
s_model = model;
//Initialise Components here(model)
selectLocale = new JComboBox<>(s_model.getLocales());
selectLocale.setSelectedIndex(13);
//Layout Components
JPanel userInputPanel = new JPanel();
userInputPanel.setLayout(new BoxLayout(userInputPanel, BoxLayout.X_AXIS));
userInputPanel.add(selectElementLabel);
userInputPanel.add(selectElement);
userInputPanel.add(selectLocale);
tableModel.addColumn("Page");
tableModel.addColumn("Data");
resultTable.setFillsViewportHeight(true);
JScrollPane resultScroller = new JScrollPane(resultTable);
resultScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
resultScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
resultScroller.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel controlButtons = new JPanel();
controlButtons.setLayout(new FlowLayout(FlowLayout.RIGHT));
controlButtons.add(statusLabel = new JLabel(s_model.getState()));
controlButtons.add(clearButton);
controlButtons.add(runButton);
this.setTitle("Element Searcher");
this.add(BorderLayout.NORTH, userInputPanel);
this.add(BorderLayout.CENTER, resultScroller);
this.add(BorderLayout.SOUTH, controlButtons);
this.setExtendedState(Frame.MAXIMIZED_BOTH);
this.setMinimumSize(new Dimension(900, 600));
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void reset(){
tableModel.setRowCount(0);
}
String getSelectedElement(){
return selectElement.getText();
}
String getSelectedLocale(){
return selectLocale.getSelectedItem().toString();
}
void setResults(Object[] result){
tableModel.addRow(result);
}
void addRunListener(ActionListener run){
runButton.addActionListener(run);
}
void addClearListerner(ActionListener clear){
clearButton.addActionListener(clear);
}
}
控制器:
public class SearchController {
private SearchModel s_model;
private SearchView s_view;
public SearchController(SearchModel model, SearchView view) {
s_model = model;
s_view = view;
s_view.addRunListener(new RunListener());
s_view.addClearListerner(new ClearListener());
}
class RunListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String selectedLocale = null;
try {
selectedLocale = s_view.getSelectedLocale();
s_model.setPageList(selectedLocale);
for (String pageUrl : s_model.getPageList()){
s_view.setResults(s_model.getResults(pageUrl));
}
} catch (Exception e1) {
System.out.println(e1);
}
}
}
class ClearListener implements ActionListener{
public void actionPerformed(ActionEvent e){
s_model.reset();
s_view.reset();
}
}
}
最后是我的模型:
public class SearchModel {
//Constants
private static final String[] localeStrings = { "cs-cz", "da-dk", "de-at", "de-ch", "de-de", "el-gr", "en-ae", "en-au", "en-ca", "en-gb", "en-ie", "en-in", "en-nz", "en-us", "en-za", "es-cl", "es-co", "es-es", "es-mx", "fi-fi", "fr-be", "fr-ca", "fr-ch", "fr-fr", "hu-hu", "it-it", "ja-jp", "ko-kr", "nb-no", "nl-be", "nl-nl", "pl-pl", "pt-br", "pt-pt", "ru-ru", "sk-sk", "sv-se", "zh-hk", "zh-sg", "zh-tw" };
private static final String INITIAL_STATE = "idle";
private HashSet<String> pageList;
private Object[] scrapeResult;
private String locale = "en-us";
//Search State
private String searchState;
public SearchModel() {
reset();
}
public void setPageList(String loc){
locale = loc;
ScrapeXML scraper = new ScrapeXML(locale);
pageList = scraper.getUrls();
}
public void setResults(String page){
ScrapeElements scraper = new ScrapeElements(page, locale);
scrapeResult = scraper.getResults();
}
public void reset(){
searchState = INITIAL_STATE;
}
public String[] getLocales(){
return localeStrings;
}
public String getState(){
return searchState;
}
public HashSet<String> getPageList(){
return pageList;
}
public Object[] getResults(String page){
setResults(page);
return scrapeResult;
}
}
如果您对代码本身有任何意见或建议,请告诉我!
谢谢!