Note that there are some explanatory texts on larger screens.

plurals
  1. POGUI program pictures change with drop down menu action java
    primarykey
    data
    text
    <p>I have written a program that calculates miles per gallon entered. it can also switch to km with a drop down menu. I am trying to make it so that when I select the drop down menu that the picture can switch each time I switch. so a different picture for miles and different one for km. its silly but its one of the requirements of the assignment. how do I go about doing that? here is what i currently have, I just want the picture to change between two I have. </p> <pre><code>import java.awt.Component; import java.awt.event.*; import java.awt.*; import javax.swing.*; import static javax.swing.GroupLayout.Alignment.*; import net.miginfocom.swing.MigLayout; import javax.imageio.ImageIO; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; public class mpg_mig extends JFrame { public mpg_mig() { JLabel titleLabel = new JLabel("Fuel Calculator"); titleLabel.setFont(new Font("Serif", Font.BOLD, 18)); titleLabel.setForeground(Color.blue); final JLabel distLabel = new JLabel("Miles:"); final JTextField distText = new JTextField(10); JLabel traveledLabel = new JLabel("traveled"); final JLabel fuelLabel = new JLabel("Gas:"); final JTextField fuelText = new JTextField(10); final JLabel mpgLabel = new JLabel("Miles per gallon:"); final JTextField mpgText = new JTextField(10); JButton clearButton = new JButton("Clear"); JButton calcButton = new JButton("Calculate"); String[] actStrings = { "Miles", "kiloMeters" }; JComboBox jComboBox1 = new JComboBox(actStrings); jComboBox1.setEditable(true); String sFileName = "circuit1.png"; BufferedImage image = null; try { image = ImageIO.read(new File(sFileName)); } catch (IOException ex) { System.out.println (ex.toString()); System.out.println(sFileName); JOptionPane.showMessageDialog(null,ex.toString() + " " + sFileName); System.exit(0); } JLabel labelPic1 = new JLabel(new ImageIcon(image)); setResizable( false ); JPanel p = new JPanel(new MigLayout("", "[] [] []", "[] [] [] [] []")); p.setBackground(Color.WHITE); setContentPane(p); p.add(labelPic1, "cell 0 0 1 3"); //(column 0, row 0, width 1, height 3) p.add(clearButton, "cell 0 3"); //(column 0, row 3) p.add(calcButton, "cell 0 4"); //(column 0, row 4) p.add(titleLabel, "cell 1 0"); //(column 1, row 0) p.add(distLabel, "cell 1 1"); //(column 1, row 1) p.add(fuelLabel, "cell 1 2"); //(column 1, row 2) p.add(mpgLabel, "cell 1 3"); //(column 1, row 3) p.add(jComboBox1, "cell 1 4"); //(column 1, row 4) p.add(distText, "cell 2 1"); //(column 2, row 1) p.add(fuelText, "cell 2 2"); //(column 2, row 2) p.add(mpgText, "cell 2 3"); //(column 2, row 3) clearButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { distText.setText(""); fuelText.setText(""); mpgText.setText(""); } }); calcButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { if(isNumeric(distText.getText()) &amp;&amp; isNumeric(fuelText.getText())) { double fuel; double dist; double result; fuel = Double.parseDouble(fuelText.getText()); dist = Double.parseDouble(distText.getText()); result = dist/fuel; result = Round(result,2); mpgText.setText(String.format("%f", result)); } else { JOptionPane.showMessageDialog(null, "Enter distance traveled and fuel used"); } } }); jComboBox1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { //JComboBox jComboBox1 = (JComboBox)event.getSource(); JComboBox jComboBox1 = (JComboBox)event.getSource(); if(jComboBox1.getSelectedItem() == "Miles") { distLabel.setText("Miles"); mpgLabel.setText("Miles per gallon:"); } else { distLabel.setText("KiloMeters"); mpgLabel.setText("KM per liter:"); } } }); setTitle("MPG and KML"); pack(); //pack disables setting frames size... setLocationRelativeTo(null);//center frame and showMessageDialog setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); } private static boolean isNumeric(String text) { try { Double.parseDouble(text); } catch (Exception e) { return false; } return true; } public double Round(double val, int plc) { double pwr = Math.pow(10,plc); val = val * pwr; //shift to the left double tmp = (int) val; if( ((int)(val + .5)) == (int) val) return (tmp/pwr); //don't round up else return((tmp + 1.0)/pwr); //round up } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName()); } catch (Exception ex) { ex.printStackTrace(); } new mpg_mig().setVisible(true); } }); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload