943,108 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 74
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 30th, 2010
0

How to add another button

Expand Post »
here's my code,
i wanted to add another button that would have its own ActionListener.
NOTE: its in object oriented style.

i wanted to add these codes in my main class for my additional buttons, but my problem is
i dont know how to make their ActionListeners.
Java Syntax (Toggle Plain Text)
  1. myButton2=new Button("GET DIAMETER");
  2. myButton3=new Button("GET AREA");
  3. myButton4=new Button("GET RADIUS");
  4. myButton5=new Button("GET CIRCUMFERENCE");
  5. myFrame.add(myButton2);
  6. myFrame.add(myButton3);
  7. myFrame.add(myButton4);
  8. myFrame.add(myButton5);


my complete code:

MAIN CLASS:
Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class FrameDemo implements WindowListener, ActionListener{
  7. static Line l=new Line();
  8. private Frame myFrame;
  9. private Label myLabel,myLabel2,myLabel3,myLabel4,myLabel5;
  10. private TextField myTextField,myTextField2,myTextField3,myTextField4,myTextField5;
  11. private Button myButton;
  12.  
  13.  
  14. public FrameDemo()throws NumberFormatException{
  15.  
  16. Scanner input=new Scanner(System.in);
  17.  
  18. myFrame=new Frame("My Frame");
  19. myLabel=new Label("P1 X --->");
  20. myLabel2=new Label("P1 Y --->");
  21. myLabel3=new Label("P2 X --->");
  22. myLabel4=new Label("P2 Y --->");
  23. myLabel5=new Label("The Distance: ");
  24.  
  25. myTextField=new TextField();
  26. myTextField2=new TextField();
  27. myTextField3=new TextField();
  28. myTextField4=new TextField();
  29. myTextField5=new TextField();
  30.  
  31. myButton=new Button("GET DISTANCE");
  32.  
  33. myLabel.setBackground(Color.BLACK);
  34. myLabel.setForeground(Color.WHITE);
  35. myLabel2.setBackground(Color.BLACK);
  36. myLabel2.setForeground(Color.WHITE);
  37. myLabel3.setBackground(Color.BLACK);
  38. myLabel3.setForeground(Color.WHITE);
  39. myLabel4.setBackground(Color.BLACK);
  40. myLabel4.setForeground(Color.WHITE);
  41. myLabel5.setBackground(Color.BLACK);
  42. myLabel5.setForeground(Color.WHITE);
  43. myLabel.setFont(new Font("Verdana", Font.BOLD, 15));
  44. myLabel2.setFont(new Font("Verdana", Font.BOLD, 15));
  45. myLabel3.setFont(new Font("Verdana", Font.BOLD, 15));
  46. myLabel4.setFont(new Font("Verdana", Font.BOLD, 15));
  47. myLabel5.setFont(new Font("Verdana", Font.BOLD, 15));
  48. myButton.setBackground(Color.BLACK);
  49. myButton.setForeground(Color.WHITE);
  50. myFrame.setSize(450, 100);
  51. myFrame.setLayout(new GridLayout(3,4));
  52.  
  53. myFrame.add(myLabel);
  54. myFrame.add(myTextField);
  55. myFrame.add(myLabel2);
  56. myFrame.add(myTextField2);
  57. myFrame.add(myLabel3);
  58. myFrame.add(myTextField3);
  59. myFrame.add(myLabel4);
  60. myFrame.add(myTextField4);
  61. myFrame.add(myButton);
  62. myFrame.add(myTextField5);
  63. myFrame.addWindowListener(this);
  64. myButton.addActionListener(this);
  65. myFrame.show();
  66. }
  67.  
  68. public static void main(String[] args){
  69. FrameDemo f=new FrameDemo();
  70. }
  71. public void actionPerformed(ActionEvent e){
  72. String x1,y1,x2,y2,dis2;
  73. double dis;
  74. x1=myTextField.getText(); y1=myTextField2.getText();
  75. x2=myTextField3.getText(); y2=myTextField4.getText();
  76. l.setP1(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
  77. l.setP2(new Point(Integer.parseInt(x2),Integer.parseInt(y2)));
  78. dis=l.getRadius();
  79. dis2=Double.toString(dis);
  80. myTextField5.setText(dis2);
  81. }
  82.  
  83. public void windowDeactivated(WindowEvent e){
  84. }
  85.  
  86. public void windowActivated(WindowEvent e){
  87. }
  88.  
  89. public void windowDeiconified(WindowEvent e){
  90. }
  91.  
  92. public void windowIconified(WindowEvent e){
  93. }
  94.  
  95. public void windowClosed(WindowEvent e){
  96. }
  97.  
  98. public void windowClosing(WindowEvent e){
  99. System.exit(0);
  100. }
  101.  
  102. public void windowOpened(WindowEvent e){
  103. }
  104. }

LINE CLASS:
Java Syntax (Toggle Plain Text)
  1. public class Line
  2. {
  3.  
  4. public Point p1;
  5. public Point p2;
  6.  
  7.  
  8. public Line()
  9. {
  10. p1=new Point();
  11. p2=new Point();
  12. }
  13.  
  14. public Line(Point p1,Point p2)
  15. {
  16. this.p1=p1;
  17. this.p2=p2;
  18. }
  19.  
  20. public void setP1(Point p1)
  21. {
  22. this.p1=p1;
  23. }
  24.  
  25. public void setP2(Point p2)
  26. {
  27. this.p2=p2;
  28. }
  29.  
  30. public Point getP1()
  31. {
  32. return this.p1;
  33. }
  34.  
  35. public Point getP2()
  36. {
  37. return this.p2;
  38. }
  39.  
  40. public double getRadius()
  41. {
  42. double len=0;
  43.  
  44.  
  45. if(p1.getX()==p2.getX())
  46. len=Math.abs(p2.getY()-p1.getY());
  47. else if(p1.getY()==p2.getY())
  48. len=Math.abs(p2.getX()-p1.getX());
  49. else
  50. len=Math.sqrt((Math.abs(p2.getX()-p1.getX())*Math.abs(p2.getX()-p1.getX()))+(Math.abs(p2.getY()-p1.getY())*Math.abs(p2.getY()-p1.getY())));
  51. return len;
  52. }
  53.  
  54. public double getDiameter()
  55. {
  56.  
  57. return getRadius()*2;
  58. }
  59.  
  60. public double getArea()
  61. {
  62.  
  63. return Math.PI*getRadius()*getRadius();
  64. }
  65.  
  66. public double getCircumference()
  67. {
  68.  
  69. return 2*Math.PI*getRadius();
  70. }
  71.  
  72. }

POINT CLASS:
Java Syntax (Toggle Plain Text)
  1. public class Point
  2. {
  3.  
  4. public int x;
  5. public int y;
  6.  
  7. public Point()
  8. {
  9. this.x=0;
  10. this.y=0;
  11. }
  12.  
  13. public Point(int x,int y)
  14. {
  15. this.x=x;
  16. this.y=y;
  17. }
  18.  
  19. public void setX(int x)
  20. {
  21. this.x=x;
  22. }
  23.  
  24. public void setY(int y)
  25. {
  26. this.y=y;
  27. }
  28.  
  29. public int getX()
  30. {
  31. return this.x;
  32. }
  33.  
  34. public int getY()
  35. {
  36. return this.y;
  37. }
  38.  
  39. }


THERE ARE NO ERRORS WITH THIS PROGRAM, IM JUST HAVING PROBLEMS IN PROVIDING
ActionListeners to my additional buttons.

any tips or advices are gladly accepted
Thanks in advance.
Last edited by intes77; Jul 30th, 2010 at 8:21 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
intes77 is offline Offline
34 posts
since Feb 2010
Jul 30th, 2010
0
Re: How to add another button
To create action listener object.
Define a class that implements ActionListener, create a new instance of the class and add it as a listener to the button.

Do a Search for ActionListener or addActionListener( on this forum for code samples.
Reputation Points: 915
Solved Threads: 500
Posting Expert
NormR1 is online now Online
5,067 posts
since Jun 2010
Jul 30th, 2010
0
Re: How to add another button
You can do this in a more compact (but maybe less readable?) way by using an anonymous inner class, eg

Java Syntax (Toggle Plain Text)
  1. myButton.addActionListener(new ActionListener() {
  2. @Override
  3. public void actionPerformed(ActionEvent e) {
  4. // do whatever
  5. }
  6. });
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Jul 30th, 2010
0
Re: How to add another button
how can i make 4 different action listeners with these 4 buttons?
Java Syntax (Toggle Plain Text)
  1. myButton2=new Button("GET DIAMETER");
  2. myButton3=new Button("GET AREA");
  3. myButton4=new Button("GET RADIUS");
  4. myButton5=new Button("GET CIRCUMFERENCE");
  5. myFrame.add(myButton2);
  6. myFrame.add(myButton3);
  7. myFrame.add(myButton4);
  8. myFrame.add(myButton5);

my idea is that i would make ActionListeners for the 4 buttons
Java Syntax (Toggle Plain Text)
  1. myButton2.addActionListener(this);
  2. myButton3.addActionListener(this);
  3. myButton4.addActionListener(this);
  4. myButton5.addActionListener(this);

now my problem is how can i make their 4 different actionPerformed methods
because the standard code is always like this:
Java Syntax (Toggle Plain Text)
  1. public void actionPerformed(ActionEvent e)
  2. {
  3. //button events will go here
  4. }

any help please, im confused
Last edited by intes77; Jul 30th, 2010 at 9:42 am.
Reputation Points: 10
Solved Threads: 0
Light Poster
intes77 is offline Offline
34 posts
since Feb 2010
Jul 30th, 2010
0
Re: How to add another button
Use the solution I gave you in my previous post - and have a different anonymous inner class, each with its own different actionPeformed, for each button:

Java Syntax (Toggle Plain Text)
  1. myButton1.addActionListener(new ActionListener() {
  2. @Override
  3. public void actionPerformed(ActionEvent e) {
  4. // do whatever you want when button 1 is pressed
  5. }
  6. });
  7.  
  8. myButton2.addActionListener(new ActionListener() {
  9. @Override
  10. public void actionPerformed(ActionEvent e) {
  11. // do whatever you want when button 2 is pressed
  12. }
  13. });
  14.  
  15. // etc
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Jul 30th, 2010
0
Re: How to add another button
oh i got ur point. so in adding another action listener.
you made an inner class for it.

just curious. is this "@Override" thing a part of the code?
Reputation Points: 10
Solved Threads: 0
Light Poster
intes77 is offline Offline
34 posts
since Feb 2010
Jul 30th, 2010
0
Re: How to add another button
It's called an annotation - they were new in java 1.5 (ish). This one says that the following method is intended to override a method in the superclass (or implement a method in a declared interface). It allows the compiler to check that you are overriding as you intended - before annotations, if you spelled the name of a method wrong when you tried to override, you wouldn't get an error message, but the code wouldn't do what you expected.
Now you know what they are called you can google for a load more examples.
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Jul 30th, 2010
0
Re: How to add another button
Earlier versions of the compiler did NOT have that option. When you attempted to override a method in a class, if you misspelled the method name or got the args wrong, the compiler was very happy to add the NEW method to the existing methods for the class instead of overriding the method you intended to override. Then your code didn't work as desired and it was a hard bug to find.

So after many complaints, the compiler writers added a feature that allows us programmers to have the compiler check that the method we've coded does override one of the methods of the class.
Reputation Points: 915
Solved Threads: 500
Posting Expert
NormR1 is online now Online
5,067 posts
since Jun 2010
Jul 30th, 2010
0
Re: How to add another button
@Override is also v useful when you're reading other people's code. Without it there's no way to see that a method is overriding another without checking every method in every superclass by hand.
Featured Poster
Reputation Points: 1895
Solved Threads: 944
Posting Expert
JamesCherrill is offline Offline
5,741 posts
since Apr 2008
Jul 31st, 2010
0
Re: How to add another button
dude thanks a lot!!! here's my final main class

Java Syntax (Toggle Plain Text)
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class FrameDemo implements WindowListener{
  7. static Line l=new Line();
  8. private Frame myFrame;
  9. private Label myLabel,myLabel2,myLabel3,myLabel4,myLabel5;
  10. private TextField myTextField,myTextField2,myTextField3,myTextField4,myTextField5,myTextField6,myTextField7,myTextField8;
  11. private Button myButton,myButton1,myButton2,myButton3;
  12.  
  13.  
  14. public FrameDemo()throws NumberFormatException{
  15.  
  16. myFrame=new Frame("My Frame");
  17. myLabel=new Label("P1 X --->");
  18. myLabel2=new Label("P1 Y --->");
  19. myLabel3=new Label("P2 X --->");
  20. myLabel4=new Label("P2 Y --->");
  21. myLabel5=new Label("The Distance: ");
  22.  
  23. myTextField=new TextField();
  24. myTextField2=new TextField();
  25. myTextField3=new TextField();
  26. myTextField4=new TextField();
  27. myTextField5=new TextField();
  28. myTextField6=new TextField();
  29. myTextField7=new TextField();
  30. myTextField8=new TextField();
  31.  
  32. myButton=new Button("GET RADIUS");
  33. myButton1=new Button("GET DIAMETER");
  34. myButton2=new Button("GET AREA");
  35. myButton3=new Button("GET CIRCUMFERENCE");
  36.  
  37. myLabel.setBackground(Color.BLACK);
  38. myLabel.setForeground(Color.WHITE);
  39. myLabel2.setBackground(Color.BLACK);
  40. myLabel2.setForeground(Color.WHITE);
  41. myLabel3.setBackground(Color.BLACK);
  42. myLabel3.setForeground(Color.WHITE);
  43. myLabel4.setBackground(Color.BLACK);
  44. myLabel4.setForeground(Color.WHITE);
  45. myLabel5.setBackground(Color.BLACK);
  46. myLabel5.setForeground(Color.WHITE);
  47. myLabel.setFont(new Font("Verdana", Font.BOLD, 15));
  48. myLabel2.setFont(new Font("Verdana", Font.BOLD, 15));
  49. myLabel3.setFont(new Font("Verdana", Font.BOLD, 15));
  50. myLabel4.setFont(new Font("Verdana", Font.BOLD, 15));
  51. myLabel5.setFont(new Font("Verdana", Font.BOLD, 15));
  52. myButton.setBackground(Color.BLACK);
  53. myButton.setForeground(Color.WHITE);
  54. myButton1.setBackground(Color.BLACK);
  55. myButton1.setForeground(Color.WHITE);
  56. myButton2.setBackground(Color.BLACK);
  57. myButton2.setForeground(Color.WHITE);
  58. myButton3.setBackground(Color.BLACK);
  59. myButton3.setForeground(Color.WHITE);
  60. myFrame.setSize(570, 140);
  61. myFrame.setLayout(new GridLayout(4,3));
  62.  
  63. myFrame.add(myLabel);
  64. myFrame.add(myTextField);
  65. myFrame.add(myLabel2);
  66. myFrame.add(myTextField2);
  67. myFrame.add(myLabel3);
  68. myFrame.add(myTextField3);
  69. myFrame.add(myLabel4);
  70. myFrame.add(myTextField4);
  71. myFrame.add(myButton);
  72. myFrame.add(myTextField5);
  73. myFrame.add(myButton1);
  74. myFrame.add(myTextField6);
  75. myFrame.add(myButton2);
  76. myFrame.add(myTextField7);
  77. myFrame.add(myButton3);
  78. myFrame.add(myTextField8);
  79. myFrame.addWindowListener(this);
  80. myButton.addActionListener(
  81. new ActionListener()
  82. {
  83. @Override
  84. public void actionPerformed(ActionEvent e)
  85. {
  86. String x1,y1,x2,y2,dis2;
  87. double dis;
  88. x1=myTextField.getText(); y1=myTextField2.getText();
  89. x2=myTextField3.getText(); y2=myTextField4.getText();
  90. l.setP1(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
  91. l.setP2(new Point(Integer.parseInt(x2),Integer.parseInt(y2)));
  92. dis=l.getRadius();
  93. dis2=Double.toString(dis);
  94. myTextField5.setText(dis2);
  95. }
  96. });
  97.  
  98. myButton1.addActionListener(
  99. new ActionListener()
  100. {
  101. @Override
  102. public void actionPerformed(ActionEvent e)
  103. {
  104. String x1,y1,x2,y2,dis2;
  105. double dis;
  106. x1=myTextField.getText(); y1=myTextField2.getText();
  107. x2=myTextField3.getText(); y2=myTextField4.getText();
  108. l.setP1(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
  109. l.setP2(new Point(Integer.parseInt(x2),Integer.parseInt(y2)));
  110. dis=l.getDiameter();
  111. dis2=Double.toString(dis);
  112. myTextField6.setText(dis2);
  113. }
  114. });
  115.  
  116. myButton2.addActionListener(
  117. new ActionListener()
  118. {
  119. @Override
  120. public void actionPerformed(ActionEvent e)
  121. {
  122. String x1,y1,x2,y2,dis2;
  123. double dis;
  124. x1=myTextField.getText(); y1=myTextField2.getText();
  125. x2=myTextField3.getText(); y2=myTextField4.getText();
  126. l.setP1(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
  127. l.setP2(new Point(Integer.parseInt(x2),Integer.parseInt(y2)));
  128. dis=l.getArea();
  129. dis2=Double.toString(dis);
  130. myTextField7.setText(dis2);
  131. }
  132. });
  133.  
  134. myButton3.addActionListener(
  135. new ActionListener()
  136. {
  137. @Override
  138. public void actionPerformed(ActionEvent e)
  139. {
  140. String x1,y1,x2,y2,dis2;
  141. double dis;
  142. x1=myTextField.getText(); y1=myTextField2.getText();
  143. x2=myTextField3.getText(); y2=myTextField4.getText();
  144. l.setP1(new Point(Integer.parseInt(x1),Integer.parseInt(y1)));
  145. l.setP2(new Point(Integer.parseInt(x2),Integer.parseInt(y2)));
  146. dis=l.getCircumference();
  147. dis2=Double.toString(dis);
  148. myTextField8.setText(dis2);
  149. }
  150. });
  151. myFrame.show();
  152. }
  153.  
  154. public static void main(String[] args){
  155. FrameDemo f=new FrameDemo();
  156. }
  157.  
  158. public void windowDeactivated(WindowEvent e){
  159. }
  160.  
  161. public void windowActivated(WindowEvent e){
  162. }
  163.  
  164. public void windowDeiconified(WindowEvent e){
  165. }
  166.  
  167. public void windowIconified(WindowEvent e){
  168. }
  169.  
  170. public void windowClosed(WindowEvent e){
  171. }
  172.  
  173. public void windowClosing(WindowEvent e){
  174. System.exit(0);
  175. }
  176.  
  177. public void windowOpened(WindowEvent e){
  178. }
  179. }

i was able to add 3 more buttons thanks a lot dude!!!
Reputation Points: 10
Solved Threads: 0
Light Poster
intes77 is offline Offline
34 posts
since Feb 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Pattern Program help
Next Thread in Java Forum Timeline: Java Web Start Class not Found Exception





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC