Solved

Please tell me what is wrong in my using cardlayout?

Posted on 2002-06-26
1
295 Views
Last Modified: 2010-08-05
Java program:
---------------------------------

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

public class Simple extends JApplet{

    Page   p1;
    Page   p2;
    CardLayout clayout;
    Container c;


    public void init() {

       c = this.getContentPane();
        clayout = new CardLayout();

        p1 = new Page(this, "Page one");
        p2 = new Page(this, "page two");

        c.setLayout(clayout);

        clayout.addLayoutComponent(p1, "p1");
        clayout.addLayoutComponent(p2, "p2");
    }


    public void mouseClicked(MouseEvent e){
        CardLayout myLayout = (CardLayout) this.getContentPane().getLayout();
        myLayout.next(this.getContentPane());
        if (e.getSource() == p1){
            p1.label.setText("Page 1 been clicked");
        } else if (e.getSource() == p2) {
            p2.label.setText("Page 2 been clicked");
        }
    }

    public void mouseEntered(MouseEvent e){
    }

    public void mouseExited(MouseEvent e){
    }

    public void mousePressed(MouseEvent e){
    }
   
    public void mouseReleased(MouseEvent e){
    }

    public void paint(Graphics g) {
        super.paint(g);
    }
}

class Page extends JPanel {

    JLabel label;
    Simple sim;

    public Page(Simple sim, String s){
        this.sim = sim;
        label = new JLabel(s);
        //label.setBackground(Color.blue);
        //label.setBound(20, 20, 150, 100);
        this.setBackground(Color.pink);
        this.setLayout(null);
        this.add(label);
    }

    public void paint(Graphics g) {
        super.paint(g);
    }
}

html file:
----------------------

<html>
<head><title>simple</title></head>
<body>
<applet code = "Simple.class" width = "570" height = "500" >
</applet>
</body>
</html>

0
Comment
Question by:icefire
1 Comment
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 100 total points
ID: 7112202
First, don't add your components directly to the CardLayout. Add them to the contentPane and it will inform the CardLayout:

    c.add("p1", p1);
    c.add("p2", p2);

Second, JApplet does not implement the MouseListener interface. So even though you've implemented all the methods, it can't be used as a MouseListener. So the first thing you need to do is add the interface to the class declaration:

    public class Simple extends JApplet implements MouseListener {

Second, you have to add the MouseListener to components that can have mouse events. In your case, you seem to want to get events on the Pages so you have to add your Simple instance as a MouseListener on those components:

    p1.addMouseListener(this);
    p2.addMouseListener(this);

Finally, setting the Page layout to 'null' is preventing your JLabel from displaying. I'm not sure why you were doing it but it doesn't seem necessary.

After making these changes, your applet would appear to do what you intended. Here is the revised code:

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

public class Simple extends JApplet implements MouseListener {

   Page   p1;
   Page   p2;
   CardLayout clayout;
   Container c;


   public void init() {
       c = this.getContentPane();
       clayout = new CardLayout();
       c.setLayout(clayout);
       p1 = new Page(this, "Page one");
       p2 = new Page(this, "page two");
       p1.addMouseListener(this);
       p2.addMouseListener(this);
       c.add("p1", p1);
       c.add("p2", p2);
   }


   public void mouseClicked(MouseEvent e){
       CardLayout myLayout = (CardLayout) this.getContentPane().getLayout();
       myLayout.next(this.getContentPane());
       if (e.getSource() == p1){
           p1.label.setText("Page 1 been clicked");
       } else if (e.getSource() == p2) {
           p2.label.setText("Page 2 been clicked");
       }
   }

   public void mouseEntered(MouseEvent e){
   }

   public void mouseExited(MouseEvent e){
   }

   public void mousePressed(MouseEvent e){
   }

   public void mouseReleased(MouseEvent e){
   }

   public void paint(Graphics g) {
       super.paint(g);
   }
}

class Page extends JPanel {

   JLabel label;
   Simple sim;

   public Page(Simple sim, String s){
       this.sim = sim;
       label = new JLabel(s);
       //label.setBackground(Color.blue);
       //label.setBound(20, 20, 150, 100);
       this.setBackground(Color.pink);
       //this.setLayout(null);
       this.add(label);
   }

   public void paint(Graphics g) {
       super.paint(g);
   }
}

Best regards,
Jim Cakalic
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now