7 Replies - 667 Views - Last Post: 19 December 2013 - 08:13 PM Rate Topic: -----

#1 spiteria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-October 13

Calculating Force on an Object at Depth

Posted 13 December 2013 - 10:38 PM

So, I'm trying to write a program that will calculate the average force on an object of varying size and shape in a liquid of varying at varying depth.

I'm using JFrame and right now I'm just trying to get the ActionListener for the button to work. The compiler gives the error

FindForce.java:37: error: MyFrame.Calculator is not abstract and does not override abstract method actionPerformed(ActionEvent) in ActionListener
   class Calculator implements ActionListener
   ^
1 error



Apparently there is a problem with my class statement "class Calculator implements ActionListener". I don't know what it would be though. It doesn't look like I've done anything different in any other programs I've written.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class GUIProgram1
{
   public static void main(String [] args)
   {
      MyFrame moveRight = new MyFrame("Move Right!");
      moveRight.setSize (500, 80);
      moveRight.setLocation (500, 300);
      moveRight.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      moveRight.setVisible(true);
   }
}
class MyFrame extends JFrame  
{
   JButton btn = new JButton("Odd or Even");
   JLabel num = new JLabel("Enter an integer:");
   JTextField text = new JTextField(10);

   public MyFrame(String s)     
   {        
      super(s);
      setLayout(new FlowLayout());

      add(num);
      add(text);
      add(btn);

      btn.addActionListener(new OddHandler());
   }
   class OddHandler implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         int x = Integer.parseInt(text.getText());
         if (x % 2 != 0)
         {
            JOptionPane.showMessageDialog(null, x + " is an odd number.");
         }
         else if (x % 2 == 0)
         {
            JOptionPane.showMessageDialog(null, x + " is an even number.");
         }
      }
   }
}



Sorry, I loaded the code from another thing in there.

This is the correct code:

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FindForce
{
   public static void main(String [] args)
   {
      MyFrame frame = new MyFrame("Calculating Force on an Object in a Liquid");
      frame.setLocation(500,200);
      frame.setSize(500,400);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }   
}

class MyFrame extends JFrame
{
   JButton calc = new JButton("Calculate");
   JLabel height1 = new JLabel("Distance from Surface");
   JTextField first = new JTextField(10);
      
   
   public MyFrame(String s)
   {
      super(s);
      setLayout(new FlowLayout());
   
      add(height1);
      add(first);
      add(calc);
      
      calc.addActionListener(new Calculator());
   }
   
   class Calculator implements ActionListener
   {
      public void actionPeformed(ActionEvent event)
      {
         int heightI = Integer.parseInt(first.getText());
         JOptionPane.showMessageDialog(null, "The distance of the object from the surface of the liquid is " + heightI + " meters.");
      }
   }    
}



Is This A Good Question/Topic? 0
  • +

Replies To: Calculating Force on an Object at Depth

#2 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 792
  • View blog
  • Posts: 3,467
  • Joined: 03-December 12

Re: Calculating Force on an Object at Depth

Posted 13 December 2013 - 10:47 PM

Looks like you are declaring a class within another class.
Was This Post Helpful? 0
  • +
  • -

#3 spiteria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-October 13

Re: Calculating Force on an Object at Depth

Posted 13 December 2013 - 10:48 PM

View Postastonecipher, on 13 December 2013 - 10:47 PM, said:

Looks like you are declaring a class within another class.


I did that in the first code that I posted though and it compiles fine. Why would it work with the first code but not the second?
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher  Icon User is offline

  • D.I.C.
  • member icon

Reputation: 792
  • View blog
  • Posts: 3,467
  • Joined: 03-December 12

Re: Calculating Force on an Object at Depth

Posted 14 December 2013 - 01:54 AM

Have a look at this previous DIC post.
Was This Post Helpful? 0
  • +
  • -

#5 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3806
  • View blog
  • Posts: 13,494
  • Joined: 12-December 12

Re: Calculating Force on an Object at Depth

Posted 14 December 2013 - 05:52 AM

public void actionPeformed(ActionEvent event)

You have misspelt Performed.
Was This Post Helpful? 3
  • +
  • -

#6 spiteria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-October 13

Re: Calculating Force on an Object at Depth

Posted 14 December 2013 - 06:32 PM

Thanks andrewsw, that did it.

So, now I'm trying to create a new layout for the UI with GroupLayout. This is what I have thus far.

import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FindForce
{
   public static void main(String [] args)
   {
      MyFrame frame = new MyFrame("Calculating Force on an Object in a Liquid");
      frame.setLocation(500,200);
      frame.setSize(350,400);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }   
}

class MyFrame extends JFrame
{
   JButton calc = new JButton("Calculate");
   JLabel depth1 = new JLabel("How Deep is the Object in the Liquid?");
   JTextField first = new JTextField("0",10);
   JLabel length1 = new JLabel("Length of the Object");
   JTextField second = new JTextField("0",10);
   JLabel width1 = new JLabel("Width of the Object");
   JTextField third = new JTextField("0",10);
         
   public MyFrame(String s)
   {
      super(s);
      
      GroupLayout layout = new GroupLayout(frame);
         frame.setLayout(layout);
         layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);
         
         layout.setHorizontalGroup(
            layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(depth1)
                  .addComponent(length1)
                  .addComponent(width1))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(first)
                  .addComponent(second)
                  .addComponent(third)
                  .addComponent(calc))
         );
         layout.setVerticalGroup(
            layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(depth1)
                  .addComponent(first))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(length1)
                  .addComponent(second))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(width1)
                  .addComponent(third))
               .addComponent(calc)
         );   
   
      add(depth1);
      add(first);
      add(length1);
      add(second);
      add(width1);
      add(third);
      add(calc);
            
      calc.addActionListener(new Calculator());
   }
   
   class Calculator implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         int depthI = Integer.parseInt(first.getText());
         int lengthI = Integer.parseInt(second.getText());
         int widthI = Integer.parseInt(third.getText());
         JOptionPane.showMessageDialog(null, "The depth of the object from the surface of the liquid is " + depthI + " meters.\n" +
                                                "The length of the object is " + lengthI + " meters.\n" + "The width of the object is " +
                                                widthI + " meters.");
      }
   }    
}



I put the GroupLayout code in to my MyFrame method, where I normally put FlowLayout. It's telling me that it can't find the frame object I created back in line 10. I don't know why the frame object wouldn't be visible within the new MyFrame class. Do I have to place the code for GroupLayout into the FindForce class or create some kind of bridge that allows the MyFrame frame object to be visible in MyFrame.

The error message I'm getting is:

FindForce.java:32: error: cannot find symbol
      GroupLayout layout = new GroupLayout(frame);
                                           ^
  symbol:   variable frame
  location: class MyFrame
FindForce.java:33: error: cannot find symbol
         frame.setLayout(layout);
         ^
  symbol:   variable frame
  location: class MyFrame
2 errors


Was This Post Helpful? 0
  • +
  • -

#7 spiteria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-October 13

Re: Calculating Force on an Object at Depth

Posted 14 December 2013 - 06:46 PM

Nevermind, I went deeper into the Java Tutorial on GroupLayout and ended up finding and using the getContentPane() method. It worked.

Here's the new code.

import java.util.*;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.awt.*;
import java.awt.event.*;

public class FindForce
{
   public static void main(String [] args)
   {
      MyFrame frame = new MyFrame("Calculating Force on an Object in a Liquid");
      frame.setLocation(500,200);
      frame.setSize(350,400);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }   
}

class MyFrame extends JFrame
{
   JButton calc = new JButton("Calculate");
   JLabel depth1 = new JLabel("How Deep is the Object in the Liquid?");
   JTextField first = new JTextField("0",10);
   JLabel length1 = new JLabel("Length of the Object");
   JTextField second = new JTextField("0",10);
   JLabel width1 = new JLabel("Width of the Object");
   JTextField third = new JTextField("0",10);
         
   public MyFrame(String s)
   {
      super(s);
      
      GroupLayout layout = new GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);
         
         layout.setHorizontalGroup(
            layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(depth1)
                  .addComponent(length1)
                  .addComponent(width1))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(first)
                  .addComponent(second)
                  .addComponent(third)
                  .addComponent(calc))
         );
         layout.setVerticalGroup(
            layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(depth1)
                  .addComponent(first))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(length1)
                  .addComponent(second))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(width1)
                  .addComponent(third))
               .addComponent(calc)
         );   
   
      add(depth1);
      add(first);
      add(length1);
      add(second);
      add(width1);
      add(third);
      add(calc);
            
      calc.addActionListener(new Calculator());
   }
   
   class Calculator implements ActionListener
   {
      public void actionPerformed(ActionEvent event)
      {
         int depthI = Integer.parseInt(first.getText());
         int lengthI = Integer.parseInt(second.getText());
         int widthI = Integer.parseInt(third.getText());
         JOptionPane.showMessageDialog(null, "The depth of the object from the surface of the liquid is " + depthI + " meters.\n" +
                                                "The length of the object is " + lengthI + " meters.\n" + "The width of the object is " +
                                                widthI + " meters.");
      }
   }    
}


Was This Post Helpful? 0
  • +
  • -

#8 spiteria  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 12-October 13

Re: Calculating Force on an Object at Depth

Posted 19 December 2013 - 08:13 PM

Hey Everyone,

I'm trying to implement a combobox with a number of JTextFields in order to run a method that will convert the units between the various choices. I need to make it so that when I hit the 'Calculate' button the numbers supplied in the JTextFields is converted from the units provided by the comboboxes and the result is displayed (right now through a JOptionPane info window. Here's what I have thus far:

import java.util.*;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.awt.*;
import java.awt.event.*;

public class FindForce
{
   public static void main(String [] args)
   {
      MyFrame frame = new MyFrame("Calculating Force on an Object in a Liquid");
      frame.setLocation(500,200);
      frame.setSize(500,400);
      frame.setVisible(true);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }   
}

class MyFrame extends JFrame
{
   JButton calc = new JButton("Calculate");
   JLabel depth1 = new JLabel("How Deep is the Object in the Liquid?");
   JTextField first = new JTextField("0",10);
   JLabel length1 = new JLabel("Length of the Object");
   JTextField second = new JTextField("0",10);
   JLabel width1 = new JLabel("Width of the Object");
   JTextField third = new JTextField("0",10);
   JLabel depth2 = new JLabel("Depth of the Object");
   JTextField fourth = new JTextField("0",10);
   JLabel densityOfLiquid = new JLabel("Density of the Liquid");
   JTextField fifth = new JTextField("0",10);
   
   String [] measurements = {"meters","centimeters","feet","inches","millimeters","yards","miles","kilometers"};
   String [] measurements2 = {"kg/m3","g/cm3","slugs/ft3"};
   
   JComboBox mDepth1 = new JComboBox(measurements);   
   JComboBox mHeight = new JComboBox(measurements);
   JComboBox mWidth = new JComboBox(measurements);
   JComboBox mDepth2 = new JComboBox(measurements);
   JComboBox mDensityL = new JComboBox(measurements2);
         
   public MyFrame(String s)
   {
      super(s);
      
      GroupLayout layout = new GroupLayout(getContentPane());
         getContentPane().setLayout(layout);
         layout.setAutoCreateGaps(true);
         layout.setAutoCreateContainerGaps(true);
         
         layout.setHorizontalGroup(
            layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(depth1)
                  .addComponent(length1)
                  .addComponent(width1)
                  .addComponent(depth2)
                  .addComponent(densityOfLiquid)
                  .addComponent(calc))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(first)
                  .addComponent(second)
                  .addComponent(third)
                  .addComponent(fourth)
                  .addComponent(fifth))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                  .addComponent(mDepth1)
                  .addComponent(mHeight)
                  .addComponent(mWidth)
                  .addComponent(mDepth2)
                  .addComponent(mDensityL))
         );
         layout.setVerticalGroup(
            layout.createSequentialGroup()
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(depth1)
                  .addComponent(first)
                  .addComponent(mDepth1))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(length1)
                  .addComponent(second)
                  .addComponent(mHeight))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(width1)
                  .addComponent(third)
                  .addComponent(mWidth))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(depth2)
                  .addComponent(fourth)
                  .addComponent(mDepth2))
               .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                  .addComponent(densityOfLiquid)
                  .addComponent(fifth)
                  .addComponent(mDensityL))
               .addComponent(calc)
         );   
           
      calc.addActionListener(new Calculator());
      mDepth1.addItemListener(new Calculator());
   }
         
   public class Calculator implements ActionListener, ItemListener
   {
      double depthI;
      double lengthI;
      double widthI;
      double depthII;
      double volume;
      double densityI;
      
      public void actionPerformed(ActionEvent event)
      {
         depthI = Double.parseDouble(first.getText());
         lengthI = Double.parseDouble(second.getText());
         widthI = Double.parseDouble(third.getText());
         depthII = Double.parseDouble(fourth.getText());
         volume = lengthI * widthI * depthII;
         densityI = Double.parseDouble(fifth.getText());
         JOptionPane.showMessageDialog(null, "The depth of the object from the surface of the liquid is " + depthI + " meters.\n" +
                                                "The volume of the object is " + volume + " meters cubed.\n" + "The density of the liquid is " +
                                                densityI + " kilograms per meter cubed.");         
      }
      public void itemStateChanged(ItemEvent event)
      {
         if (event.getStateChange(measurements[7]) == SELECTED)
         {
            depthI = depthI * 1000;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[6]) == SELECTED)
         {
            depthI = depthI * 1609.344;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[5]) == SELECTED)
         {
            depthI = depthI * .9144;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[4]) == SELECTED)
         {
            depthI = depthI / 1000;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[3]) == SELECTED)
         {
            depthI = depthI * .0254;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[2]) == SELECTED)
         {
            depthI = depthI * .3048;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[1]) == SELECTED)
         {
            depthI = depthI / 100;
            setVisible(true);
         }
         else if (event.getStateChange(measurements[0]) == SELECTED)
         {
            depthI = depthI;
            setVisible(true);
         }
         else
         {
            setVisible(false);
         }         
      }
   }
   //I need to figure out how to implement multiple nested or interconnected Event Listerners
   
   /*
      To deal with the error I keep getting:
         Note: FindForce.java uses unchecked or unsafe operations.
         Note: Recompile with -Xlint:unchecked for details.
      I need to figure out how to deal with unchecked exceptions.   
   */   
}



Originally I had the ItemListener and the ActionListener implemented in separate classes. The class that used ActionListener was Calculator and the class that implemented ItemListener was Measurements. I was reading through the tutorial on Component Listeners though and realized I can use multiple listeners in a class. So, I moved both the ActionListener for the Calculate button and the ItemListener for the Comboboxes into one class. I still don't know how to link the combobox the the action of the button though. Once the combobox is selected it doesn't perform the unit changing operation.

I think I need to move the Double.parseDouble commands in the ActionEvent method out into the Calculate class so the ItemEvent method can access the variables once they have values. I need to link them so that the conversion happen whenever a selection is made or a value entered and the button pushed.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1