第三周作业一

用户头像
dll
关注
发布于: 2020 年 10 月 12 日

1.请在草稿纸上手写一个单例模式的实现代码,拍照提交作业





2.请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。







代码如下:

public class Main {
public static void main(String[] args) {
WinForm winForm=new WinForm("window窗口",500, 550, 400, 500);
winForm.addComponent(new PictureMyComponent("logo图片",20, 10, 350, 100));
winForm.addComponent(new ButtonMyComponent("登录",50, 250, 120, 20));
winForm.addComponent(new ButtonMyComponent("注册",200, 250, 120, 20));

FrameMyComponent frame = new FrameMyComponent("FRAME1",200, 150, 350, 100);
frame.addComponent(new LableMyComponent("用户名",20, 130, 60, 25));
frame.addComponent(new TextBoxMyComponent("文本框",95, 130, 150, 25));
frame.addComponent(new LableMyComponent("密码",20, 154, 60, 25));
frame.addComponent(new PasswordBoxMyComponent("密码框",95, 170, 150, 25));
frame.addComponent(new CheckBoxMyComponent("复选框",20, 210, 20, 30));
frame.addComponent(new TextBoxMyComponent("记住用户名",50, 210, 80, 30));
frame.addComponent(new LinkLableMyComponent("忘记密码",170, 210, 80, 30));
winForm.addComponent(frame);
frame.print();

}
}
-----------------------------------------------------------------------------------

public abstract class MyComponent {
protected String name;
protected Container container;

public MyComponent(String name,int x, int y, int width, int height) {
this.name = name;
this.container=initContainer(name, x, y, width, height);
}

public abstract void print();

public abstract Container initContainer(String name,int x, int y, int width, int height);

}
--------------------------------------------------------------------------------
public abstract class MyContainer extends MyComponent {
private List<MyComponent> childComponent;

public MyContainer(String name,int x, int y, int width, int height) {
super(name, x, y, width, height);
this.childComponent = new ArrayList<>();
}


public void addComponent(MyComponent component) {
this.childComponent.add(component);
this.container.add(component.container);
}

public void print() {
for (MyComponent component : childComponent) {
component.print();
}

}

@Override
public Container initContainer(String name, int x, int y, int width, int height) {
JPanel jPanel = new JPanel();
jPanel.setBounds(x, y, width, height);
return jPanel;
}

}
--------------------------------------------------------------------------------------
public class WinForm extends MyContainer {
public WinForm(String name, int x, int y, int width, int height) {
super(name, x, y, width, height);
}
@Override
public Container initContainer(String name, int x, int y, int width, int height) {
JFrame rootjFrame = new JFrame();
rootjFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
rootjFrame.setVisible(true);
rootjFrame.setTitle(name);
rootjFrame.setBounds(x, y, width, height);
return rootjFrame;
}

@Override
public void print() {
System.out.println(String.format("print Frame(%s)", this.name));
super.print();
}
}


--------------------------------------------------------------------------------------

public class ButtonMyComponent extends MyComponent {
public ButtonMyComponent(String name, int x, int y, int width, int height) {
super(name, x, y, width, height);
}

@Override
public Container initContainer(String name, int x, int y, int width, int height) {
JButton jButton = new JButton(name);
jButton.setBounds(x, y, width, height);
return jButton;
}

@Override
public void print() {
System.out.println(String.format("print Button(%s)", this.name));
}


}




发布于: 2020 年 10 月 12 日 阅读数: 13
用户头像

dll

关注

还未添加个人签名 2018.10.11 加入

还未添加个人简介

评论

发布
暂无评论
第三周作业一