当我实例化应该在包含另一个实例化类的main方法中打开JFrame的类时,JFrame无法打开,我不确定我做错了什么,因为没有语法错误。
感谢您的帮助。
泰加
获取数据类:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
/**
* This class get the data from the user in a .csv format and split it into data and variable.
* The class also turn the level data into returns
*/
/**
* @author
*/
public class GetData
{
private ArrayList<String> originalDataString = new ArrayList<String>(); //This would be used to get the data from file
private String dateTitle; //For date title
private String variableTitle; //For the title of the variable from your file.
private ArrayList<String> date = new ArrayList<String>(); //To store the date
private ArrayList<Double> variableLevel = new ArrayList<Double>(); //To store the content of the Level data
private ArrayList<Double> variableReturn = new ArrayList<Double>(); //To store the content of the Return data
/**
* The constructor opens a file to read the data
* @param filename The file to open
* @throws IOException
*/
public void getDataMethod() throws IOException
{
String filename = JOptionPane.showInputDialog("What is the name of the file you for the data?" +
"\nThe file must be saved as .csv(comma delimited)");
File file = new File(filename);
//Making sure that the file entered exist
if (!file.exists())
{
System.out.println("The file " + filename + " is not found");
System.exit(0);
}
//Getting the data from the file
Scanner inputFile = new Scanner(file);
String line; //To read the lines from the data
while (inputFile.hasNext())
{
line = inputFile.nextLine();
//Add each line to the ArrayList
originalDataString.add(line);
}
//This part split the date and the variable and put them into one array
String arrayLine = " "; // To add all the Strings in the originalDataString array.
String[] splitOriginalDataString; // This array stores the split data
for (int i = 0; i < originalDataString.size(); i++)
{
arrayLine += (originalDataString.get(i) + ",");
}
splitOriginalDataString = arrayLine.split(",");
//This part separates the data to dates and variable
dateTitle = splitOriginalDataString[0];
variableTitle = splitOriginalDataString[1];
for (int i = 2; i < splitOriginalDataString.length; i++)
{
if (i%2 == 0)
date.add(splitOriginalDataString[i]);
else
variableLevel.add(Double.parseDouble(splitOriginalDataString[i]));
}
//Getting the returns from the level data
variableReturn.add(0.0); //To account for the fact that the returns starts on the second month
for (int i = 0; i < variableLevel.size()-1; i++)
{
variableReturn.add(((variableLevel.get(i+1)/variableLevel.get(i))-1)*100);
}
// To ask the user if they want to check whether the Level data was imported correctly
String checkLevelData = JOptionPane.showInputDialog("Do you want to Check whether the data was imported correctly?" +
"\n" + "Choose: Y for Yes and N for No");
StringBuilder level = new StringBuilder();
level.append(dateTitle + " " + variableTitle);
level.append("\n" + "============");
for(int i = 0; i < date.size(); i++)
{
level.append("\n" + date.get(i) + " " + variableLevel.get(i));
}
if (checkLevelData.equalsIgnoreCase("Y"))
{
JOptionPane.showMessageDialog(null, level);
}
// To ask the user if they want to check whether the Level data was imported correctly
String checkReturnData = JOptionPane.showInputDialog("Do you want to check the return data?" +
"\n" + "Choose: Y for Yes and N for No");
StringBuilder Lreturn = new StringBuilder();
Lreturn.append(dateTitle + " " + variableTitle);
Lreturn.append("\n" + "============");
DecimalFormat formatter = new DecimalFormat("#0.00");
for(int i = 1; i < date.size(); i++)
{
Lreturn.append("\n" + date.get(i) + " " + formatter.format(variableReturn.get(i)));
}
if (checkReturnData.equalsIgnoreCase("Y"))
{
JOptionPane.showMessageDialog(null, Lreturn);
}
// Close the file
inputFile.close();
//Exiting the Message Dialog
System.exit(0);
}
public String getVariableTitle()
{
return variableTitle;
}
public String getDateTitle()
{
return dateTitle;
}
public ArrayList<String> getDate()
{
return date;
}
public ArrayList<Double> getVariableLevel()
{
return variableLevel;
}
public ArrayList<Double> getVariableReturn()
{
return variableReturn;
}
}
SummaryStatistics类:
import javax.swing.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
/**
* This class performs some summary statistics of the data.
*/
/**
* @author
*
*/
public class SummaryStatistics extends JFrame
{
private String dateTitle; //For date title
private String variableTitle; //For the title of the variable from your file.
private String[] date; //To store the date from GetData Class
private double[] variableLevel; //To store the content of the Level data from GetData Class
private double[] variableReturn; //To store the content of the Return data from GetData Class
private JPanel panel; //To reference a panel
private JLabel messageLabel; //To reference a label
private JButton avgLevelButton;
private JButton lowLevelButton;
private JButton highLevelButton;
private JButton varLevelButton;
private JButton sdLevelButton;
private JButton allLevelButton;
private JButton avgReturnButton;
private JButton lowReturnButton;
private JButton largeReturnButton;
private JButton varReturnButton;
private JButton sdReturnButton;
private JButton allReturnButton;
private final int WINDOW_WIDTH = 600; //Window width
private final int WINDOW_HEIGTH = 200; //Window height
//Constructor
public SummaryStatistics()
{
copyGetData();
//Set the window title
setTitle("Summary Statistics of " + variableTitle);
//set the size of the window
setSize(WINDOW_WIDTH, WINDOW_HEIGTH);
//Specify what happens when the close button is clicked
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Adding the buildPanel method to the frame
buildPanel();
//Adding the panel to the frame's content pane
add(panel);
setVisible(true);
}
/**
* This method copies the data and the data from the GetData Class
*/
public void copyGetData()
{
GetData data = new GetData();
//Get the dateTitle and variableTitle
dateTitle = data.getDateTitle();
variableTitle = data.getVariableTitle();
//Copy the date, varibaleLevel and variableReturn
date = new String[data.getDate().size()];
variableLevel = new double[data.getVariableLevel().size()];
variableReturn = new double[data.getVariableReturn().size()];
for (int i = 0; i < data.getDate().size(); i++)
{
date[i] = data.getDate().get(i);
}
for (int i = 0; i < data.getVariableLevel().size(); i++)
{
variableLevel[i] = data.getVariableLevel().get(i);
}
for (int i = 0; i < data.getVariableReturn().size(); i++)
{
variableReturn[i] = data.getVariableReturn().get(i);
}
}
/**
* This buildPanel method adds label and a buttons panel.
*/
private void buildPanel()
{
//Create label to display instructions
messageLabel = new JLabel("Press the buttom to get the summary statistics you want");
//Create the buttons to get the level summary statistics.
avgLevelButton = new JButton("Average of the Prices");
lowLevelButton = new JButton("Lowest Price");
highLevelButton = new JButton("Highest Price");
varLevelButton = new JButton("Variance of the Prices");
sdLevelButton = new JButton("Standard Deviation of the Prices");
allLevelButton = new JButton("All the Summary Statistics");
//Adding an action listener to the button
avgLevelButton.addActionListener(new ButtonListener());
//Create a panel object
panel = new JPanel();
//Add label and buttons to the panel
panel.add(messageLabel);
panel.add(avgLevelButton);
panel.add(lowLevelButton);
panel.add(highLevelButton);
panel.add(varLevelButton);
panel.add(sdLevelButton);
panel.add(allLevelButton);
}
/**
* Private inner class that handles the event when the user clicks a button
*/
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
//Get the action command
String actionCommand = e.getActionCommand();
//To create the display format
DecimalFormat formatter = new DecimalFormat("#0.00");
//Determine which button was clicked
//Level Statistics
if (actionCommand.equals("Average of the Prices"));
{
double sum = 0; //Accumulator to get the sum of the data for the average
for (int i = 0; i < variableLevel.length; i++)
{
sum += variableLevel[i];
}
JOptionPane.showMessageDialog(null, "The average of the prices of " + variableTitle + ":" +
"\n" + formatter.format(sum/variableLevel.length));
}
}
}
//TODO System.exit(0);
}
主要方法:
import java.io.IOException;
public class AnalyticsDemo {
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
//Creating an Instance to GetData class
GetData getData = new GetData();
getData.getDataMethod();
//Creating an Instance of SummaryStatistics Class
SummaryStatistics summary = new SummaryStatistics();
}
}
答案 0 :(得分:1)
在System.exit(0)
方法的末尾有一个getDataMethod
语句,这会导致JVM在您创建框架之前退出...