Solved

Please tell me what is wrong in my using cardlayout?

Posted on 2002-06-26
1
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 120
how to debug htl and js pages 8 53
Bot application - advice 3 58
Running JavaFX on the Raspberry Pi 27 127
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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…

735 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