写点什么

第三周 代码重构 作业 「架构师训练营 3 期」

用户头像
feiyun123
关注
发布于: 2020 年 12 月 13 日

1、手写单例模式代码


2、组合模式使用

按要求窗口显示如下,没有使用图片,使用了背景色。

控制台按顺序打印内容如下:

代码如下:

package com.hyf.test;
import javax.swing.*;import java.awt.*;import java.util.ArrayList;import java.util.List;
/** * @author hyf * @date 2020/12/12 */public class WinForm { private String name; private String text; private List<WinForm> subList; private Object object;
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getText() { return text; }
public void setText(String text) { this.text = text; }
public void setSubList(List<WinForm> subList) { this.subList = subList; }
public Object getObject() { return object; }
public void setObject(Object object) { this.object = object; }
public WinForm(String name, String text, List<WinForm> subList, Object object) { this.name = name; this.text = text; this.subList = subList; this.object = object; }
public List<WinForm> getSubList() { return subList; }
public void add(WinForm component) { if (this.subList.size() == 0) { System.out.println("print " + this.text); } if (component.subList == null) { System.out.println("print " + component.text); }
subList.add(component);
}
public void remove(WinForm component) { subList.remove(component); }

public static void main(String[] args) { JFrame jFrame = new JFrame(); jFrame.setSize(500, 500); //setBounds setLayout布局器 两个不能同时使用 jFrame.setLayout(null); WinForm mainFrame = new WinForm("JFrame", "WinForm(WINDOW窗口)", new ArrayList<WinForm>(), jFrame);
Canvas canvas = new Canvas(); canvas.setBounds(100, 20, 300, 60); canvas.setBackground(new Color(255, 0, 0)); WinForm pic = new WinForm("Canvas", "Picture(LOGO图片)", null, canvas);
WinForm frame1 = new WinForm("JFrame", "Frame(FRAME1)", new ArrayList<WinForm>(), null);
JButton loginButton = new JButton(); loginButton.setText("登录"); loginButton.setBounds(100, 400, 100, 30); WinForm login = new WinForm("Button", "Button(登录)", null, loginButton);
JButton registerButton = new JButton(); registerButton.setText("注册"); registerButton.setBounds(250, 400, 100, 30); WinForm register = new WinForm("Button", "Button(注册)", null, registerButton);
mainFrame.add(pic); mainFrame.add(login); mainFrame.add(register);
mainFrame.add(frame1);
JLabel jLabel = new JLabel(); jLabel.setText("用户名"); jLabel.setBounds(100, 200, 100, 30); WinForm usernameLabel = new WinForm("JLabel", "Label(用户名)", null, jLabel);
JLabel jLabel2 = new JLabel(); jLabel2.setText("密码"); jLabel2.setBounds(100, 250, 100, 30); WinForm pwdLabel = new WinForm("JLabel", "Label(密码)", null, jLabel2);
JTextField userText = new JTextField(); userText.setBounds(200, 200, 200, 30); WinForm uText = new WinForm("JTextField", "TextBox(文本框)", null, userText);
JPasswordField pwdField = new JPasswordField(); pwdField.setBounds(200, 250, 200, 30); WinForm pText = new WinForm("JPasswordField", "PasswordBox(密码框)", null, pwdField);
JCheckBox checkBox = new JCheckBox(); checkBox.setBounds(100, 300, 30, 30); WinForm cbox = new WinForm("JCheckBox", "CheckBox(复选框)", null, checkBox);
JLabel remeberMeLabel = new JLabel(); remeberMeLabel.setText("记住用户名"); remeberMeLabel.setBounds(150, 300, 100, 30); WinForm rmbLabel = new WinForm("JLabel", "TextBox(记住用户名)", null, remeberMeLabel);
JLabel linkLabel = new JLabel(); linkLabel.setText("忘记密码"); linkLabel.setBounds(250, 300, 100, 30); WinForm lLabel = new WinForm("JLabel", "LinkLabel(忘记密码)", null, linkLabel);
frame1.add(usernameLabel); frame1.add(uText); frame1.add(pwdLabel); frame1.add(pText); frame1.add(cbox); frame1.add(rmbLabel); frame1.add(lLabel);

for (WinForm winForm : mainFrame.getSubList()) { if ("Button".equals(winForm.getName())) { jFrame.add((JButton) winForm.getObject()); } if ("Canvas".equals(winForm.getName())) { Canvas canvas1 = (Canvas) winForm.getObject(); jFrame.add(canvas1); } if ("JFrame".equals(winForm.getName())) {
} if (winForm.getSubList() != null) { for (WinForm winForm1 : winForm.getSubList()) { if ("JLabel".equals(winForm1.getName())) { jFrame.add((JLabel) winForm1.getObject()); } if ("JTextField".equals(winForm1.getName())) { jFrame.add((JTextField) winForm1.getObject()); } if ("JPasswordField".equals(winForm1.getName())) { jFrame.add((JPasswordField) winForm1.getObject()); } if ("JCheckBox".equals(winForm1.getName())) { jFrame.add((JCheckBox) winForm1.getObject()); } } } }
jFrame.setVisible(true); }}

复制代码


用户头像

feiyun123

关注

还未添加个人签名 2019.09.28 加入

还未添加个人简介

评论

发布
暂无评论
第三周 代码重构 作业 「架构师训练营 3 期」