1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: JAVA: Replacing the main window(Display)

Discussion in 'Software Development' started by sepala, Feb 20, 2011.

Thread Status:
Not open for further replies.
Advertisement
  1. sepala

    sepala Thread Starter

    Joined:
    May 20, 2010
    Messages:
    4,038
    Hi all,

    I have created a small JApplet which will change the main window when the button is clicked. But it is not working. Please help me. Here is my code

    Code:
    public class test extends JApplet implements ActionListener{
    
        JButton button1;
    
        JLabel label1;
    
        JPanel panel1,panel2;
    
        public void init()
        {
            button1 = new JButton("Click");
            button1.addActionListener(this);
            label1 = new JLabel("LBALE");
    
    
            panel1 = (JPanel)getContentPane();
            panel1.setLayout(new FlowLayout());
            panel1.add(button1);
    
            panel2 = new JPanel();
            panel2.setLayout(new FlowLayout());
            panel2.add(label1);
        }
    
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource().equals(button1))
            {
                panel2 = panel1;
            }
        }
    
    }j
     
  2. sepala

    sepala Thread Starter

    Joined:
    May 20, 2010
    Messages:
    4,038
    solved
     
  3. andythepandy

    andythepandy

    Joined:
    Jul 2, 2006
    Messages:
    379
    Could you post what you did to solve your problem? So anyone stumbling across this thread in future will be helped by it :)
     
  4. sepala

    sepala Thread Starter

    Joined:
    May 20, 2010
    Messages:
    4,038
    Hello Friend

    OK, here we go

    Code:
    import java.awt.event.*;
    
    import javax.swing.*;
    import java.awt.*;
    
    public class tester extends JApplet implements ActionListener{
    
        JButton button1;
    
        JLabel label1;
    
        JPanel card1,card2,cards;
    
       CardLayout ourLayout;
    
        public void init()
        {
            button1 = new JButton("Click");
            button1.addActionListener(this);
            label1 = new JLabel("LBALE");
    
    
            JPanel panel1 = new JPanel();
            panel1.setLayout(new FlowLayout());
            panel1.add(button1);
    
            JPanel panel2 = new JPanel();
            panel2.setLayout(new FlowLayout());
            panel2.add(label1);
    
            ourLayout = new CardLayout();
    
            card1 = new JPanel();
            card1.add(panel1);
    
            card2 = new JPanel();
            card2.add(panel2);
    
            cards = new JPanel();
            cards.setLayout(ourLayout);
            cards.add(card1,"one");
            cards.add(card2,"two");
    
            JPanel mPanel = (JPanel)getContentPane();
            mPanel.setLayout(new BorderLayout());
            mPanel.add(cards);
    
        }
    
        public void actionPerformed(ActionEvent ae)
        {
            if(ae.getSource().equals(button1))
            {
                ourLayout.show(cards,"two");
            }
        }
    
    }
    
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/981786

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice