Java 如何在多个类中使用swing
我只是想知道一些关于秋千的事情 1) 如何在swing中使用MVC模型?Java 如何在多个类中使用swing,java,model-view-controller,swing,Java,Model View Controller,Swing,我只是想知道一些关于秋千的事情 1) 如何在swing中使用MVC模型? 2) 假设我有一个主窗口,我需要将菜单作为单独的类来执行,将所有组件作为单独的类来执行。哪种方法将是集成它的最佳方法?Swing具有简化MVC实现的内置机制。它有一个行动框架。 负责构建视图的类应该关注JComponent子类的实例化并将它们放置在面板上。应对用户活动作出反应的每个组件都应该有相应的操作(b.setAction(myAction))。 我通常创建包com.myapp.actions,并将所有操作放在那里。有
2) 假设我有一个主窗口,我需要将菜单作为单独的类来执行,将所有组件作为单独的类来执行。哪种方法将是集成它的最佳方法?Swing具有简化MVC实现的内置机制。它有一个行动框架。 负责构建视图的类应该关注JComponent子类的实例化并将它们放置在面板上。应对用户活动作出反应的每个组件都应该有相应的操作(
b.setAction(myAction)
)。
我通常创建包com.myapp.actions
,并将所有操作放在那里。有时我也会创建抽象动作,但它是特定于应用程序的。操作允许您将逻辑与表示层分离。将行动视为“模型”的切入点
典型应用程序的控件多于操作。某些控件重用相同的操作。例如,您可以通过键入Ctrl-S、单击菜单项或工具栏按钮、使用上下文菜单等来保存文件。但所有这些控件都将调用相同的操作SaveFileAction
关于你的第二个问题,有两种不同的方式。首先是基于继承。有人在需要框架时扩展JFrame,并将所有布局实现到这个特殊类中
另一种方法是创建一组生成布局的实用方法。我个人更喜欢这个。我认为,如果您确实需要某个对象的子类,那么应该使用继承,例如,当您希望重写某个超类方法时(例如,paint()
)
我希望我的描述能有所帮助。祝你好运。好的,这叫做“用过度技巧回答”,对此我深表歉意,但我举了一个简单的例子,尝试使用一个简单的MVC模式来做一件小事:按下按钮并更改JTextField中的文本。这太过分了,因为您只需几行代码就可以完成同样的事情,但它确实在单独的文件中演示了一些MVC以及模型如何控制状态。如果有什么令人困惑的地方,请提问 将所有内容放在一起并开始工作的主类:
import javax.swing.*;
public class SwingMvcTest {
private static void createAndShowUI() {
// create the model/view/control and connect them together
MvcModel model = new MvcModel();
MvcView view = new MvcView(model);
MvcControl control = new MvcControl(model);
view.setGuiControl(control);
// EDIT: added menu capability
McvMenu menu = new McvMenu(control);
// create the GUI to display the view
JFrame frame = new JFrame("MVC");
frame.getContentPane().add(view.getMainPanel()); // add view here
frame.setJMenuBar(menu.getMenuBar()); // edit: added menu capability
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
// call Swing code in a thread-safe manner per the tutorials
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
createAndShowUI();
}
});
}
}
视图类:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.*;
public class MvcView {
private MvcControl control;
private JTextField stateField = new JTextField(10);
private JPanel mainPanel = new JPanel(); // holds the main GUI and its components
public MvcView(MvcModel model) {
// add a property change listener to the model to listen and
// respond to changes in the model's state
model.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
// if the state change is the one we're interested in...
if (evt.getPropertyName().equals(MvcModel.STATE_PROP_NAME)) {
stateField.setText(evt.getNewValue().toString()); // show it in the GUI
}
}
});
JButton startButton = new JButton("Start");
startButton.addActionListener(new ActionListener() {
// all the buttons do is call methods of the control
public void actionPerformed(ActionEvent e) {
if (control != null) {
control.startButtonActionPerformed(e); // e.g., here
}
}
});
JButton endButton = new JButton("End");
endButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (control != null) {
control.endButtonActionPerformed(e); // e.g., and here
}
}
});
// make our GUI pretty
int gap = 10;
JPanel buttonPanel = new JPanel(new GridLayout(1, 0, gap, 0));
buttonPanel.add(startButton);
buttonPanel.add(endButton);
JPanel statePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
statePanel.add(new JLabel("State:"));
statePanel.add(Box.createHorizontalStrut(gap));
statePanel.add(stateField);
mainPanel.setBorder(BorderFactory.createEmptyBorder(gap, gap, gap, gap));
mainPanel.setLayout(new BorderLayout(gap, gap));
mainPanel.add(buttonPanel, BorderLayout.CENTER);
mainPanel.add(statePanel, BorderLayout.PAGE_END);
}
// set the control for this view
public void setGuiControl(MvcControl control) {
this.control = control;
}
// get the main gui and its components for display
public JComponent getMainPanel() {
return mainPanel;
}
}
控件:
import java.awt.event.ActionEvent;
public class MvcControl {
private MvcModel model;
public MvcControl(MvcModel model) {
this.model = model;
}
// all this simplistic control does is change the state of the model, that's it
public void startButtonActionPerformed(ActionEvent ae) {
model.setState(State.START);
}
public void endButtonActionPerformed(ActionEvent ae) {
model.setState(State.END);
}
}
模型使用PropertyChangeSupport对象来允许其他对象(在这种情况下是视图)侦听状态的更改。因此,模型实际上是我们的“可观察的”,而视图是“观察者”
一个简单的枚举State,用于封装State的概念:
public enum State {
NO_STATE("No State"), START("Start"), END("End");
private String text;
private State(String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
public String getText() {
return text;
}
}
编辑:我看到您也提到了菜单,所以我添加了菜单支持,添加了这个类,并在SwingMcvTest类中添加了几行。请注意,由于代码分离,对GUI进行此更改非常简单,因为菜单需要做的只是调用控制方法。它不需要了解模型或视图:
import java.awt.event.ActionEvent;
import javax.swing.*;
public class McvMenu {
private JMenuBar menuBar = new JMenuBar();
private MvcControl control;
@SuppressWarnings("serial")
public McvMenu(MvcControl cntrl) {
this.control = cntrl;
JMenu menu = new JMenu("Change State");
menu.add(new JMenuItem(new AbstractAction("Start") {
public void actionPerformed(ActionEvent ae) {
if (control != null) {
control.startButtonActionPerformed(ae);
}
}
}));
menu.add(new JMenuItem(new AbstractAction("End") {
public void actionPerformed(ActionEvent ae) {
if (control != null) {
control.endButtonActionPerformed(ae);
}
}
}));
menuBar.add(menu);
}
public JMenuBar getMenuBar() {
return menuBar;
}
}
上帝啊,这是很多代码来做一些琐碎的事情!我提名我自己和我的代码参加本周的stackoverflow Rube Goldberg大奖。在你的问题中发布一个示例程序,我们可以帮助你完成它。鼓舞人心的(一如既往):(纠正了一个拼写错误)不是Rube Goldberg;也许是松耦合,很好。我刚开始荡秋千……这会有帮助的。谢谢
import java.awt.event.ActionEvent;
import javax.swing.*;
public class McvMenu {
private JMenuBar menuBar = new JMenuBar();
private MvcControl control;
@SuppressWarnings("serial")
public McvMenu(MvcControl cntrl) {
this.control = cntrl;
JMenu menu = new JMenu("Change State");
menu.add(new JMenuItem(new AbstractAction("Start") {
public void actionPerformed(ActionEvent ae) {
if (control != null) {
control.startButtonActionPerformed(ae);
}
}
}));
menu.add(new JMenuItem(new AbstractAction("End") {
public void actionPerformed(ActionEvent ae) {
if (control != null) {
control.endButtonActionPerformed(ae);
}
}
}));
menuBar.add(menu);
}
public JMenuBar getMenuBar() {
return menuBar;
}
}