繁体   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