// Windchill GUI import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Windchill implements ActionListener { // class constants private static final int WINDOW_WIDTH = 275; // pixels private static final int WINDOW_HEIGHT = 250; // pixels private static final int TEXT_WIDTH = 20; // characters private static final FlowLayout LAYOUT_STYLE = new FlowLayout(); private static final String LEGEND = "This windchill " + "calculator is intended for velocities greater than 4 mph."; // instance variables // window for GUI private JFrame window = new JFrame("Windchill Calculator"); // legend private JTextArea legendArea = new JTextArea(LEGEND, 2, TEXT_WIDTH); // user entry area for temperature private JLabel fahrTag = new JLabel("Fahrenheit temperature"); private JTextField fahrText = new JTextField(TEXT_WIDTH); // user entry area for windspeed private JLabel windTag = new JLabel("Windspeed (mph)"); private JTextField windText = new JTextField(TEXT_WIDTH); // entry area for windchill result private JLabel chillTag = new JLabel("Windchill temperature"); private JTextField chillText = new JTextField(TEXT_WIDTH); // run button private JButton runButton = new JButton("Run"); // Windchill(): constructor public Windchill() { // configure GUI window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); legendArea.setEditable(false); legendArea.setLineWrap(true); legendArea.setWrapStyleWord(true); legendArea.setBackground(window.getBackground()); chillText.setEditable(false); chillText.setBackground(Color.WHITE); // register event listener runButton.addActionListener(this); // arrange components in GUI window.setLayout(LAYOUT_STYLE); window.add(legendArea); window.add(fahrTag); window.add(fahrText); window.add(windTag); window.add(windText); window.add(chillTag); window.add(chillText); window.add(runButton); // display GUI window.setVisible(true); } // actionPerformed(): run button action event handler public void actionPerformed(ActionEvent e) { // get user's responses String response1 = fahrText.getText(); double t = Double.parseDouble(response1); String response2 = windText.getText(); double v = Double.parseDouble(response2); // compute windchill double windchillTemperature = 0.081 * (t - 91.4) * (3.71*Math.sqrt(v) + 5.81 - 0.25*v) + 91.4; int perceivedTemperature = (int) Math.round(windchillTemperature); // display windchill String output = String.valueOf(perceivedTemperature); chillText.setText(output); } // main(): application entry point public static void main(String[] args) { Windchill gui = new Windchill(); } }