簡體   English   中英

在 JOptionPane 內的 JScrollPane 上方添加文本

[英]Adding text above JScrollPane which is inside a JOptionPane

我有以下代碼:

            JTextArea textArea = new JTextArea(5, 120);
            textArea.setText("Error message more detail");
            textArea.setEditable(false);
            JScrollPane scrollPane = new JScrollPane(textArea);
            JOptionPane.showMessageDialog(ScenariosUploader.this, scrollPane, "Error Message", JOptionPane.ERROR_MESSAGE);  

這將創建以下 JOptionPane:

在此處輸入圖像描述

我的問題是如何在窗格上方添加類似“錯誤詳細信息”的文本?

與其將 scrollPane 提供給showMessageDialog方法,不如為其提供一個面板(使用BorderLayout) ,其中包含滾動窗格和“errorDetail”label:

public static void main(String[] args) {
    JTextArea textArea = new JTextArea(5, 120);
    textArea.setText("Error message more detail");
    textArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(textArea);

    JPanel panel = new JPanel(new BorderLayout());

    JLabel errorDetailLabel = new JLabel("Error detail:");
    panel.add(errorDetailLabel, BorderLayout.PAGE_START);
    panel.add(scrollPane, BorderLayout.CENTER);

    JOptionPane.showMessageDialog(null, panel, "Error Message", JOptionPane.ERROR_MESSAGE);
}

結果:

預習

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM