在软件开发中,有一种UI交互组件为对话框,用于和用户进行某些确认信息的交互,比如:警告信息、提示信息、确认信息等交互功能。在某个软件系统中,开发者把对话框组件命名为Dialog,经过产品需求分析和功能分析。现发现:对话框(Dialog)包含的内容可以包括:图标、标题、提示内容、确认按钮、取消按钮,关闭按钮等6个内容组成。在具体编程使用时,用户可以根据自己的需求,创建出符合自己需要的对话框。 请结合自己所学的设计模式内容,采用合理的设计模式完成上述需求,画出对应的设计模式的类图,并使用Java代码完成编码实践,
时间: 2023-12-03 13:44:09 浏览: 134
根据上述需求,可以采用建造者模式来构建对话框组件,该模式可以将复杂对象的构建过程和表示分离开来,使得同样的构建过程可以创建不同的表示。
对话框组件的建造者类:
```
public class DialogBuilder {
private ImageIcon icon;
private String title;
private String message;
private ActionListener confirmListener;
private ActionListener cancelListener;
private ActionListener closeListener;
public DialogBuilder setIcon(ImageIcon icon) {
this.icon = icon;
return this;
}
public DialogBuilder setTitle(String title) {
this.title = title;
return this;
}
public DialogBuilder setMessage(String message) {
this.message = message;
return this;
}
public DialogBuilder setConfirmListener(ActionListener listener) {
this.confirmListener = listener;
return this;
}
public DialogBuilder setCancelListener(ActionListener listener) {
this.cancelListener = listener;
return this;
}
public DialogBuilder setCloseListener(ActionListener listener) {
this.closeListener = listener;
return this;
}
public Dialog build() {
return new Dialog(icon, title, message, confirmListener, cancelListener, closeListener);
}
}
```
对话框组件类:
```
public class Dialog extends JDialog {
private JLabel iconLabel;
private JLabel titleLabel;
private JLabel messageLabel;
private JButton confirmButton;
private JButton cancelButton;
private JButton closeButton;
public Dialog(ImageIcon icon, String title, String message,
ActionListener confirmListener, ActionListener cancelListener, ActionListener closeListener) {
setModal(true);
setResizable(false);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(300, 200);
setLocationRelativeTo(null);
setIcon(icon);
setTitle(title);
setMessage(message);
setConfirmListener(confirmListener);
setCancelListener(cancelListener);
setCloseListener(closeListener);
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
c.anchor = GridBagConstraints.CENTER;
iconLabel = new JLabel();
panel.add(iconLabel, c);
c.gridx = 1;
c.gridy = 0;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
c.anchor = GridBagConstraints.WEST;
titleLabel = new JLabel();
panel.add(titleLabel, c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
c.anchor = GridBagConstraints.WEST;
messageLabel = new JLabel();
panel.add(messageLabel, c);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
c.anchor = GridBagConstraints.CENTER;
confirmButton = new JButton("确认");
panel.add(confirmButton, c);
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
c.anchor = GridBagConstraints.CENTER;
cancelButton = new JButton("取消");
panel.add(cancelButton, c);
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
c.gridheight = 1;
c.insets = new Insets(10, 10, 10, 10);
c.anchor = GridBagConstraints.CENTER;
closeButton = new JButton("关闭");
panel.add(closeButton, c);
setContentPane(panel);
}
public void setIcon(ImageIcon icon) {
if (icon != null) {
iconLabel.setIcon(icon);
}
}
public void setTitle(String title) {
if (title != null) {
titleLabel.setText(title);
}
}
public void setMessage(String message) {
if (message != null) {
messageLabel.setText(message);
}
}
public void setConfirmListener(ActionListener listener) {
if (listener != null) {
confirmButton.addActionListener(listener);
}
}
public void setCancelListener(ActionListener listener) {
if (listener != null) {
cancelButton.addActionListener(listener);
}
}
public void setCloseListener(ActionListener listener) {
if (listener != null) {
closeButton.addActionListener(listener);
}
}
}
```
使用示例:
```
Dialog dialog = new DialogBuilder()
.setIcon(new ImageIcon("icon.png"))
.setTitle("标题")
.setMessage("提示内容")
.setConfirmListener(e -> {
// 确认按钮点击事件处理
dialog.dispose();
})
.setCancelListener(e -> {
// 取消按钮点击事件处理
dialog.dispose();
})
.setCloseListener(e -> {
// 关闭按钮点击事件处理
dialog.dispose();
})
.build();
dialog.setVisible(true);
```
阅读全文