For some reason, my final JOptionPane.sho wInputDialog is throwing an error when I try to compile, and I get some errors from the ActionPerformed area with an e.getSource. it is a swing array to take entered numbers and average them while also having the option to view the highest and lowest number. I am very new to this, and I am not sure where I went wrong.
here is the code. Thanks
here is the code. Thanks
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.text.DecimalFormat;
public class AverageGrade extends JFrame implements ActionListener
{
//construct components
JLabel sortPrompt = new JLabel("Sort by:");
JComboBox fieldCombo = new JComboBox();
JTextPane textPane = new JTextPane();
boolean firstRun = true;
int arrayLength;
double[] grades = new double[50];
DecimalFormat format = new DecimalFormat("##.##");
double maxGrade = Double.MIN_GRADE;
double minGrade = Double.MAX_GRADE;
//construct instance of AverageGrade
public AverageGrade()
{
super("Grade Averages");
}
//create the menu system
public JMenuBar createMenuBar()
{
//create an instance of the menu
JMenuBar mnuBar = new JMenuBar();
setJMenuBar(mnuBar);
//construct and populate the File menu
JMenu mnuFile = new JMenu("File");
mnuFile.setMnemonic(KeyEvent.VK_F);
mnuFile.setDisplayedMnemonicIndex(0);
mnuBar.add(mnuFile);
JMenuItem mnuFileExit = new JMenuItem("Exit");
mnuFileExit.setMnemonic(KeyEvent.VK_X);
mnuFileExit.setDisplayedMnemonicIndex(1);
mnuFile.add(mnuFileExit);
mnuFileExit.setActionCommand("Exit");
mnuFileExit.addActionListener(this);
JMenuItem mnuFileNewGrades = new JMenuItem("Insert New Grades");
mnuFileNewGrades.setMnemonic(KeyEvent.VK_I);
mnuFileNewGrades.setDisplayedMnemonicIndex(0);
mnuFile.add(mnuFileNewGrades);
mnuFileNewGrades.setActionCommand("NewGrades");
mnuFileNewGrades.addActionListener(this);
return mnuBar;
}
//create the content pane
public Container createContentPane()
{
//populate the JComboBox
fieldCombo.addItem("Highest Grade");
fieldCombo.addItem("Lowest Grade");
fieldCombo.addActionListener(this);
fieldCombo.setToolTipText("Click the drop-down arrow to display High/Low fields.");
//construct and populate the north panel
JPanel northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(sortPrompt);
northPanel.add(fieldCombo);
//create the JTextPane and center panel
JPanel centerPanel = new JPanel();
setTabsAndStyles(textPane);
textPane = addTextToTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scrollPane.setPreferredSize(new Dimension(500, 200));
centerPanel.add(scrollPane);
//create Container and set attributes
Container c = getContentPane();
c.setLayout(new BorderLayout(10,10));
c.add(northPanel,BorderLayout.NORTH);
c.add(centerPanel,BorderLayout.CENTER);
return c;
}
//method to create tab stops and set font styles
protected void setTabsAndStyles(JTextPane textPane)
{
//create Tap Stops
TabStop[] tabs = new TabStop[2];
tabs[0] = new TabStop(200, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
tabs[1] = new TabStop(350, TabStop.ALIGN_LEFT, TabStop.LEAD_NONE);
TabSet tabset = new TabSet(tabs);
//set Tab Style
StyleContext tabStyle = StyleContext.getDefaultStyleContext();
AttributeSet aset =
tabStyle.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.TabSet, tabset);
textPane.setParagraphAttributes(aset, false);
}
//method to add new text to the JTextPane
public JTextPane addTextToTextPane()
{
Document doc = textPane.getDocument();
try
{
StyleContext style = StyleContext.getDefaultStyleContext();
AttributeSet set = style.addAttribute(SimpleAttributeSet.EMPTY,StyleConstants.ALIGN_LEFT,style);
//clear previous text
doc.remove(0, doc.getLength());
//insert detail
for (int gv = 0; gv<arrayLength; gv++)
//insert grade
{
doc.insertString(doc.getLength(), String.valueOf(grades[gv])+"\n" ,set);
if(firstRun)
System.out.println("Insert Grades");
else
{
double average = average();
doc.insertString(doc.getLength(),"Average: " + String.valueOf(format.format(average)) + "\n",set);
}
firstRun = false;
}
}
catch(BadLocationException ble)
{
System.err.println("Couldn't insert text.");
}
return textPane;
}
//event to process user clicks
public void actionPerformed(ActionEvent e)
{
String arg = e.getActionCommand();
//user clicks the drop down box
if (e.getSource() == "Highest Grade")
{
for (grades>maxGrade)
{
maxGrade = grades;
(JOptionPane.showInputDialog(null, "The highest grade is ", + maxGrade));
}
}
else if (e.getSource() == "Lowest Grade")
{
for (grades<minGrade)
{
minGrade = grades;
(JOptionPane.showInputDialog(null, " The lowest grade is ", + minGrade));
}
}
//user clicks the file
if (e.getSource() == "NewGrades")
{
for (int gv = 0; gv<grades.length; gv++)
grades[gv] = 0.0;
double grade = 0;
int counter = 0;
while (grade != -1 && counter <50)
{
try
{
grade = Double.parseDouble
(JOptionPane.showInputDialog(null, "Enter Grade", "Enter Value"));
grades[counter] = grade;
if (grade != -1)
counter++;
}
catch (NumberFormatException nfe)
{
System.out.println("Incorrect entry");
JOptionPane.showMessageDialog(null, "You have entered an invalid number. If you are " + "finished, enter -1.");
}
}
arrayLength = counter;
addTextToTextPane();
}
//user clicks Exit on the File menu
else if (e.getSource() == "Exit")
{
System.exit(0);
}
}
public double average()
{
double sum = 0, average;
int totalCount = 0;
for(int gv = 0; gv < arrayLength; gv++)
{
if (grades[gv] > 0)
{
sum += grades[gv];
totalCount++;
}
}
average = sum / (double) totalCount;
return average;
}
//method to sort arrays
public void sort(String tempArray[])
{
boolean change;
do
{
change = false;
//loop to control number of passes
for (int gv = 0; gv < arrayLength - 1; gv++)
{
if (grades[gv] > grades[gv+1])
{
swap(gv);
change = true;
}
}
}
while(change);
}
//method to swap two elements of an array
public void swap(int index)
{
double temp = grades[index];
grades[index] = grades[index+1];
grades[index+1] = temp;
}
//main method executes at run time
public static void main(String[] args)
{
AverageGrade m = new AverageGrade();
m.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
m.setJMenuBar(m.createMenuBar());
m.setContentPane(m.createContentPane());
m.setSize(600,375);
m.setVisible(true);
JOptionPane.showMessageDialog(null, "To enter grades, select File, then Enter New Grades.\n" + "To finish, enter -1 in the grade box.", JOptionPane.INFORMATION_MESSAGE);
}
}