AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

problem in coding window

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
vasu
Guest





PostPosted: Mon Jul 18, 2005 1:30 am    Post subject: problem in coding window Reply with quote



hi ,
i am new with java and am learning it from Schildt's complete
reference, i try to convert an applet example there to a window
program, it got compiled but result is not
as expected it is only showing the last checkbox, plz help me with
corrections or correct code
import java.awt.*;
import java.awt.event.*;

public class CBGroup extends Frame
{
String msg;
Checkbox win98,winNT,solaris,mac;
//CheckboxGroup cbg=new CheckboxGroup();
public CBGroup()
{
//cbg=new CheckboxGroup();
win98=new Checkbox("Windows 98/XP",true);
winNT=new Checkbox("Windows NT/2000",false);
solaris=new Checkbox("Solaris",false);
mac=new Checkbox("MacOS",false);

add(win98);
add(winNT);
add(solaris);
add(mac);

win98.addItemListener(new myItemAdapter(this));
winNT.addItemListener(new myItemAdapter(this));
solaris.addItemListener(new myItemAdapter(this));
mac.addItemListener(new myItemAdapter(this));
//addItemListener(new myItemAdapter(this));
addWindowListener(new myWindowAdapter());
}

public void paint(Graphics g)
{
msg="Current Selection : ";
//msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);
}

public static void main(String args[])
{ CBGroup cbgroup=new CBGroup();

cbgroup.setSize(new Dimension(900,600));
cbgroup.setTitle("A Linux X Window Application");
cbgroup.setVisible(true);
}
}

class myItemAdapter implements ItemListener
{
CBGroup cbg;
public myItemAdapter(CBGroup cbg)
{
this.cbg=cbg;
}
public void itemStateChanged(ItemEvent ie)
{
cbg.repaint();
}
}

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


----------------

Back to top
Mark
Guest





PostPosted: Mon Jul 18, 2005 2:43 am    Post subject: Re: problem in coding window Reply with quote



vasu wrote:
Quote:
hi ,
i am new with java and am learning it from Schildt's complete
reference, i try to convert an applet example there to a window
program, it got compiled but result is not
as expected it is only showing the last checkbox, plz help me with
corrections or correct code
import java.awt.*;
import java.awt.event.*;

public class CBGroup extends Frame
{
String msg;
Checkbox win98,winNT,solaris,mac;
//CheckboxGroup cbg=new CheckboxGroup();
public CBGroup()
{
//cbg=new CheckboxGroup();
win98=new Checkbox("Windows 98/XP",true);
winNT=new Checkbox("Windows NT/2000",false);
solaris=new Checkbox("Solaris",false);
mac=new Checkbox("MacOS",false);

add(win98);
add(winNT);
add(solaris);
add(mac);

win98.addItemListener(new myItemAdapter(this));
winNT.addItemListener(new myItemAdapter(this));
solaris.addItemListener(new myItemAdapter(this));
mac.addItemListener(new myItemAdapter(this));
//addItemListener(new myItemAdapter(this));
addWindowListener(new myWindowAdapter());
}

public void paint(Graphics g)
{
msg="Current Selection : ";
//msg += cbg.getSelectedCheckbox().getLabel();
g.drawString(msg,6,100);
}

public static void main(String args[])
{ CBGroup cbgroup=new CBGroup();

cbgroup.setSize(new Dimension(900,600));
cbgroup.setTitle("A Linux X Window Application");
cbgroup.setVisible(true);
}
}

class myItemAdapter implements ItemListener
{
CBGroup cbg;
public myItemAdapter(CBGroup cbg)
{
this.cbg=cbg;
}
public void itemStateChanged(ItemEvent ie)
{
cbg.repaint();
}
}

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


----------------

The default layout of a Frame is the BorderLayout. You do not specify

where the Checkbox items are to be added, so I assume they all go to the
same place, and get put atop one another, so that only the last one
shows. Specify a FlowLayout just before you do your add, and you will
see your all the CheckBox items:

setLayout(new FlowLayout());

add(win98);
add(winNT);
add(solaris);
add(mac);

You will need to study layout managers, and you might want to look at
using Swing instead of the old and limited AWT.

Mark

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.