add and remove components...

Thread Solved

Join Date: Jan 2009
Posts: 2
Reputation: raff004 is an unknown quantity at this point 
Solved Threads: 0
raff004 raff004 is offline Offline
Newbie Poster

add and remove components...

 
0
  #1
Jan 14th, 2009
whats wrong with these code?
the buttons don't add or remove the panel..
someone help me!??plss..


  1.  
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.event.*;
  6.  
  7. public class addremove extends JPanel implements ActionListener{
  8. private JButton addbut;
  9. private JButton revbut;
  10. private JPanel panel1;
  11. private JPanel panel2;
  12.  
  13. public addremove() {
  14.  
  15. addbut = new JButton ("Add");
  16. revbut = new JButton ("Remove");
  17. panel1 = new JPanel();
  18. panel2 = new JPanel();
  19.  
  20. panel1.setBackground(Color.blue);
  21. panel2.setBackground(Color.red);
  22.  
  23. setPreferredSize (new Dimension (218, 160));
  24. setLayout (null);
  25.  
  26.  
  27. add (addbut);
  28. add (revbut);
  29. add (panel2);
  30.  
  31.  
  32. addbut.setBounds (20, 120, 80, 25);
  33. revbut.setBounds (120, 120, 80, 25);
  34. panel1.setBounds (20,10,180,60);
  35. panel2.setBounds (20,10,180,60);
  36. }
  37.  
  38. public void actionPerformed(ActionEvent e)
  39. {
  40. if(e.getSource() == addbut)
  41. {
  42. remove(panel2);
  43.  
  44. add(panel1);
  45.  
  46. }
  47. else if (e.getSource() == revbut)
  48. {
  49. remove(panel1);
  50.  
  51. add(panel2);
  52.  
  53. }
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60. public static void main (String[] args) {
  61. JFrame frame = new JFrame ("addremove");
  62. frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  63. frame.getContentPane().add (new addremove());
  64. frame.pack();
  65. frame.setVisible (true);
  66. frame.setLocation(450,280);
  67. frame.setResizable(false);
  68. }
  69. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 2
Reputation: raff004 is an unknown quantity at this point 
Solved Threads: 0
raff004 raff004 is offline Offline
Newbie Poster

Re: add and remove components...

 
0
  #2
Jan 14th, 2009
,.,i've already found the solution..
sorry about this stupid question..hehe
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC