import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Temps_Grid_Keenan extends JFrame
{
private static final long serialVersionUID = 1L;
private static final int WIDTH = 400;
private static final int HEIGHT = 500;
private JTextField high1Field, low1Field, high2Field, low2Field;
private JTextField high3Field, low3Field, high4Field, low4Field;
private JTextField high5Field, low5Field, high6Field, low6Field;
private JTextField high7Field, low7Field, ave_highField, ave_lowField;
String temperatureText, aveHigh1 = "0", aveLow1 = "0";
public int count = 1, count2 = 1;
public int high1Temp, high2Temp, high3Temp, high4Temp;
public int high5Temp, high6Temp, high7Temp, totalHigh = 0;
public int low1Temp, low2Temp, low3Temp, low4Temp;
public int low5Temp, low6Temp, low7Temp, totalLow = 0;
public int aveHigh = 0, aveLow = 0, total = 0;
JLabel weekdayLabel, highLabel, lowLabel;
JLabel tempLabel;
public Temps_Grid_Keenan()
{
setTitle("Weekly Temperature Average");
setSize(WIDTH, HEIGHT);
setLayout(new GridLayout(11,3,5,5));
setDefaultCloseOperation(EXIT_ON_CLOSE);
createContents();
setVisible(true);
}
private void createContents()
{
weekdayLabel = new JLabel("Weekday");
add(weekdayLabel);
highLabel = new JLabel("High");
add(highLabel);
lowLabel = new JLabel("Low");
add(lowLabel);
JButton sundayButton = new JButton("Sunday");
sundayButton.addActionListener(new ButtonListener());
add(sundayButton);
high1Field = new JTextField(15);
add(high1Field);
low1Field = new JTextField(15);
add(low1Field);
JButton mondayButton = new JButton("Monday");
mondayButton.addActionListener(new ButtonListener());
add(mondayButton);
high2Field = new JTextField(15);
add(high2Field);
low2Field = new JTextField(15);
add(low2Field);
JButton tuesdayButton = new JButton("Tuesday");
tuesdayButton.addActionListener(new ButtonListener());
add(tuesdayButton);
high3Field = new JTextField(15);
add(high3Field);
low3Field = new JTextField(15);
add(low3Field);
JButton wednesdayButton = new JButton("Wednesday");
wednesdayButton.addActionListener(new ButtonListener());
add(wednesdayButton);
high4Field = new JTextField(15);
add(high4Field);
low4Field = new JTextField(15);
add(low4Field);
JButton thursdayButton = new JButton("Thursday");
thursdayButton.addActionListener(new ButtonListener());
add(thursdayButton);
high5Field = new JTextField(15);
add(high5Field);
low5Field = new JTextField(15);
add(low5Field);
JButton fridayButton = new JButton("Friday");
fridayButton.addActionListener(new ButtonListener());
add(fridayButton);
high6Field = new JTextField(15);
add(high6Field);
low6Field = new JTextField(15);
add(low6Field);
JButton saturdayButton = new JButton("Saturday");
saturdayButton.addActionListener(new ButtonListener());
add(saturdayButton);
high7Field = new JTextField(15);
add(high7Field);
low7Field = new JTextField(15);
add(low7Field);
JLabel ave_highLabel = new JLabel("Average High");
add(ave_highLabel);
ave_highField = new JTextField(15);
add(ave_highField);
JLabel blankLabel = new JLabel();
add(blankLabel);
JLabel ave_lowLabel = new JLabel("Average Low");
add(ave_lowLabel);
ave_lowField = new JTextField(15);
add(ave_lowField);
high1Field.addActionListener(new ButtonListener());
high2Field.addActionListener(new ButtonListener());
high3Field.addActionListener(new ButtonListener());
high4Field.addActionListener(new ButtonListener());
high5Field.addActionListener(new ButtonListener());
high6Field.addActionListener(new ButtonListener());
high7Field.addActionListener(new ButtonListener());
low1Field.addActionListener(new ButtonListener());
low2Field.addActionListener(new ButtonListener());
low3Field.addActionListener(new ButtonListener());
low4Field.addActionListener(new ButtonListener());
low5Field.addActionListener(new ButtonListener());
low6Field.addActionListener(new ButtonListener());
low7Field.addActionListener(new ButtonListener());
}
private class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("Sunday"))
{
clearFields();
high1Field.setBackground(Color.YELLOW);
low1Field.setBackground(Color.YELLOW);
}
else if (e.getActionCommand().equals("Monday"))
{
clearFields();
high2Field.setBackground(Color.YELLOW);
low2Field.setBackground(Color.YELLOW);
}
else if (e.getActionCommand().equals("Tuesday"))
{
clearFields();
high3Field.setBackground(Color.YELLOW);
low3Field.setBackground(Color.YELLOW);
}
else if (e.getActionCommand().equals("Wednesday"))
{
clearFields();
high4Field.setBackground(Color.YELLOW);
low4Field.setBackground(Color.YELLOW);
}
else if (e.getActionCommand().equals("Thursday"))
{
clearFields();
high5Field.setBackground(Color.YELLOW);
low5Field.setBackground(Color.YELLOW);
}
else if (e.getActionCommand().equals("Friday"))
{
clearFields();
high6Field.setBackground(Color.YELLOW);
low6Field.setBackground(Color.YELLOW);
}
else if (e.getActionCommand().equals("Saturday"))
{
clearFields();
high7Field.setBackground(Color.YELLOW);
low7Field.setBackground(Color.YELLOW);
}
/********************** Input High and Low ***********************/
/************************ Sunday High ****************************/
if (e.getSource() == high1Field)
{
high1Temp = Integer.parseInt(high1Field.getText());
totalHigh = totalHigh + high1Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Sunday Low ****************************/
else if (e.getSource() == low1Field)
{
low1Temp = Integer.parseInt(low1Field.getText());
totalLow = totalLow + low1Temp;
calcAverage1(totalLow, count2);
count2++;
}
/************************ Monday High ****************************/
else if (e.getSource() == high2Field)
{
high2Temp = Integer.parseInt(high2Field.getText());
totalHigh = totalHigh + high2Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Monday Low ****************************/
else if (e.getSource() == low2Field)
{
low2Temp = Integer.parseInt(low2Field.getText());
totalLow = totalLow + low2Temp;
calcAverage1(totalLow, count2);;
count2++;
}
/************************ Tuesday High ****************************/
else if (e.getSource() == high3Field)
{
high3Temp = Integer.parseInt(high3Field.getText());
totalHigh = totalHigh + high3Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Tuesday Low ****************************/
else if (e.getSource() == low3Field)
{
low3Temp = Integer.parseInt(low3Field.getText());
totalLow = totalLow + low3Temp;
calcAverage1(totalLow, count2);
count2++;
}
/************************ Wednesday High ****************************/
else if (e.getSource() == high4Field)
{
high4Temp = Integer.parseInt(high4Field.getText());
totalHigh = totalHigh + high4Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Wednesday Low ****************************/
else if (e.getSource() == low4Field)
{
low4Temp = Integer.parseInt(low4Field.getText());
totalLow = totalLow + low4Temp;
calcAverage1(totalLow, count2);
count2++;
}
/************************ Thursday High ****************************/
else if (e.getSource() == high5Field)
{
high5Temp = Integer.parseInt(high5Field.getText());
totalHigh = totalHigh + high5Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Thursday Low ****************************/
else if (e.getSource() == low5Field)
{
low5Temp = Integer.parseInt(low5Field.getText());
totalLow = totalLow + low5Temp;
calcAverage1(totalLow, count2);
count2++;
}
/************************ Friday High ****************************/
else if (e.getSource() == high6Field)
{
high6Temp = Integer.parseInt(high6Field.getText());
totalHigh = totalHigh + high6Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Friday Low ****************************/
else if (e.getSource() == low6Field)
{
low6Temp = Integer.parseInt(low6Field.getText());
totalLow = totalLow + low6Temp;
calcAverage1(totalLow, count2);
count2++;
}
/************************ Saturday High ****************************/
else if (e.getSource() == high7Field)
{
high7Temp = Integer.parseInt(high7Field.getText());
totalHigh = totalHigh + high7Temp;
calcAverage(totalHigh, count);
count++;
}
/************************ Saturday Low ****************************/
else if (e.getSource() == low7Field)
{
low7Temp = Integer.parseInt(low7Field.getText());
totalLow = totalLow + low7Temp;
calcAverage1(totalLow, count2);
count2++;
}
ave_highField.setText(aveHigh1);
ave_lowField.setText(aveLow1);
}
}
/************** Calculate average high and low; clear fields ***************************/
public void calcAverage(int totalHigh, int count)
{
aveHigh = totalHigh / count;
aveHigh1 = Integer.toString(aveHigh);
}
public void calcAverage1(int totalLow, int count2)
{
aveLow = totalLow / count2;
aveLow1 = Integer.toString(aveLow);
}
public void clearFields()
{
high1Field.setBackground(Color.WHITE);
low1Field.setBackground(Color.WHITE);
high2Field.setBackground(Color.WHITE);
low2Field.setBackground(Color.WHITE);
high3Field.setBackground(Color.WHITE);
low3Field.setBackground(Color.WHITE);
high4Field.setBackground(Color.WHITE);
low4Field.setBackground(Color.WHITE);
high5Field.setBackground(Color.WHITE);
low5Field.setBackground(Color.WHITE);
high6Field.setBackground(Color.WHITE);
low6Field.setBackground(Color.WHITE);
high7Field.setBackground(Color.WHITE);
low7Field.setBackground(Color.WHITE);
}
public static void main (String [] args)
{
new Temps_Grid_Keenan();
}
}