簡體   English   中英

來自JFileChooser的2個窗口中的Java直方圖

[英]Java histogram in 2 windows from JFileChooser

此刻的這段代碼允許我選擇多個文本文件,然后將其分析成條形圖。我的問題是,例如,當從JFileChooser中選擇2個文件時,條形圖一個接一個地打開,因此第一個將打開,然后在確定按下第二個會打開,我需要它們同時並排打開嗎? 如果有人可以給我任何指示,將不勝感激。

if ("Analyze Text File".equals(command)) {
    JFileChooser chooser = new JFileChooser();
    chooser.setMultiSelectionEnabled(true);
    if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
        File[] files = chooser.getSelectedFiles();
        for (File file : files) {
            try {
                BufferedReader reader = new BufferedReader(new FileReader(file));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line);
                }
                String text = sb.toString();
                Map<Integer, Integer> counts = getCounts(text);
                int width = counts.size() * BAR_WIDTH;
                int max = maxCount(counts);
                int height = max * INCREMENT + 100;
                int horizon = height - 25;
                HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);
                JOptionPane.showMessageDialog(null, panel);
                reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

JOptionPane用於阻止彈出窗口。 諸如錯誤消息之類的東西。出於您的目的,您可能應該依賴無模式的JDialog

正如Ray建議的那樣,只需使用JDialogs即可控制模態。 記住JDialog就像JFrame一樣工作,因此,如果您知道如何制作JFrame ,那么JDialog應該很容易。 但是使用JDialog您可以控制模式。

for (File file : files) {
    try {
        ...
        HistogramPanel panel = new HistogramPanel(width, counts, height, horizon);
        JDialog dialog = new JDialog();
        dialog.setModal(false);        <---- very important
        dialog.setLayout(new BorderLayout());
        dialog.add(panel);
        dialog.pack();
        dialog.setLocationByPlatform(true);
        dialog.setVisible(true);
    } catch (...)
}

暫無
暫無

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

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