Java-实现mvc
我刚刚开始使用 MVC 模式制作 Java Swing 应用程序,到目前为止我发现它大大简化了我的工作并解决了很多问题。现在我有一些困扰我或不知道如何解决的问题:
这是我的 UI 视图:
package env.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;
import env.helper.*;
public class UI_View extends JFrame
{
private JPanel left = new JPanel(new MigLayout());
private JPanel center = new JPanel(new MigLayout());
private JPanel right = new JPanel(new MigLayout());
//LEFT PANEL
private Tower_Helper towers = new Tower_Helper();
private Tree_Helper modules = new Tree_Helper();
private JScrollPane mod_bar = new JScrollPane(modules);
//CENTER PANEL
private Browse_Helper browser = new Browse_Helper();
private JScrollPane browser_bar = new JScrollPane(browser);
//RIGHT PANEL - LABELS
private JLabel tower_name = new JLabel("No tower selected");
private JLabel shield = new JLabel("0");
private JLabel armor = new JLabel("0");
private JLabel em = new JLabel("0.0");
private JLabel th = new JLabel("0.0");
private JLabel kn = new JLabel("0.0");
private JLabel ex = new JLabel("0.0");
//RIGHT PANEL - CONTROLLS
private JProgressBar cap_bar = new JProgressBar();
private JProgressBar cpu_bar = new JProgressBar();
private int total = 0;
private int consumed = 0;
public UI_View()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(800, 600));
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setTitle("Environment Prototype");
this.setLayout(new MigLayout());
left.setMinimumSize(new Dimension(252, 540));
left.setMaximumSize(new Dimension(252, 37500));
left.add(towers, "growx, pushx, wrap");
left.add(mod_bar,"grow, push");
center.setMinimumSize(new Dimension(298, 540));
center.add(browser_bar, "grow, push");
right.setMinimumSize(new Dimension(250, 540));
right.setMaximumSize(new Dimension(250, 37500));
tower_name.setToolTipText("Double click to remove tower if modules have been removed.");
right.add(tower_name, "span, wrap");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel("Capacitor"), "wrap, span");
cap_bar.setMinimumSize(new Dimension(215, 40));
right.add(cap_bar, "wrap, span");
right.add(new JLabel("CPU"), "wrap, span");
cpu_bar.setMinimumSize(new Dimension(215, 40));
right.add(cpu_bar, "wrap, span");
right.add(new JLabel("Shield: "));
right.add(shield, "wrap");
right.add(new JLabel("Armor: "));
right.add(armor, "wrap");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel("EM: "));
right.add(em, "wrap");
right.add(new JLabel("TH: "));
right.add(th, "wrap");
right.add(new JLabel("KN: "));
right.add(kn, "wrap");
right.add(new JLabel("EX: "));
right.add(ex, "wrap");
this.add(left, "dock west");
this.add(center, "dock center");
this.add(right, "dock east");
}
//getter - JLabel tower name
public JLabel getTowerNameLabel()
{
return this.tower_name;
}
//listener - JLabel tower name
public void towerLabelListner(MouseListener event)
{
this.tower_name.addMouseListener(event);
}
//getter - JList browser
public JList getBrowserList()
{
return this.browser;
}
//listener - JList browser
public void browserListListener(MouseListener event)
{
this.browser.addMouseListener(event);
}
//getter - JTree modules
public JTree getModuleTree()
{
return this.modules;
}
//listener - JTree modules
public void modulesTreeListener(MouseListener event)
{
this.modules.addMouseListener(event);
}
//getter - JComboBox towers
public JComboBox getTowers()
{
return this.towers;
}
//lisetner - JComboBox towers
public void towersBoxListener(ActionListener evnet)
{
this.towers.addActionListener(evnet);
}
}
这是我的 UI 控制器
package env.controller;
import env.model.UI_Model;
import env.view.UI_View;
import java.awt.event.*;
import javax.swing.*;
public class UI_Controller
{
private UI_Model ui_model;
private UI_View ui_view;
public UI_Controller(UI_Model model, UI_View view)
{
ui_model = model;
ui_view = view;
ui_view.towersBoxListener(new towerSelectionListener());
ui_view.modulesTreeListener(new modulesSelectionListener());
ui_view.browserListListener(new browserSelectionListener());
ui_view.towerLabelListner(new towerRemovalListener());
}
class towerRemovalListener implements MouseListener
{
@Override public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2 && ui_model.getModulesCounter() == 0)
{
ui_view.getTowerNameLabel().setText(ui_model.reset_label());
ui_model.retsetSelectionInfo();
} else if (ui_model.getModulesCounter() != 0){
JFrame dialog = new JFrame();
JOptionPane.showMessageDialog(dialog, "Cannot remove tower whit modules still selected.",
"Cannot remove tower !", JOptionPane.WARNING_MESSAGE);
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
class browserSelectionListener implements MouseListener
{
@Override public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2)
{
String selection = ui_view.getBrowserList().getSelectedValue().toString();
ui_model.removeFromList(selection);
ui_view.getBrowserList().setModel(ui_model.getSelectedModules());
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
class modulesSelectionListener implements MouseListener
{
@Override public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2 && ui_model.getSelectionInfo() != null)
{
String output = ui_view.getModuleTree().getLastSelectedPathComponent().toString();
ui_model.addToList(output);
ui_view.getBrowserList().setModel(ui_model.getSelectedModules());
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
class towerSelectionListener implements ActionListener
{
@Override public void actionPerformed(ActionEvent e)
{
String output = ui_view.getTowers().getSelectedItem().toString();
if(ui_model.getSelectionInfo() == null)
{
if(ui_model.validateTower(output))
{
ui_model.setSelectionInfo(output);
ui_view.getTowerNameLabel().setText(ui_model.getSelectionInfo());
}
} else {
JFrame dialog = new JFrame();
JOptionPane.showMessageDialog(dialog, "There is a already selected tower: "
+ ui_model.getSelectionInfo(), "Cannot select a new tower !",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
这是我的 UI 模型:
package env.model;
import javax.swing.*;
public class UI_Model
{
private String is_tower_selected = null;
private DefaultListModel selected_modules = new DefaultListModel();
private int module_counter = 0;
private final String reset_tower_name = "No tower selected";
private int shield = 0;
private int armor = 0;
private double em = 0.0;
private double th = 0.0;
private double kn = 0.0;
private double ex = 0.0;
public String reset_label()
{
return this.reset_tower_name;
}
public int getModulesCounter()
{
return this.module_counter;
}
public UI_Model()
{
}
public DefaultListModel getSelectedModules()
{
return this.selected_modules;
}
public void removeFromList(String input)
{
this.selected_modules.removeElement(input);
this.module_counter -= 1;
}
public void addToList(String output)
{
if(validateInput(output) == true)
{
this.selected_modules.addElement(" " + output);
this.module_counter += 1;
}
}
public String getSelectionInfo()
{
return this.is_tower_selected;
}
public void setSelectionInfo(String tower)
{
this.is_tower_selected = tower;
}
public void retsetSelectionInfo()
{
this.is_tower_selected = null;
}
public void outputSelection(String output)
{
System.out.println(output);
}
public boolean validateTower(String input)
{
if(input.equals("Select a Control Tower "))
{
return false;
}
return true;
}
public boolean validateInput(String input)
{
if(input.contains("<html>") || input.equals("Starbase Modules"))
{
return false;
}
return true;
}
}
如果您到目前为止,非常感谢 :) 现在回答问题:
我正在尝试将 JProgressBar 组件实现到我的视图中 创建了它的一个实例,但我对它是什么有点困惑 实现它的正确方法,因为元素被添加到 JList 组件 最大总量中消耗的部分 JProgressBar 将更新。
在项目开始时,我被告知要实现 Observable 类到我的视图,观察者到我的模型。然而我被告知 提供一些我当时没有的代码供参考。 好吧,现在我有一些代码,但是我不明白这个观察者/ 可观察的实施可以帮助我解决我的情况或惠及我的情况 JProgressBar 问题尤其突出。我可以得到一些建议吗 实现 O/O 如何在这段代码中帮助我。
因为我无法将 BorderFactory.createEmptyBorder 应用于 JLabel 在我的 JPanel 上的标签之间留出间距,我使用“right.add(new JLabel(""), "wrap, span")" 创建垂直间隙如何 我用这个代替。是否有一个预定义的方法可以设置 JPanel 元素之间的间隙还是我需要参考布局 Manager。
欢迎任何关于修复、更改或替换内容的提示,我知道我在这里放置了很多数据,但我正在努力正确地学习这一点,而且我没有其他地方可以参考。
I just started doing Java Swing applications using MVC pattern and so far i found it has simplified my work a lot and solved quite a few problems. Now i have a few questions that bug me or dont now how to resolve:
Here is my UI VIEW:
package env.view;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import net.miginfocom.swing.MigLayout;
import env.helper.*;
public class UI_View extends JFrame
{
private JPanel left = new JPanel(new MigLayout());
private JPanel center = new JPanel(new MigLayout());
private JPanel right = new JPanel(new MigLayout());
//LEFT PANEL
private Tower_Helper towers = new Tower_Helper();
private Tree_Helper modules = new Tree_Helper();
private JScrollPane mod_bar = new JScrollPane(modules);
//CENTER PANEL
private Browse_Helper browser = new Browse_Helper();
private JScrollPane browser_bar = new JScrollPane(browser);
//RIGHT PANEL - LABELS
private JLabel tower_name = new JLabel("No tower selected");
private JLabel shield = new JLabel("0");
private JLabel armor = new JLabel("0");
private JLabel em = new JLabel("0.0");
private JLabel th = new JLabel("0.0");
private JLabel kn = new JLabel("0.0");
private JLabel ex = new JLabel("0.0");
//RIGHT PANEL - CONTROLLS
private JProgressBar cap_bar = new JProgressBar();
private JProgressBar cpu_bar = new JProgressBar();
private int total = 0;
private int consumed = 0;
public UI_View()
{
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setMinimumSize(new Dimension(800, 600));
this.setExtendedState(JFrame.MAXIMIZED_BOTH);
this.setTitle("Environment Prototype");
this.setLayout(new MigLayout());
left.setMinimumSize(new Dimension(252, 540));
left.setMaximumSize(new Dimension(252, 37500));
left.add(towers, "growx, pushx, wrap");
left.add(mod_bar,"grow, push");
center.setMinimumSize(new Dimension(298, 540));
center.add(browser_bar, "grow, push");
right.setMinimumSize(new Dimension(250, 540));
right.setMaximumSize(new Dimension(250, 37500));
tower_name.setToolTipText("Double click to remove tower if modules have been removed.");
right.add(tower_name, "span, wrap");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel("Capacitor"), "wrap, span");
cap_bar.setMinimumSize(new Dimension(215, 40));
right.add(cap_bar, "wrap, span");
right.add(new JLabel("CPU"), "wrap, span");
cpu_bar.setMinimumSize(new Dimension(215, 40));
right.add(cpu_bar, "wrap, span");
right.add(new JLabel("Shield: "));
right.add(shield, "wrap");
right.add(new JLabel("Armor: "));
right.add(armor, "wrap");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel(""), "wrap, span");
right.add(new JLabel("EM: "));
right.add(em, "wrap");
right.add(new JLabel("TH: "));
right.add(th, "wrap");
right.add(new JLabel("KN: "));
right.add(kn, "wrap");
right.add(new JLabel("EX: "));
right.add(ex, "wrap");
this.add(left, "dock west");
this.add(center, "dock center");
this.add(right, "dock east");
}
//getter - JLabel tower name
public JLabel getTowerNameLabel()
{
return this.tower_name;
}
//listener - JLabel tower name
public void towerLabelListner(MouseListener event)
{
this.tower_name.addMouseListener(event);
}
//getter - JList browser
public JList getBrowserList()
{
return this.browser;
}
//listener - JList browser
public void browserListListener(MouseListener event)
{
this.browser.addMouseListener(event);
}
//getter - JTree modules
public JTree getModuleTree()
{
return this.modules;
}
//listener - JTree modules
public void modulesTreeListener(MouseListener event)
{
this.modules.addMouseListener(event);
}
//getter - JComboBox towers
public JComboBox getTowers()
{
return this.towers;
}
//lisetner - JComboBox towers
public void towersBoxListener(ActionListener evnet)
{
this.towers.addActionListener(evnet);
}
}
Here is my UI CONTROLLER
package env.controller;
import env.model.UI_Model;
import env.view.UI_View;
import java.awt.event.*;
import javax.swing.*;
public class UI_Controller
{
private UI_Model ui_model;
private UI_View ui_view;
public UI_Controller(UI_Model model, UI_View view)
{
ui_model = model;
ui_view = view;
ui_view.towersBoxListener(new towerSelectionListener());
ui_view.modulesTreeListener(new modulesSelectionListener());
ui_view.browserListListener(new browserSelectionListener());
ui_view.towerLabelListner(new towerRemovalListener());
}
class towerRemovalListener implements MouseListener
{
@Override public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2 && ui_model.getModulesCounter() == 0)
{
ui_view.getTowerNameLabel().setText(ui_model.reset_label());
ui_model.retsetSelectionInfo();
} else if (ui_model.getModulesCounter() != 0){
JFrame dialog = new JFrame();
JOptionPane.showMessageDialog(dialog, "Cannot remove tower whit modules still selected.",
"Cannot remove tower !", JOptionPane.WARNING_MESSAGE);
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
class browserSelectionListener implements MouseListener
{
@Override public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2)
{
String selection = ui_view.getBrowserList().getSelectedValue().toString();
ui_model.removeFromList(selection);
ui_view.getBrowserList().setModel(ui_model.getSelectedModules());
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
class modulesSelectionListener implements MouseListener
{
@Override public void mouseClicked(MouseEvent e)
{
if(e.getClickCount() == 2 && ui_model.getSelectionInfo() != null)
{
String output = ui_view.getModuleTree().getLastSelectedPathComponent().toString();
ui_model.addToList(output);
ui_view.getBrowserList().setModel(ui_model.getSelectedModules());
}
}
@Override public void mousePressed(MouseEvent e) {}
@Override public void mouseReleased(MouseEvent e) {}
@Override public void mouseEntered(MouseEvent e) {}
@Override public void mouseExited(MouseEvent e) {}
}
class towerSelectionListener implements ActionListener
{
@Override public void actionPerformed(ActionEvent e)
{
String output = ui_view.getTowers().getSelectedItem().toString();
if(ui_model.getSelectionInfo() == null)
{
if(ui_model.validateTower(output))
{
ui_model.setSelectionInfo(output);
ui_view.getTowerNameLabel().setText(ui_model.getSelectionInfo());
}
} else {
JFrame dialog = new JFrame();
JOptionPane.showMessageDialog(dialog, "There is a already selected tower: "
+ ui_model.getSelectionInfo(), "Cannot select a new tower !",
JOptionPane.WARNING_MESSAGE);
}
}
}
}
Here is my UI MODEL:
package env.model;
import javax.swing.*;
public class UI_Model
{
private String is_tower_selected = null;
private DefaultListModel selected_modules = new DefaultListModel();
private int module_counter = 0;
private final String reset_tower_name = "No tower selected";
private int shield = 0;
private int armor = 0;
private double em = 0.0;
private double th = 0.0;
private double kn = 0.0;
private double ex = 0.0;
public String reset_label()
{
return this.reset_tower_name;
}
public int getModulesCounter()
{
return this.module_counter;
}
public UI_Model()
{
}
public DefaultListModel getSelectedModules()
{
return this.selected_modules;
}
public void removeFromList(String input)
{
this.selected_modules.removeElement(input);
this.module_counter -= 1;
}
public void addToList(String output)
{
if(validateInput(output) == true)
{
this.selected_modules.addElement(" " + output);
this.module_counter += 1;
}
}
public String getSelectionInfo()
{
return this.is_tower_selected;
}
public void setSelectionInfo(String tower)
{
this.is_tower_selected = tower;
}
public void retsetSelectionInfo()
{
this.is_tower_selected = null;
}
public void outputSelection(String output)
{
System.out.println(output);
}
public boolean validateTower(String input)
{
if(input.equals("Select a Control Tower "))
{
return false;
}
return true;
}
public boolean validateInput(String input)
{
if(input.contains("<html>") || input.equals("Starbase Modules"))
{
return false;
}
return true;
}
}
Thanks a lot if you came so far : ) Now for the questions:
I am trying to implement JProgressBar component to my view i have
created an instance of it but i am a bit confused on what is the
proper way to implement it because as elements are added to JList
components the consumed part out of total maximum amount of
JProgressBar will be updated.At the start of the project i have been told to implement Observable
class to my view and Observer to my model. However i was told to
provide some code that i didnt have at that time for a reference.
Well now i have some code however i do not see how this Observer /
Observable implementation could help me in my case or whit my
JProgressBar problem in particular. Can i get some suggestions on
how implementing O/O can help me out in this code.Since i cannot apply BorderFactory.createEmptyBorder to JLabel to
make spacing between labels on my JPanel i use "right.add(new
JLabel(""), "wrap, span")" to create a vertical gap between how can
i substitute this. Is there a pre-defined method that can set the
gap between the JPanel elements or do i need to refer to the Layout
Manager.
Any tips on what to fix, change or replace are welcomed i know i putted a lot of data here but i am trying to learn this properly and i dont have another place to refer to.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你说
但这对我来说听起来很倒退。视图很像
Observer
,而模型就像Observable
。也许您理解错了?另外,检查一下
MouseAdapter
类——将其子类化而不是直接实现MouseListener
会删除很多无用的代码!You said
but that sounds quite backwards to me. A view is most like an
Observer
, and a model like anObservable.
Perhaps you got it wrong?Also, check out the class
MouseAdapter
-- subclassing it instead of implementingMouseListener
directly would cut out a lot of useless code!我不确定您在这里得到什么,除非您链接它们,否则它不会更新?
如果您在 UI_Model 类上扩展 Observable,请将 addToList() 更改为调用 setChanges(),然后调用 notificationObservers()。
然后在视图对象上实现 Observer,然后将视图作为观察者添加到控制器中的模型中:
I'm not sure what you're getting at here, unless you link them, it won't update?
If you extend Observable on the UI_Model class, change addToList() to call setChanges() then notifyObservers().
Then implement Observer on your view object, then add the view as an observer to the model in the controller: