954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

add and remove components...

whats wrong with these code?
the buttons don't add or remove the panel..
someone help me!??plss..:S

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

public class addremove extends JPanel implements ActionListener{
    private JButton addbut;
    private JButton revbut;
    private JPanel panel1;
    private JPanel panel2;

    public addremove() {

        addbut = new JButton ("Add");
        revbut = new JButton ("Remove");
        panel1 = new JPanel();
		panel2 = new JPanel();

		panel1.setBackground(Color.blue);
		panel2.setBackground(Color.red);

        setPreferredSize (new Dimension (218, 160));
        setLayout (null);


        add (addbut);
        add (revbut);
        add (panel2);


        addbut.setBounds (20, 120, 80, 25);
        revbut.setBounds (120, 120, 80, 25);
        panel1.setBounds (20,10,180,60);
        panel2.setBounds (20,10,180,60);
    }

    public void actionPerformed(ActionEvent e)
    {
		if(e.getSource() == addbut)
		{
			remove(panel2);

			add(panel1);

		}
		else if (e.getSource() == revbut)
		{
			remove(panel1);

			add(panel2);

		}
	}





    public static void main (String[] args) {
        JFrame frame = new JFrame ("addremove");
        frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add (new addremove());
        frame.pack();
        frame.setVisible (true);
        frame.setLocation(450,280);
        frame.setResizable(false);
    }
}
raff004
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

,.,i've already found the solution..
sorry about this stupid question..hehe

raff004
Newbie Poster
2 posts since Jan 2009
Reputation Points: 10
Solved Threads: 0
 

Hey!
greate you solved the problem. Could you tell us how? As I'm a starter in Java I need working code examples to solve problems not broken code.
hopeing you do still remember
Thank you!
Carl

HerrLehmann
Newbie Poster
1 post since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You