簡體   English   中英

JOptionPane 和圖形

[英]JOptionPane and Graphs

我想知道如何讓 JOptionPane 能夠滾動直方圖。 我設法讓 JOptionPane 顯示我的直方圖,唯一的問題是你不能滾動它。 窗口延伸到屏幕底部,只顯示圖形的一半。

注意:只有在讀取包含 300 多個 0 到 130 之間的整數的大型輸入文件時才會發生這種情況。程序將無法識別其他任何內容。

輸入值:

54 38 42 40 34 51 54 58 61 55 54 42 40 34 51 54 54 54 38 60 42 40 54 54 54 54 54 54 54 54 54 5 4 5 3 4 5 4 5 2 5 3 4 5 4 5 3 5 24 18 31 38 34 41 32 28 24 18 31 38 34 41 32 28 31 38 35 51 34 41 56 63 59 66 48 46 58 41 56 63 51 59 48 46 58 41 56 63 53 52 58 48 49 58 41 56 63 51 52 59 58 66 63 71 69 70 72 67 66 63 71 74 75 69 73 78 72 67 63 71 74 59 56 69 70 78 72 66 71 74 69 70 78 72 67 63 59 58 57 64 71 72 67 63 59 64 71 73 79 75 78 72 67 63 59 63 71 73 75 78 72 67 63 57 73 77 75 78 72 67 71 73 77 75 78 81 83 87 84 91 92 90 84 76 83 82 89 78 81 83 87 84 91 92 90 84 78 85 82 89 96 91 78 81 83 86 82 91 92 90 84 85 82 89 92 96 91 95 97 98 91 95 97 93 87 85 94 89 92 96 93 96 97 98 98 100 102 95 97 93 87 88 89 93 84 89 92 95 95 95 97 94 91 87 84 80 90 82 80 73 75 70 74 74 75 70 66 63 71 69 70 72 67 66 63 71 55 59 53 58 52 67 63 71 74 59 56 69 70 58 62 56 51 54 49 60 68 62 67 63 59 58 57 64 44 38 42 40 34 51 54 55 54 42 40 34 51 54 54 54 38 42 31 38 34 41 32 28 24 38 3 8 34 41 32 28 24 18 31 38 34 41 32 28 31 28 35 41 34 37 26 23 29 40 45 46 39 31 40 38 29 34 36

import javax.swing.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

public class CoolWeather
{
    static JFileChooser selecter;
    static Scanner in;
    public static void main(String[] args) throws FileNotFoundException
    {
       //Get Input File
       File inputFile;
       selecter = new JFileChooser(".");
       int status = selecter.showOpenDialog(null);
       if(status != JFileChooser.APPROVE_OPTION) 
       {
            JOptionPane.showMessageDialog(null, "Closing Program");
            System.exit(0);
       }
       inputFile = selecter.getSelectedFile();
       JOptionPane.showMessageDialog(null, "Opening: " + inputFile.getName());
       //Creates Array
       int[] temps = readData(inputFile);
       //Prints Histogram
       showMessage(temps);   
    }
    //The Following Method Creates and Populates An Array With Data
    //From The Input File
    public static int[] readData(File inputFile) throws FileNotFoundException
    {
       in = new Scanner(inputFile);
       in.useDelimiter("[^0-9/s]+");
       int[] temps = new int[131];
       int count = 0;
       int num;
       do
       {
           num = in.nextInt();
           count++;
           temps[num]++;
       } 
       while (in.hasNextInt());
       JOptionPane.showMessageDialog(null, "The Number Of Entries Read: " + count);
       return temps;    
    }
    public static void showMessage(int[] temps)
    { 
        String output = "Temp\tCount\tVisual";
        // for each array element, output a bar in histogram
        for ( int counter = 0; counter < temps.length; counter++ )
        {
            if (temps[counter] > 0)
            {
            output += "\n" + counter + "\t" + temps[ counter ] + "\t";
            // print bar of asterisks
            for ( int stars = 0; stars < temps[ counter ]; stars++ )
                {output += "*";}
            }
        } // end outer for
        JTextArea outputArea = new JTextArea();
        outputArea.setText( output );
        JOptionPane.showMessageDialog( null, outputArea, "CoolWeather Histogram", JOptionPane.INFORMATION_MESSAGE );
        System.exit( 0 );
    }
}

這是JOptionPane我使用的UI處理異常的時候,但原理是一樣的- JTextAreaJScrollPane

JLabel label = new JLabel(e.getLocalizedMessage() + ":");
label.setFont(getFont().deriveFont(Font.BOLD));
label.setAlignmentX(JLabel.LEFT_ALIGNMENT);

StringWriter writer = new StringWriter();
e.printStackTrace(new PrintWriter(writer));

JTextArea textArea = new JTextArea();
textArea.setEditable(false);
textArea.setFont(getFont());
textArea.setTabSize(2);
textArea.setText(writer.toString());
SwingUtilities.invokeLater(() -> textArea.setCaretPosition(0));

JScrollPane scrollPane = new JScrollPane(textArea);
scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
scrollPane.setPreferredSize(new Dimension(500, 200));

JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(label);
panel.add(Box.createVerticalStrut(5));
panel.add(scrollPane);

JOptionPane.showMessageDialog(this, panel, ApplicationInfo.getAppName(), JOptionPane.ERROR_MESSAGE);
    JTextArea outputArea = new JTextArea();
    outputArea.setText(output);
    outputArea.setEditable(false);
    JScrollPane scrollPane = new JScrollPane(outputArea);
    scrollPane.setAlignmentX(JScrollPane.LEFT_ALIGNMENT);
    scrollPane.setPreferredSize(new Dimension(500, 200));
    JOptionPane.showMessageDialog( null, scrollPane, "CoolWeather Histogram", JOptionPane.INFORMATION_MESSAGE);
    System.exit( 0 );

暫無
暫無

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

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