最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java?Swing實(shí)現(xiàn)自定義彈窗組件CusDialog(附源碼)

 更新時(shí)間:2026年05月22日 09:15:42   作者:RainCity  
Swing 原生對(duì)話框 JDialog 和 JFrame 一樣,標(biāo)題欄是系統(tǒng)自帶的,樣式陳舊且無(wú)法自定義,下面我們就來(lái)看看如何通過(guò)Java Swing實(shí)現(xiàn)自定義彈窗組件CusDialog吧

一、背景

Swing 原生對(duì)話框 JDialog 和 JFrame 一樣,標(biāo)題欄是系統(tǒng)自帶的,樣式陳舊且無(wú)法自定義。同時(shí),原生對(duì)話框不支持圓角、自定義標(biāo)題欄圖標(biāo)、拖拽移動(dòng)等功能。 CusDialog 的作用就是:移除系統(tǒng)裝飾,自繪標(biāo)題欄,實(shí)現(xiàn)與 CusFrame 風(fēng)格統(tǒng)一的彈窗。同時(shí)支持模態(tài)/非模態(tài)、可拖拽移動(dòng)、圓角邊框、遮罩層加載效果、回調(diào)事件等功能。

二、類(lèi)源碼

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.json.JSONObject;
import lombok.Getter;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 1. 自定義 Dialog 彈窗
 2. 實(shí)現(xiàn)圓角彈窗和自定義標(biāo)題欄,支持拖拽移動(dòng)、模態(tài)/非模態(tài)、遮罩層加載、回調(diào)事件
 3.  4. 使用示例:
 5. CusDialog dialog = new CusDialog(frame, "彈窗標(biāo)題");
 6. dialog.setSize(500, 400);
 7. dialog.showDialog();
 */
public class CusDialog extends JDialog {
    private static Integer WIDTH = 1300;
    private static Integer HEIGHT = 800;
    private final int cornerRadius = 15;
    private boolean isRoundEnabled = true;
    private JPanel titlePanel;
    private JLabel leftIcon;
    private JLabel titleLabel;
    private JLabel rightIcon;
    @Getter
    private JLabel closeButton;
    /** 關(guān)閉按鈕事件 */
    private Runnable closeAction;
    private Point initialClick;
    private boolean isDragEnabled = true;
    private JPanel contentPanel;
    private boolean paintBorder = true;
    private boolean isLoading = false;
    /** 父窗口引用,用于多顯示器支持 */
    private final Frame owner;
    /** 對(duì)話框關(guān)閉回調(diào)事件 */
    private Consumer<JSONObject> callBack;

    /**
     * 默認(rèn)構(gòu)造函數(shù)
     */
    public CusDialog() {
        this("");
    }

    /**
     * 指定父窗口的構(gòu)造函數(shù)
     * @param owner 父窗口
     */
    public CusDialog(Frame owner) {
        this(owner, "");
    }

    /**
     * 帶標(biāo)題的構(gòu)造函數(shù)
     * @param title 彈窗標(biāo)題
     * @param sizes 尺寸 [寬, 高]
     */
    public CusDialog(String title, Integer... sizes) {
        super((Frame) null, title, true);
        if (ArrayUtil.isNotEmpty(sizes)) {
            if (sizes.length == 1) WIDTH = sizes[0];
            if (sizes.length > 1) {
                WIDTH = sizes[0];
                HEIGHT = sizes[1];
            }
        }
        this.owner = null;
        setSize(WIDTH, HEIGHT);
        initialized();
    }

    /**
     * 指定父窗口和標(biāo)題的構(gòu)造函數(shù)
     * @param owner 父窗口
     * @param title 彈窗標(biāo)題
     */
    public CusDialog(Frame owner, String title) {
        super(owner, title, true);
        this.owner = owner;
        isLoading = false;
        setSize(WIDTH, HEIGHT);
        initialized();
        addWindowFocusListener(new WindowAdapter() {
            @Override
            public void windowGainedFocus(WindowEvent e) {
                repaint();
            }
            @Override
            public void windowLostFocus(WindowEvent e) {
                repaint();
            }
        });
    }

    /**
     * 初始化彈窗
     */
    private void initialized() {
        setUndecorated(true);
        // 標(biāo)題欄容器
        titlePanel = createTitlePanel();
        // 左側(cè)區(qū)域(圖標(biāo)+標(biāo)題+右側(cè)圖標(biāo))
        JPanel leftArea = new JPanel(new FlowLayout(FlowLayout.LEFT, 5, 8));
        leftArea.setOpaque(false);
        // 左側(cè)圖標(biāo)
        leftIcon = new JLabel();
        leftArea.add(leftIcon);
        // 標(biāo)題文本
        titleLabel = new JLabel(getTitle());
        titleLabel.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16));
        leftArea.add(titleLabel);
        // 標(biāo)題右側(cè)圖標(biāo)
        rightIcon = new JLabel();
        leftArea.add(rightIcon);
        titlePanel.add(leftArea, BorderLayout.WEST);
        // 關(guān)閉按鈕(保持在最右側(cè))
        closeButton = createCloseButton();
        titlePanel.add(closeButton, BorderLayout.EAST);
        // 中間彈性空間
        titlePanel.add(Box.createHorizontalGlue(), BorderLayout.CENTER);
        // 添加拖動(dòng)支持
        addDragListeners();
        // 布局設(shè)置
        getContentPane().add(titlePanel, BorderLayout.NORTH);
        contentPanel = createContentPanel();
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        // 設(shè)置對(duì)話框形狀為圓角矩形
        setRoundEnable(true);
    }

    /**
     * 獲取父窗口所在的屏幕設(shè)備
     * @return 屏幕設(shè)備
     */
    private GraphicsDevice getOwnerScreenDevice() {
        if (owner instanceof CusFrame) {
            return ((CusFrame) owner).getCurrentScreenDevice();
        } else if (owner != null) {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice[] screens = ge.getScreenDevices();
            Rectangle ownerBounds = owner.getBounds();
            int centerX = ownerBounds.x + ownerBounds.width / 2;
            int centerY = ownerBounds.y + ownerBounds.height / 2;
            for (GraphicsDevice screen : screens) {
                Rectangle screenBounds = screen.getDefaultConfiguration().getBounds();
                if (screenBounds.contains(centerX, centerY)) {
                    return screen;
                }
            }
        }
        return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
    }

    /**
     * 在父窗口屏幕居中顯示
     */
    private void setLocationToOwnerScreen() {
        GraphicsDevice ownerScreen = getOwnerScreenDevice();
        if (ownerScreen != null) {
            Rectangle screenBounds = ownerScreen.getDefaultConfiguration().getBounds();
            int x = screenBounds.x + (screenBounds.width - getWidth()) / 2;
            int y = screenBounds.y + (screenBounds.height - getHeight()) / 2;
            setLocation(x, y);
        }
    }

    /**
     * 創(chuàng)建標(biāo)題欄
     * @return 標(biāo)題欄面板
     */
    private JPanel createTitlePanel() {
        if (null == titlePanel) {
            titlePanel = new JPanel(new BorderLayout());
        }
        titlePanel.setOpaque(false);
        return titlePanel;
    }

    /**
     * 設(shè)置標(biāo)題欄下劃線(默認(rèn)顏色)
     */
    public void setUnderLine() {
        setUnderLine(new Color(0, 183, 195));
    }

    /**
     * 設(shè)置標(biāo)題欄下劃線顏色
     * @param color 下劃線顏色
     */
    public void setUnderLine(Color color) {
        setUnderLine(color, 1);
    }

    /**
     * 設(shè)置標(biāo)題欄下劃線
     * @param color 下劃線顏色
     * @param thickness 下劃線厚度
     */
    public void setUnderLine(Color color, int thickness) {
        titlePanel.setBorder(BorderFactory.createMatteBorder(0, 0, thickness, 0, color));
    }

    /**
     * 設(shè)置是否允許拖動(dòng)對(duì)話框
     * @param enabled true=允許拖動(dòng),false=禁止拖動(dòng)
     */
    public void setDragEnabled(boolean enabled) {
        if (this.isDragEnabled != enabled) {
            this.isDragEnabled = enabled;
            updateDragListeners();
        }
    }

    /**
     * 更新拖動(dòng)事件監(jiān)聽(tīng)器狀態(tài)
     */
    private void updateDragListeners() {
        MouseListener[] mouseListeners = titlePanel.getMouseListeners();
        MouseMotionListener[] motionListeners = titlePanel.getMouseMotionListeners();
        for (MouseListener listener : mouseListeners) {
            titlePanel.removeMouseListener(listener);
        }
        for (MouseMotionListener listener : motionListeners) {
            titlePanel.removeMouseMotionListener(listener);
        }
        if (isDragEnabled) {
            addDragListeners();
        }
    }

    /**
     * 添加拖動(dòng)事件監(jiān)聽(tīng)器
     */
    private void addDragListeners() {
        titlePanel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                initialClick = e.getPoint();
            }
        });
        titlePanel.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                if (isDragEnabled) {
                    int deltaX = e.getX() - initialClick.x;
                    int deltaY = e.getY() - initialClick.y;
                    setLocation(getX() + deltaX, getY() + deltaY);
                }
            }
        });
    }

    /**
     * 設(shè)置左側(cè)圖標(biāo)
     * @param icon 圖標(biāo)
     */
    public void setIcon(Icon icon) {
        leftIcon.setIcon(icon);
        leftIcon.setVisible(null != icon);
    }

    /**
     * 設(shè)置左側(cè)圖標(biāo)及點(diǎn)擊事件
     * @param icon 圖標(biāo)
     * @param runnable 點(diǎn)擊事件
     */
    public void setIcon(Icon icon, Runnable runnable) {
        leftIcon.setIcon(icon);
        leftIcon.setVisible(null != icon);
        addMouseClicked(leftIcon, runnable);
    }

    /**
     * 設(shè)置標(biāo)題右側(cè)圖標(biāo)
     * @param icon 圖標(biāo)
     */
    public void setRightIcon(Icon icon) {
        rightIcon.setIcon(icon);
        rightIcon.setVisible(null != icon);
    }

    /**
     * 設(shè)置標(biāo)題右側(cè)圖標(biāo)及點(diǎn)擊事件
     * @param icon 圖標(biāo)
     * @param runnable 點(diǎn)擊事件
     */
    public void setRightIcon(Icon icon, Runnable runnable) {
        rightIcon.setIcon(icon);
        rightIcon.setVisible(null != icon);
        addMouseClicked(rightIcon, runnable);
    }

    /**
     * 設(shè)置點(diǎn)擊事件
     * @param component 組件
     * @param runnable 點(diǎn)擊事件
     */
    private static void addMouseClicked(Component component, Runnable runnable) {
        component.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        component.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
            		// 詳情見(jiàn)第二篇
                CallbackProcessor.run(runnable);
            }
        });
    }

    /**
     * 創(chuàng)建關(guān)閉按鈕
     * @return 關(guān)閉按鈕
     */
    private JLabel createCloseButton() {
        JLabel close = new JLabel("×");
        close.setFont(new Font("Microsoft YaHei", Font.PLAIN, 32));
        close.setForeground(new Color(102, 102, 102));
        close.setBorder(BorderFactory.createEmptyBorder(-5, 10, 0, 10));
        addMouseClicked(close, () -> {
            if (null != closeAction) {
                closeAction.run();
            } else {
                dispose();
            }
        });
        return close;
    }

    /**
     * 關(guān)閉按鈕監(jiān)聽(tīng)事件
     * @param closeAction 關(guān)閉事件
     */
    public void addCloseAction(Runnable closeAction) {
        this.closeAction = closeAction;
    }

    /**
     * 修改關(guān)閉按鈕樣式
     * @param icon 圖標(biāo)
     */
    public void setCloseButtonStyle(Icon icon) {
        closeButton.setIcon(icon);
        closeButton.setText("");
        closeButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (null != closeAction) {
                    closeAction.run();
                } else {
                    dispose();
                }
            }
        });
    }

    /**
     * 關(guān)閉按鈕監(jiān)聽(tīng)事件(帶圖標(biāo))
     * @param icon 圖標(biāo)
     * @param runnable 關(guān)閉事件
     */
    public void addCloseAction(Icon icon, Runnable runnable) {
        if (null == icon) {
            closeButton.setText("×");
        } else {
            closeButton.setIcon(icon);
            closeButton.setText("");
        }
        this.closeAction = runnable;
    }

    /**
     * 設(shè)置標(biāo)題文字
     * @param title 標(biāo)題文字
     */
    @Override
    public void setTitle(String title) {
        super.setTitle(title);
        titleLabel.setText(title);
    }

    /**
     * 設(shè)置標(biāo)題文字和字體
     * @param title 標(biāo)題文字
     * @param font 字體
     */
    public void setTitle(String title, Font font) {
        super.setTitle(title);
        titleLabel.setText(title);
        titleLabel.setFont(font);
    }

    /**
     * 設(shè)置標(biāo)題字體
     * @param font 字體
     */
    public void setTitleFont(Font font) {
        titleLabel.setFont(font);
    }

    /**
     * 設(shè)置標(biāo)題欄樣式
     * @param operate 自定義操作
     */
    public void setTitleStyle(BiConsumer<JPanel, JLabel> operate) {
        if (null != operate) {
            operate.accept(titlePanel, titleLabel);
        }
    }

    /**
     * 創(chuàng)建內(nèi)容部分
     * @return 內(nèi)容面板
     */
    private JPanel createContentPanel() {
        if (null == contentPanel) {
            contentPanel = new JPanel(new BorderLayout());
        }
        contentPanel.setBackground(Color.WHITE);
        return contentPanel;
    }

    /**
     * 啟用圓角
     * @param enable 是否啟用圓角
     */
    private void setRoundEnable(boolean enable) {
        isRoundEnabled = enable;
        setShape(createShape(enable));
        repaint();
    }

    /**
     * 創(chuàng)建對(duì)話框形狀
     * @param enable 是否是圓角
     * @return 形狀
     */
    private Shape createShape(boolean enable) {
        return enable ? new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), cornerRadius, cornerRadius) : null;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        if (paintBorder) {
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g2d.setColor(new Color(0, 183, 195));
            g2d.setStroke(new BasicStroke(1));
            g2d.drawRoundRect(0, 0, getWidth() - 1, getHeight() - 1, cornerRadius, cornerRadius);
            g2d.dispose();
        }
    }

    @Override
    public void setSize(int width, int height) {
        super.setSize(width, height);
        SwingUtilities.invokeLater(() -> setShape(createShape(isRoundEnabled)));
    }

    @Override
    public Component add(Component comp) {
        return null == contentPanel ? super.add(comp) : contentPanel.add(comp);
    }

    @Override
    public Component add(String name, Component comp) {
        return null == contentPanel ? super.add(name, comp) : contentPanel.add(name, comp);
    }

    @Override
    public Component add(Component comp, int index) {
        return null == contentPanel ? super.add(comp, index) : contentPanel.add(comp, index);
    }

    @Override
    public void add(Component comp, Object constraints) {
        if (null == contentPanel) {
            super.add(comp, constraints);
        } else {
            contentPanel.add(comp, constraints);
        }
    }

    @Override
    public void add(Component comp, Object constraints, int index) {
        if (null == contentPanel) {
            super.add(comp, constraints, index);
        } else {
            contentPanel.add(comp, constraints, index);
        }
    }

    /**
     * 設(shè)置內(nèi)容面板邊框
     * @param border 邊框
     */
    public void setBorder(Border border) {
        if (null != contentPanel) {
            contentPanel.setBorder(border);
        }
    }

    /**
     * 打開(kāi)對(duì)話框,并在父窗口屏幕居中
     */
    public synchronized void showDialog() {
        if (!isLoading) {
            setLocationToOwnerScreen();
        }
        setVisible(true);
    }

    /**
     * 隱藏對(duì)話框
     */
    public void hideDialog() {
        setVisible(false);
    }

    /**
     * 關(guān)閉對(duì)話框(帶回調(diào)參數(shù))
     * @param jsonObject 回調(diào)參數(shù)
     */
    public void hideDialog(JSONObject jsonObject) {
        setVisible(false);
        // 詳情見(jiàn)第二篇
        CallbackProcessor.accept(callBack, jsonObject);
    }

    /**
     * 設(shè)置回調(diào)事件
     * @param callBack 回調(diào)事件
     * @return 當(dāng)前對(duì)話框?qū)嵗?
     */
    public CusDialog setCallBack(Consumer<JSONObject> callBack) {
        this.callBack = callBack;
        return this;
    }

    /**
     * 運(yùn)行后臺(tái)任務(wù)并顯示加載對(duì)話框
     * @param tip 提示語(yǔ)句
     * @param parentBounds 父窗口的邊界
     * @param doInBackground 后臺(tái)任務(wù)
     * @param doneAction 后臺(tái)任務(wù)完成回調(diào)
     */
    public <T> void loading(String tip, Rectangle parentBounds, Supplier<T> doInBackground, Consumer<T> doneAction) {
        loading(tip, parentBounds);
        // 詳情見(jiàn)第三篇
        ComponentUtils.handleSwingWorker(doInBackground, result -> {
            hideDialog();
            // 詳情見(jiàn)第二篇
            CallbackProcessor.accept(doneAction, result);
        });
        SwingUtilities.invokeLater(this::showDialog);
    }

    /**
     * 運(yùn)行后臺(tái)任務(wù)并顯示加載對(duì)話框(指定尺寸)
     * @param tip 提示語(yǔ)句
     * @param width 寬
     * @param height 高
     * @param parentBounds 父窗口的邊界
     * @param doInBackground 后臺(tái)任務(wù)
     * @param doneAction 后臺(tái)任務(wù)完成回調(diào)
     */
    public <T> void loading(String tip, int width, int height, Rectangle parentBounds, Supplier<T> doInBackground, Consumer<T> doneAction) {
        loading(tip, width, height, parentBounds);
        // 詳情見(jiàn)第三篇
        ComponentUtils.handleSwingWorker(doInBackground, result -> {
            hideDialog();
            // 詳情見(jiàn)第二篇
            CallbackProcessor.accept(doneAction, result);
        });
        SwingUtilities.invokeLater(this::showDialog);
    }

    /**
     * 打開(kāi)遮罩層(根據(jù)父窗口邊界)
     * @param tip 提示語(yǔ)句
     * @param parentBounds 父窗口的邊界
     */
    public void loading(String tip, Rectangle parentBounds) {
        setSize(parentBounds.width - 20, parentBounds.height - 80);
        initLoading(tip, parentBounds);
    }

    /**
     * 打開(kāi)遮罩層(指定尺寸)
     * @param tip 提示語(yǔ)句
     * @param width 寬
     * @param height 高
     * @param parentBounds 父窗口的邊界
     */
    public void loading(String tip, int width, int height, Rectangle parentBounds) {
        setSize(width, height);
        initLoading(tip, parentBounds);
    }

    /**
     * 初始化遮罩層
     * @param tip 提示語(yǔ)句
     * @param parentBounds 父窗口的邊界
     */
    public void initLoading(String tip, Rectangle parentBounds) {
        paintBorder = false;
        isLoading = true;
        setBackground(new Color(0, 0, 0, 0.5f));
        titlePanel.setVisible(false);
        contentPanel.setOpaque(false);
        // 創(chuàng)建進(jìn)度條
        JProgressBar progressBar = new JProgressBar();
        progressBar.setIndeterminate(true);
        progressBar.setString(tip);
        progressBar.setStringPainted(true);

        JPanel reservePanel = new JPanel(new BorderLayout());
        reservePanel.setOpaque(false);
        add(reservePanel, BorderLayout.CENTER);

        JPanel progressPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        progressPanel.setPreferredSize(new Dimension(getSize().width, getSize().height / 5 * 3));
        progressPanel.setOpaque(false);
        progressPanel.add(progressBar);
        add(progressPanel, BorderLayout.SOUTH);
        int x = parentBounds.x + (parentBounds.width - getWidth()) / 2;
        int y = parentBounds.y + (parentBounds.height - getHeight()) / 2 + 20;
        setLocation(x, y);
    }
}

三、核心功能說(shuō)明

彈窗裝飾:

  • setUndecorated(true):移除系統(tǒng)默認(rèn)標(biāo)題欄
  • setShape(new RoundRectangle2D.Double(...)):設(shè)置圓角矩形形狀
  • paint(Graphics g):繪制圓角邊框

自定義標(biāo)題欄:

  • 左側(cè):圖標(biāo) + 標(biāo)題文字 + 右側(cè)圖標(biāo)
  • 右側(cè):關(guān)閉按鈕
  • 支持拖拽移動(dòng)彈窗

標(biāo)題欄樣式:

  • setUnderLine():設(shè)置標(biāo)題欄下劃線
  • setTitleStyle():自定義標(biāo)題欄樣式
  • setIcon() / setRightIcon():設(shè)置標(biāo)題欄圖標(biāo)

遮罩層加載:

  • loading(String tip, Rectangle parentBounds, Supplier, Consumer):執(zhí)行后臺(tái)任務(wù)并顯示加載彈窗
  • 加載時(shí)顯示不確定進(jìn)度條,任務(wù)完成后自動(dòng)關(guān)閉
  • 彈窗背景半透明,形成遮罩效果

多屏幕適配:

  • getOwnerScreenDevice():檢測(cè)父窗口所在的屏幕
  • setLocationToOwnerScreen():在父窗口屏幕居中顯示

關(guān)閉回調(diào):

  • addCloseAction(Runnable):設(shè)置關(guān)閉按鈕點(diǎn)擊事件
  • setCallBack(Consumer):設(shè)置關(guān)閉時(shí)回調(diào),可傳遞參數(shù)

四、使用示例

4.1 創(chuàng)建基本彈窗

CusDialog dialog = new CusDialog(frame, "提示");
dialog.setSize(400, 300);
dialog.setTitle("操作確認(rèn)");
dialog.showDialog();

4.2 設(shè)置標(biāo)題欄樣式

dialog.setTitleStyle((titlePanel, titleLabel) -> {
    titlePanel.setBackground(new Color(50, 50, 80));
    titleLabel.setForeground(Color.WHITE);
});
dialog.setUnderLine(new Color(100, 150, 200), 2);

4.3 設(shè)置標(biāo)題欄圖標(biāo)

// 注:圖片獲取需自行實(shí)現(xiàn)
ImageIcon icon = xxx;
dialog.setIcon(icon);

4.4 設(shè)置關(guān)閉回調(diào)

dialog.addCloseAction(() -> {
    System.out.println("彈窗已關(guān)閉");
});

4.5 遮罩層加載效果

Rectangle bounds = frame.getBounds();
dialog.loading("正在加載數(shù)據(jù),請(qǐng)稍候...", bounds,
    () -> {
        // 后臺(tái)執(zhí)行耗時(shí)任務(wù)
        Thread.sleep(2000);
        return "加載完成";
    },
    result -> {
        // 任務(wù)完成,更新UI
        System.out.println(result);
    }
);

4.6 帶回調(diào)參數(shù)的關(guān)閉

JSONObject result = new JSONObject();
result.set("code", 200);
dialog.setCallBack(json -> {
    System.out.println("回調(diào)參數(shù):" + json);
});
dialog.hideDialog(result);

五、注意事項(xiàng)

  1. ComponentUtils 依賴(lài):loading 方法中使用了 ComponentUtils.handleSwingWorker,已在第3篇中介紹
  2. CallbackProcessor 依賴(lài):使用了 CallbackProcessor.run 和 CallbackProcessor.accept,已在第2篇中介紹
  3. 模態(tài)對(duì)話框:構(gòu)造函數(shù)中 super(owner, title, true) 第三個(gè)參數(shù)為 true 表示模態(tài)彈窗,會(huì)阻塞父窗口
  4. 遮罩層:loading 方法會(huì)隱藏標(biāo)題欄和內(nèi)容面板,顯示進(jìn)度條,適合用于加載中場(chǎng)景

六、與 CusFrame 的區(qū)別

特性CusFrameCusDialog
窗口類(lèi)型主窗口彈窗
模態(tài)不支持支持(可阻塞父窗口)
遮罩層加載不支持支持
關(guān)閉回調(diào)支持支持 + 回調(diào)參數(shù)
窗口調(diào)整大小支持不支持(固定大?。?/td>
最小化/最大化支持不支持
多屏幕適配支持支持

以上就是Java Swing實(shí)現(xiàn)自定義彈窗組件CusDialog(附源碼)的詳細(xì)內(nèi)容,更多關(guān)于Java Swing自定義組件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java使用String類(lèi)格式化當(dāng)前日期實(shí)現(xiàn)代碼

    Java使用String類(lèi)格式化當(dāng)前日期實(shí)現(xiàn)代碼

    這篇文章主要介紹了Java使用String類(lèi)格式化當(dāng)前日期實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2014-02-02
  • Java Lambda表達(dá)式詳解和實(shí)例

    Java Lambda表達(dá)式詳解和實(shí)例

    這篇文章主要介紹了Java Lambda表達(dá)式詳細(xì)介紹,從簡(jiǎn)單的到復(fù)雜的實(shí)例講解,需要的朋友可以參考下
    2014-05-05
  • 五分鐘解鎖springboot admin監(jiān)控新技巧

    五分鐘解鎖springboot admin監(jiān)控新技巧

    本文不會(huì)講如何搭建企業(yè)的運(yùn)維監(jiān)控系統(tǒng),有興趣的可以去找找成熟的比如Zabbix、Prometheus,甚至比較簡(jiǎn)單的Wgcloud都能滿(mǎn)足一定的需求,不在此贅述。本文講解如何使用Springboot admin對(duì)spring boot項(xiàng)目進(jìn)行應(yīng)用監(jiān)控,感興趣的朋友一起看看吧
    2021-06-06
  • 詳解java裝飾模式(Decorator Pattern)

    詳解java裝飾模式(Decorator Pattern)

    這篇文章主要為大家詳細(xì)介紹了java裝飾模式Decorator Pattern,這種類(lèi)型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它是作為現(xiàn)有的類(lèi)的一個(gè)包裝,對(duì)裝飾器模式感興趣的小伙伴們可以參考一下
    2016-04-04
  • SpringBoot整合RabbitMQ消息隊(duì)列的完整步驟

    SpringBoot整合RabbitMQ消息隊(duì)列的完整步驟

    這篇文章主要給大家介紹了關(guān)于SpringBoot整合RabbitMQ消息隊(duì)列的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Java Spring中Quartz調(diào)度器詳解及實(shí)例

    Java Spring中Quartz調(diào)度器詳解及實(shí)例

    這篇文章主要介紹了Java Spring中Quartz調(diào)度器詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • Java反射機(jī)制及Method.invoke詳解

    Java反射機(jī)制及Method.invoke詳解

    這篇文章主要介紹了Java反射機(jī)制及Method.invoke詳解,本文講解了JAVA反射機(jī)制、得到某個(gè)對(duì)象的屬性、得到某個(gè)類(lèi)的靜態(tài)屬性、執(zhí)行某對(duì)象的方法、執(zhí)行某個(gè)類(lèi)的靜態(tài)方法等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • SpringBoot配置開(kāi)發(fā)環(huán)境的詳細(xì)步驟(JDK、Maven、IDEA等)

    SpringBoot配置開(kāi)發(fā)環(huán)境的詳細(xì)步驟(JDK、Maven、IDEA等)

    文章介紹了如何配置SpringBoot開(kāi)發(fā)環(huán)境,包括安裝JDK、Maven和IDEA,并提供了詳細(xì)的步驟和配置方法,感興趣的朋友一起看看吧
    2024-12-12
  • MyBatis Plus大數(shù)據(jù)量查詢(xún)慢原因分析及解決

    MyBatis Plus大數(shù)據(jù)量查詢(xún)慢原因分析及解決

    大數(shù)據(jù)量查詢(xún)慢常因全表掃描、分頁(yè)不當(dāng)、索引缺失、內(nèi)存占用高及ORM開(kāi)銷(xiāo),優(yōu)化措施包括分頁(yè)查詢(xún)、流式讀取、SQL優(yōu)化、批處理、多數(shù)據(jù)源、結(jié)果集二次處理及配置調(diào)優(yōu)
    2025-09-09
  • 教你輕松制作java視頻播放器

    教你輕松制作java視頻播放器

    這篇文章主要為大家詳細(xì)介紹了如何編寫(xiě)屬于自己的java視頻播放器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-06-06

最新評(píng)論

泰宁县| 磴口县| 西青区| 包头市| 大名县| 重庆市| 延寿县| 武强县| 海林市| 蕉岭县| 英山县| 大余县| 新邵县| 钟祥市| 鄂州市| 理塘县| 华阴市| 富锦市| 海兴县| 高平市| 固安县| 时尚| 漠河县| 东海县| 五大连池市| 淮北市| 登封市| 左贡县| 金门县| 稷山县| 长沙县| 碌曲县| 眉山市| 伊春市| 澳门| 乐陵市| 河津市| 上林县| 满洲里市| 威宁| 仪征市|