//-- V.java类,用来做MVC设计模式中的表现
import java.awt.Container;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SpringLayout;
public class V {
//Title
final private static String MENU_DLG_TITLE = "MVC";
// Frame
private JFrame frame = new JFrame(MENU_DLG_TITLE);
// Container
private Container con = frame.getContentPane();
Font font = new Font("test", 20, 20);
JLabel lable = new JLabel(font.getName());
JButton enlarge = new JButton("enlarge");
JButton abridge = new JButton("abridge");
public V() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
SpringLayout lay = new SpringLayout();
con.setLayout(lay);
lay.putConstraint(SpringLayout.WEST, lable, 120,SpringLayout.WEST, con);
lay.putConstraint(SpringLayout.NORTH, lable, 180, SpringLayout.NORTH, con);
lay.putConstraint(SpringLayout.WEST, enlarge, 80, SpringLayout.WEST, con);
lay.putConstraint(SpringLayout.NORTH, enlarge, 380, SpringLayout.NORTH, con);
lay.putConstraint(SpringLayout.WEST, abridge, 180, SpringLayout.WEST,con);
lay.putConstraint(SpringLayout.NORTH, abridge, 380, SpringLayout.NORTH, con);
con.add(lable);
con.add(enlarge);
con.add(abridge);
C c = new C(this, new M());
enlarge.addActionListener(c);
abridge.addActionListener(c);
frame.setBounds(520, 80, 300, 450);
frame.setVisible(true);
}
public JButton getEnlarge() {
return enlarge;
}
public void setEnlarge(JButton enlarge) {
this.enlarge = enlarge;
}
public JButton getAbridge() {
return abridge;
}
public void setAbridge(JButton abridge) {
this.abridge = abridge;
}
public JFrame getFrame() {
return frame;
}
public void setFrame(JFrame frame) {
this.frame = frame;
}
public Container getCon() {
return con;
}
public void setCon(Container con) {
this.con = con;
}
public JLabel getLable() {
return lable;
}
public void setLable(JLabel lable) {
this.lable = lable;
}
}
//-- C.java类,用来做MVC设计模式中的控制器
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class C implements ActionListener{
private V v ;
private M m;
public C(V v,M m){
this.v = v;
this.m = m;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == v.getEnlarge()) {
m.setSize(m.getSize() + 5);
m.setStyle(m.getStyle() + 5);
}else{
m.setSize(m.getSize() - 5);
m.setStyle(m.getStyle() - 5);
}
System.out.println("m.Style:" + m.getStyle());
System.out.println("m.Size:" + m.getSize());
Font font = new Font(v.getLable().getText(),m.getStyle(),m.getSize());
v.getLable().setFont(font);
}
public V getV() {
return v;
}
public void setV(V v) {
this.v = v;
}
public M getM() {
return m;
}
public void setM(M m) {
this.m = m;
}
}
//-- M.java 用来做MVC设计模式中的Medel
public class M {
private static final long serialVersionUID = 1L;
private int size = 20;
private int style = 20;
public M() {
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public int getStyle() {
return style;
}
public void setStyle(int style) {
this.style = style;
}
}
//-- Test.java 测试代码
public class Test {
public static void main(String[] args) {
M m = new M();
V v = new V();
new C(v,m);
}
}