Results 1 to 6 of 6
- 03-21-2012, 01:40 AM #1
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Can anyone help me with an action listener?
I'm new to java and trying to learn on my own by watching video tutorials. I made a program with a scanner to do a simple conversion of whatever you typed in. Like you enter kilos and it converts it into pounds. What i'm trying to do now is make a program using java swing so it opens up a window but I can't seem to get the action listener right. (I'm using Java SE6)
Here's the code:
Java Code:import javax.swing.*; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class Converter extends JFrame implements ActionListener { private JButton convert; JTextArea text = new JTextArea(1,20); private int conversion = 0; public Converter() { setLayout(new FlowLayout()); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(400,200); convert = new JButton("Convert"); add(convert); add(text); convert.addActionListener(this); } public void actionPerformed(ActionEvent e) { } }Last edited by mdCollins1; 03-21-2012 at 01:45 AM.
- 03-21-2012, 03:44 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Can anyone help me with an action listener?
What is your problem exactly? Your actionPerformed method should do something in it (in this case get data and perform operations on it, then display the result).
http://docs.oracle.com/javase/tutori...nlistener.html
http://docs.oracle.com/javase/tutori...nts/intro.html
http://docs.oracle.com/javase/tutori...ybigindex.html is a great resource to use along side whatever videos you are watching.Last edited by sunde887; 03-21-2012 at 03:46 AM.
- 03-21-2012, 03:50 AM #3
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: Can anyone help me with an action listener?
I'm not exactly sure what to put into it to make it display the correct answer. I'm trying to make it so you enter inches and click convert and it tells you how many cms that would equal. (or another conversion)
(And thanks for the link on the really big index. I think it will come in handy)
-
Re: Can anyone help me with an action listener?
- 03-21-2012, 03:58 AM #5
Member
- Join Date
- Mar 2012
- Posts
- 7
- Rep Power
- 0
Re: Can anyone help me with an action listener?
This is the original code that only displays it in the console:
I was trying to make it with java swing and got completely stuck. I have no idea what to do now.Java Code:import java.util.Scanner; public class lbstokg_exact { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double lbs; double answer; System.out.println("Pounds to KG converter"); System.out.println("Enter pounds"); lbs = scanner.nextDouble(); answer = lbs / 2.20462262; System.out.println(lbs+" lbs is equal to "+answer+" kg"); } }
(This isn't the inches to centimeters but it is essentially the same idea.)
- 03-21-2012, 04:07 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Re: Can anyone help me with an action listener?
The main components you will need is a JLabel and a JTextField.
Here are the APIdocs for both classes:
JTextField (Java Platform SE 7 )
JLabel (Java Platform SE 7 )
Specifically look for methods to get or set data.
Similar Threads
-
Action-Listener with multiple Arguments? (Button Listener, specifically)
By Kevinw778 in forum AWT / SwingReplies: 2Last Post: 12-11-2011, 10:44 PM -
Problem in action listener
By cool in forum AWT / SwingReplies: 4Last Post: 11-16-2010, 07:44 AM -
Action Listener
By greatmajestics in forum AWT / SwingReplies: 8Last Post: 03-25-2010, 05:39 PM -
Action Listener? how to use this?
By jeffrey in forum New To JavaReplies: 2Last Post: 10-12-2009, 08:51 AM -
action listener on jcombobox
By chkm8 in forum New To JavaReplies: 2Last Post: 02-05-2009, 10:14 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks