Solved

Please tell me what is wrong in my using cardlayout?

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 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