Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: ActionListener

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ActionListener

    Now I am having problems with the actionListener. How do I get it to work so that the volume takes all variables?

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.text.DecimalFormat;
    import java.awt.Container;

    public class rectangularSolid extends JApplet implements ActionListener {

    public void init() {

    String in = JOptionPane.showInputDialog("1(volume),2(surface area),3(diagonal)");
    int input = Integer.parseInt(in);

    switch(input) {

    case 1:
    JLabel prompt;
    JLabel prompt2;
    JLabel prompt3;
    JLabel prompt4;
    Container c = getContentPane();
    c.setLayout(new FlowLayout());
    JTextField insert;
    JTextField insert2;
    JTextField insert3;
    JTextField insert4;
    prompt = new JLabel("enter length");
    insert = new JTextField(10);
    c.add(prompt);
    c.add(insert);
    prompt2 = new JLabel("enter width");
    insert2 = new JTextField(10);
    c.add(prompt2);
    c.add(insert2);
    prompt3 = new JLabel("enter height");
    insert3 = new JTextField(10);
    c.add(prompt3);
    c.add(insert3);
    insert.addActionListener(this);
    insert2.addActionListener(this);
    insert3.addActionListener(this);
    break;
    case 2:
    JTextArea outputArea;
    outputArea = new JTextArea(2,10);
    Container c1 = getContentPane();
    c1.add(outputArea);
    DecimalFormat twoDigits = new DecimalFormat("0.00");
    String sa = JOptionPane.showInputDialog("enter length");
    double length2 = Double.parseDouble(sa);
    String w2 = JOptionPane.showInputDialog("enter width");
    double width2 = Double.parseDouble(w2);
    String h2 = JOptionPane.showInputDialog("enter height");
    double height2 = Double.parseDouble(h2);
    String output = "";
    output = (" Surface area = "
    + twoDigits.format(surfaceArea(length2,width2,height 2)));
    outputArea.setText(output);
    JOptionPane.showMessageDialog(null,outputArea,null ,
    JOptionPane.INFORMATION_MESSAGE);
    break;}
    }
    public void actionPerformed(ActionEvent e ) {
    double length = Double.parseDouble(e.getActionCommand() );
    double width = Double.parseDouble(e.getActionCommand());
    double height = Double.parseDouble(e.getActionCommand());
    showStatus("Volume is " + volume ( length, width, height ) );
    }
    public double volume(double length, double width, double height) {
    return length * width * height;
    }
    public double surfaceArea(double length2, double width2, double height2) {
    return ( 2 * length2 * width2 + 2 * length2 * height2 + 2 * width2 * height2);

    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ActionListener

    You start by wrapping your code in highlight tags, which you've been asked to do in previous posts.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. ActionListener inside another ActionListener
    By kpat in forum AWT / Java Swing
    Replies: 6
    Last Post: March 28th, 2012, 03:43 PM
  2. ActionListener help
    By hello_world in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 27th, 2011, 11:45 PM
  3. ActionListener help
    By QBird in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 1st, 2011, 12:25 PM
  4. [SOLVED] ActionListener help
    By kbwalker87 in forum What's Wrong With My Code?
    Replies: 13
    Last Post: October 14th, 2010, 06:57 PM
  5. ActionListener Help?
    By Drag01 in forum AWT / Java Swing
    Replies: 1
    Last Post: March 30th, 2010, 08:21 PM