score:1

Accepted answer

the problem is that you have duplicate variables home and clients .

the folllowing is your modified code to fix that, with comments on the changed lines (five lines total) :

import java.awt.cardlayout;
import java.awt.eventqueue;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;

import javax.swing.jbutton;
import javax.swing.jframe;
import javax.swing.jpanel;
import javax.swing.border.emptyborder;

public class ia extends jframe {

    private final jpanel contentpane;
    //   private final jpanel home;  // removed
    //   private jpanel clients;  // removed

    /**
     * launch the application.
     */
    public static void main(final string[] args) {
        eventqueue.invokelater(new runnable() {
            public void run() {
                try {
                    ia frame = new ia();
                    frame.setvisible(true);
                } catch (exception e) {
                    e.printstacktrace();
                }
            }
        });
    }

    /**
     * create the frame.
     */
    public ia() {
        setdefaultcloseoperation(jframe.exit_on_close);
        setbounds(100, 100, 450, 300);
        contentpane = new jpanel();
        contentpane.setborder(new emptyborder(5, 5, 5, 5));
        setcontentpane(contentpane);
        contentpane.setlayout(new cardlayout(0, 0));

        final jpanel home = new jpanel();
        contentpane.add(home, "name_714429679706141");
        home.setlayout(null);

        final jpanel clients = new jpanel();  // moved up
        contentpane.add(clients, "name_714431450350356");  // moved up
        clients.setlayout(null);  // moved up

        jbutton btnclients = new jbutton("clients");
        btnclients.addactionlistener(new actionlistener() {
            public void actionperformed(final actionevent e) {
                home.setvisible(false);
                clients.setvisible(true);
            }
        });
        btnclients.setbounds(160, 108, 89, 23);
        home.add(btnclients);

        jbutton btnhome = new jbutton("home");
        btnhome.addactionlistener(new actionlistener() {
            public void actionperformed(final actionevent e) {
                clients.setvisible(false);
                home.setvisible(true);
            }
        });
        btnhome.setbounds(169, 107, 89, 23);
        clients.add(btnhome);
    }

}

score:0

i would take a look at this post, however i have a feeling you'll need to use a actionlistener to get this done... java swing. opening a new jpanel from a jbutton and making the buttons pretty i would of left this as a comment but apparently you need 50 rep for that...

this link might be more helpful.. how to open a new window by clicking a button

score:0

when the following code is invoked the clients variable equals to null.

jbutton btnclients = new jbutton("clients");
btnclients.addactionlistener(new actionlistener() {
    public void actionperformed(actionevent e) {
        home.setvisible(false);
        clients.setvisible(true);
    }
});

write this:

jpanel clients = new jpanel();
contentpane.add(clients, "name_714431450350356");
clients.setlayout(null);
jbutton btnhome = new jbutton("home");
btnhome.setbounds(169, 107, 89, 23);
clients.add(btnhome);

before you add the action listener


Related Query

More Query from same tag