?
Solved

Please tell me what is wrong in my using cardlayout?

Posted on 2002-06-26
1
Medium Priority
?
302 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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses

718 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