Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 01-01-2009, 08:24 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Posts: 26
Default Working with Panel in Java


I have written a Java code.The following is my code...


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;


public class Game extends Frame{
public Panel x;
Panel pi,pi1,pi2,pi3,pi4;
public Game(){
pi=new Panel();
add(pi);
pi1= new Panel(new GridLayout(4,0));
pi2= new Panel();
pi3= new Panel(new GridLayout(4,4));
pi4= new Panel();
Label l3[][]=new Label[4][4];
x=pi1;
JButton b1=new JButton(" Play ");
JButton b2=new JButton(" Objective ");
JButton b3=new JButton(" Instruction ");
JButton b4=new JButton(" Exit ");
pi.add(pi1);
pi1.add(b1);
pi1.add(b2);
pi1.add(b3);
pi1.add(b4);
b1.addActionListener(new MyAction(this));
b2.addActionListener(new MyAction(this));
b3.addActionListener(new MyAction(this));
b4.addActionListener(new MyAction(this));
Label l1= new Label("This is a puzzle game.You have to arrange the numbers in proper order...");
pi2.add(l1);
JButton b5= new JButton(" Back ");
pi2.add(b5);
b5.addActionListener(new MyAction(this));
Label l2= new Label("You should use four arrow kyes to move the numbers...");
pi4.add(l2);
pi4.add(b5);
Panel p[][]= new Panel[4][4];
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
p[i][j]=new Panel();
pi3.add(p[i][j]);
p[i][j].addKeyListener(new MyKeyAdapter(this));
}
}
for(int i=0;i<4;i++)
{
for(int j=0;j<4;j++)
{
l3[i][j]=new Label(""+i);
}
}
p[0][0].add(l3[0][0]);
p[0][1].add(l3[0][3]);
p[0][2].add(l3[3][2]);
p[0][3].add(l3[1][2]);
p[1][0].add(l3[1][3]);
p[1][1].add(l3[2][1]);
p[1][2].add(l3[0][1]);
p[1][3].add(l3[2][2]);
p[2][0].add(l3[3][1]);
p[2][1].add(l3[0][2]);
p[2][2].add(l3[1][1]);
p[2][3].add(l3[3][0]);
p[3][0].add(l3[2][3]);
p[3][1].add(l3[2][0]);
p[3][2].add(l3[1][0]);
addWindowListener(new MyWindowAdapter());
}
public static void main(String args[])
{
Game g = new Game();
g.setSize(new Dimension(300, 200));
g.setTitle("A Puzzle Game");
g.setLayout(new FlowLayout(FlowLayout.CENTER));
g.setVisible(true);
}
}



class MyWindowAdapter extends WindowAdapter{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}


class MyAction implements ActionListener{
Game g;
public MyAction(Game g)
{
this.g=g;
}
public void actionPerformed(ActionEvent ae)
{
String str=ae.getActionCommand();
if(str.equals("Objective"))
{
g.pi.remove(g.x);
g.pi.add(g.pi2);
g.pi.x=g.pi2;
}
if(str.equals("Instruction"))
{
g.pi.remove(g.x);
g.pi.add(g.pi4);
g.pi.x=g.pi4;
}
if(str.equals("Exit"))
{
System.exit(0);
}
}
}


class MyKeyAdapter extends KeyAdapter{
Game g;
public MyKeyAdapter(Game g)
{
this.g=g;
}
public void keyTyped(KeyEvent ke)
{
}
}


This code is perfect when actionPerformed function in MyAction class is kept empty...
But I have to write something to obtain the desired result...I want to remove the Panels pi2,pi4 etc when buttons "Objective" or "Instructions" are pressed...
Please help ...
Adam Cruge is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 02-01-2009, 01:15 AM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Working with Panel in Java

Your Button's constructor had its name padded with spaces, so you probably have to feed the same string into the condition.

This will work (removes and stuff), but I'm not sure its what you asked for:
Code:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;


public class Game extends Frame{
    public Panel x;
    Panel pi,pi1,pi2,pi3,pi4;
    public Game(){
        pi=new Panel();
        add(pi);
        pi1= new Panel(new GridLayout(4,0));
        pi2= new Panel();
        pi3= new Panel(new GridLayout(4,4));
        pi4= new Panel();
        Label l3[][]=new Label[4][4];
        x=pi1;
        JButton b1=new JButton(" Play ");
        JButton b2=new JButton(" Objective ");
        JButton b3=new JButton(" Instruction ");
        JButton b4=new JButton(" Exit ");
        pi.add(pi1);
        pi1.add(b1);
        pi1.add(b2);
        pi1.add(b3);
        pi1.add(b4);
        b1.addActionListener(new MyAction(this));
        b2.addActionListener(new MyAction(this));
        b3.addActionListener(new MyAction(this));
        b4.addActionListener(new MyAction(this));
        Label l1= new Label("This is a puzzle game.You have to arrange the numbers in proper order...");
        pi2.add(l1);
        JButton b5= new JButton(" Back ");
        pi2.add(b5);
        b5.addActionListener(new MyAction(this));
        Label l2= new Label("You should use four arrow kyes to move the numbers...");
        pi4.add(l2);
        pi4.add(b5);
        Panel p[][]= new Panel[4][4];
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                p[i][j]=new Panel();
                pi3.add(p[i][j]);
                p[i][j].addKeyListener(new MyKeyAdapter(this));
            }
        }
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
            {
                l3[i][j]=new Label(""+i);
            }
        }
        p[0][0].add(l3[0][0]);
        p[0][1].add(l3[0][3]);
        p[0][2].add(l3[3][2]);
        p[0][3].add(l3[1][2]);
        p[1][0].add(l3[1][3]);
        p[1][1].add(l3[2][1]);
        p[1][2].add(l3[0][1]);
        p[1][3].add(l3[2][2]);
        p[2][0].add(l3[3][1]);
        p[2][1].add(l3[0][2]);
        p[2][2].add(l3[1][1]);
        p[2][3].add(l3[3][0]);
        p[3][0].add(l3[2][3]);
        p[3][1].add(l3[2][0]);
        p[3][2].add(l3[1][0]);
        addWindowListener(new MyWindowAdapter());
    }
    public static void main(String args[])
    {
        Game g = new Game();
        g.setSize(new Dimension(300, 200));
        g.setTitle("A Puzzle Game");
        g.setLayout(new FlowLayout(FlowLayout.CENTER));
        g.setVisible(true);
    }
}



class MyWindowAdapter extends WindowAdapter{
    public void windowClosing(WindowEvent we)
    {
        System.exit(0);
    }
}


class MyAction implements ActionListener{
    Game g;
    public MyAction(Game g)
    {
        this.g=g;
    }
    public void actionPerformed(ActionEvent ae)
    {
        String str=ae.getActionCommand();
        if(str.equals(" Objective "))
        {
            g.pi.remove(g.x);
            g.pi.add(g.pi2);
            g.pi=g.pi2;
        }
        if(str.equals(" Instruction "))
        {
            g.pi.remove(g.x);
            g.pi.add(g.pi4);
            g.pi=g.pi4;
        }
        if(str.equals(" Exit "))
        {
            System.exit(0);
        }
    }
}


class MyKeyAdapter extends KeyAdapter{
    Game g;
    public MyKeyAdapter(Game g)
    {
        this.g=g;
    }
    public void keyTyped(KeyEvent ke)
    {
    }
}
I've never used AWT much.

And please, do learn to indent while typing code!
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 02-01-2009, 09:57 AM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Posts: 26
Default Re: Working with Panel in Java

Thank you for your help...
But one thing is still there is one problem...The panels are removed but new panels are not added until I maximize the window.When I maximize the window then the new panels are added.When I again make the window be to its previous size I can see the panel that I wanted to add...Can you please help me with this problem???
Adam Cruge is offline  
Old 08-01-2009, 05:16 PM   #4 (permalink)
Right Off the Assembly Line
 
Join Date: Feb 2008
Posts: 26
Default Re: Working with Panel in Java

When u run the Java code you will find that whenever buttons are pressed the window get blank,that I wanted to do intentionally,but I also wanted it to display something whenever buttons are pressed.But those thing is displayed only when I maximize or minimize the window.Can anybody solve this problem?
Adam Cruge is offline  
Old 08-01-2009, 11:46 PM   #5 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Working with Panel in Java

http://java.sun.com/javase/6/docs/ap...ml#invalidate()
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java programming..how can I distribute java program....? Manojap Programming 5 16-05-2009 02:46 PM
java program not working in eclipse ilugd QnA (read only) 1 27-10-2007 10:44 PM
java Script help, Drop down and check box not working. bukaida QnA (read only) 3 14-09-2006 01:55 PM
OPERA MINI WORKING & ALL JAVA SOFTWARE CONNECT TO INTERNET vamsee Mobiles and Tablets 3 18-01-2006 07:35 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:04 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2