Solved

Please tell me what is wrong in my using cardlayout?

Posted on 2002-06-26
1
293 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

757 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

19 Experts available now in Live!

Get 1:1 Help Now