/* Ch06Ex01.java CIS 160 David Klick 2011-09-24 Demonstrates a working GUI (answer to Chapter 6, Exercise 1) */ import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.text.DecimalFormat; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JTextField; import javax.swing.SwingConstants; import javax.swing.SwingUtilities; public class Ch06Ex01 extends JFrame { JTextField txtScr1, txtScr2, txtScr3, txtScr4; JTextField txtWgt1, txtWgt2, txtWgt3, txtWgt4; public Ch06Ex01() { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private void createAndShowGUI() { final int CENTER = SwingConstants.CENTER; JButton btnExit = new JButton("Exit"); JButton btnCalc = new JButton("Calc"); setTitle("Chapter 6, Exercise 1"); setSize(200,250); Container c = getContentPane(); c.setLayout(new GridLayout(6,3)); c.add(new JLabel("Test #", CENTER)); c.add(new JLabel("Score", CENTER)); c.add(new JLabel("Weight", CENTER)); c.add(new JLabel("1", CENTER)); txtScr1 = new JTextField(10); txtWgt1 = new JTextField(10); c.add(txtScr1); c.add(txtWgt1); c.add(new JLabel("2", CENTER)); txtScr2 = new JTextField(10); txtWgt2 = new JTextField(10); c.add(txtScr2); c.add(txtWgt2); c.add(new JLabel("3", CENTER)); txtScr3 = new JTextField(10); txtWgt3 = new JTextField(10); c.add(txtScr3); c.add(txtWgt3); c.add(new JLabel("4", CENTER)); txtScr4 = new JTextField(10); txtWgt4 = new JTextField(10); c.add(txtScr4); c.add(txtWgt4); c.add(new JLabel()); c.add(btnCalc); c.add(btnExit); btnCalc.addActionListener(new CalcListener()); btnExit.addActionListener(new ExitListener()); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); } public static void main(String[] args) { Ch06Ex01 app = new Ch06Ex01(); } public class ExitListener implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public class CalcListener implements ActionListener { public void actionPerformed(ActionEvent e) { // get the four weightings and calc total double wgt1, wgt2, wgt3, wgt4, totWgt; wgt1 = Double.parseDouble(txtWgt1.getText()); wgt2 = Double.parseDouble(txtWgt2.getText()); wgt3 = Double.parseDouble(txtWgt3.getText()); wgt4 = Double.parseDouble(txtWgt4.getText()); totWgt = wgt1 + wgt2 + wgt3 + wgt4; // convert weightings to percent of total wgt1 /= totWgt; wgt2 /= totWgt; wgt3 /= totWgt; wgt4 /= totWgt; // get scores double scr1, scr2, scr3, scr4; scr1 = Double.parseDouble(txtScr1.getText()); scr2 = Double.parseDouble(txtScr2.getText()); scr3 = Double.parseDouble(txtScr3.getText()); scr4 = Double.parseDouble(txtScr4.getText()); // compute weighted average double wavg = scr1*wgt1 + scr2*wgt2 + scr3*wgt3 + scr4*wgt4; // display weighted average DecimalFormat twoPlaces = new DecimalFormat("##.##"); JOptionPane.showMessageDialog(null, "The weighted average is " + twoPlaces.format(wavg)); } } }