第三周作业
发布于: 2020 年 06 月 23 日
Component.Java
abstract public class Component { protected void add(Component component){ throw new UnsupportedOperationException(); } protected void remove(Component component){ throw new UnsupportedOperationException(); } protected String getName(){ throw new UnsupportedOperationException(); } abstract public void print();}
LeafComponent.Java
public class LeafComponent extends Component { private String name; public LeafComponent(String name){ this.name = name; } public String getName(){ return name; } public void setName(String value){ name = value; } @Override public void print() { System.out.println("Print "+this.getClass().getSimpleName()+"("+getName()+")"); }}
FrameComponent.Java
import java.util.ArrayList;import java.util.List;public class FrameComponent extends Component{ private String name; private List<Component> componentList; public FrameComponent(String name){ this.name = name; componentList = new ArrayList<>(); } @Override public String getName(){ return name; } public void setName(String value){ name = value; } @Override public void add(Component component){ componentList.add(component); } @Override public void remove(Component component){ componentList.remove(component); } public void print() { System.out.println("Print "+this.getClass().getSimpleName()+"("+getName()+")"); for (Component component:componentList) { component.print(); } }}
Button.Java
public class Button extends LeafComponent { public Button(String name) { super(name); }}
CheckBox.Java
public class CheckBox extends LeafComponent { public CheckBox(String name) { super(name); }}
Frame.Java
public class Frame extends FrameComponent { public Frame(String name) { super(name); }}
Label.Java
public class Label extends LeafComponent { public Label(String name) { super(name); }}
LinkLabel.Java
public class LinkLabel extends Label { public LinkLabel(String name) { super(name); }}
PasswordBox.Java
public class PasswordBox extends TextBox { public PasswordBox(String name) { super(name); }}
Picture.Java
public class Picture extends LeafComponent { public Picture(String name) { super(name); }}
TextBox.Java
public class TextBox extends LeafComponent { public TextBox(String name) { super(name); }}
WinForm.Java
public class WinForm extends FrameComponent { public WinForm(String name) { super(name); }}
Test
@Test void testCompose(){ FrameComponent winform1 = new WinForm("WINDOWS窗体"); FrameComponent frame1 = new Frame("FRAME1"); winform1.add(new Picture("LOGO图片")); winform1.add(new Button("登录")); winform1.add(new Button("注册")); winform1.add(frame1); frame1.add(new Label("用户名")); frame1.add(new TextBox("文本框")); frame1.add(new Label("密码")); frame1.add(new PasswordBox("密码框")); frame1.add(new CheckBox("复选框")); frame1.add(new TextBox("记住用户名")); frame1.add(new LinkLabel("忘记密码")); winform1.print(); }
Result:
划线
评论
复制
发布于: 2020 年 06 月 23 日阅读数: 54
版权声明: 本文为 InfoQ 作者【王鑫龙】的原创文章。
原文链接:【http://xie.infoq.cn/article/3db9ac162e4c03188505d8f1a】。
本文遵守【CC BY-NC】协议,转载请保留原文出处及本版权声明。
王鑫龙
关注
还未添加个人签名 2018.02.04 加入
还未添加个人简介
评论