java编写Dialog类包含的内容可以包括:图标、标题、提示内容、确认按钮、取消按钮,关闭按钮等6个内容组成。并画出类图
时间: 2023-07-10 21:04:27 浏览: 124
以下是Java编写Dialog类的代码和类图:
```
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Dialog extends JDialog {
private JLabel iconLabel;
private JLabel titleLabel;
private JLabel messageLabel;
private JButton okButton;
private JButton cancelButton;
private JButton closeButton;
public Dialog(JFrame parent, String title, String message, String iconPath) {
super(parent, title, true);
// 设置对话框大小
setSize(400, 200);
// 设置对话框位置
setLocationRelativeTo(parent);
// 设置对话框布局
setLayout(new BorderLayout());
// 设置图标
ImageIcon icon = new ImageIcon(iconPath);
iconLabel = new JLabel(icon);
add(iconLabel, BorderLayout.WEST);
// 设置标题
titleLabel = new JLabel(title);
titleLabel.setFont(new Font("宋体", Font.BOLD, 16));
JPanel titlePanel = new JPanel();
titlePanel.add(titleLabel);
add(titlePanel, BorderLayout.NORTH);
// 设置提示内容
messageLabel = new JLabel(message);
JPanel messagePanel = new JPanel();
messagePanel.add(messageLabel);
add(messagePanel, BorderLayout.CENTER);
// 设置确认按钮
okButton = new JButton("确定");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
// 设置取消按钮
cancelButton = new JButton("取消");
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
// 设置关闭按钮
closeButton = new JButton("关闭");
closeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
}
});
// 设置按钮面板
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
if (parent != null) {
buttonPanel.add(cancelButton);
}
buttonPanel.add(okButton);
buttonPanel.add(closeButton);
add(buttonPanel, BorderLayout.SOUTH);
}
}
```
类图如下:
```
+-------------------+
| Dialog |
+-------------------+
| - iconLabel: JLabel|
| - titleLabel: JLabel|
| - messageLabel: JLabel|
| - okButton: JButton|
| - cancelButton: JButton|
| - closeButton: JButton|
+-------------------+
| + Dialog(JFrame, String, String, String)|
+-------------------+
```
其中,`JLabel`、`JButton`、`JPanel`、`Font`、`ImageIcon`、`ActionEvent`、`ActionListener`、`BorderLayout`和`FlowLayout`都是Java Swing中的类。
阅读全文