checkboxes

0 Edwin Charles · February 25, 2015
i'm sorry bucky but when i run this pgm i'm getting a window with only one check box and no text field and the other check box 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class tuna extends JFrame{
private JTextField tf;
private JCheckBox boldbox;
private JCheckBox italicbox;

public tuna(){
super("CHECKBOX");

tf = new JTextField("This is a sentence !!!",20);
tf.setFont(new Font("seriff",Font.PLAIN,14));
add(tf);

boldbox = new JCheckBox("BOLD");
italicbox = new JCheckBox("ITALIC");
add(boldbox);
add(italicbox);

HandlerClass handler = new HandlerClass();
boldbox.addItemListener(handler);
italicbox.addItemListener(handler);
}

private class HandlerClass implements ItemListener{
public void itemStateChanged(ItemEvent event){
Font font = null;

if(boldbox.isSelected() && italicbox.isSelected())
font= new Font("seriff", Font.BOLD + Font.ITALIC, 14);
else if(boldbox.isSelected())
font= new Font("seriff", Font.BOLD, 14);
else if(italicbox.isSelected())
font= new Font("seriff", Font.ITALIC, 14);
else
font= new Font("seriff",Font.PLAIN,14);

tf.setFont(font);
}
}


}

i've not shown the main class...
PLZZZZ HELP!!!!

Post a Reply

Replies

Oldest  Newest  Rating
0 Nicholas Eason · February 25, 2015
Well, your class has no Layout... so it's on a BorderLayout. I'm pretty sure since you didn't set any "Layout Parameters" when adding the components they all default to BorderLayout.CENTER.

You have a stack of books. You can only see the books from the top. Place another book in, and you can't see the previous one, but it's not invisible!

That's what you've essentially done here, you've just stacked components on top of each other, with the most recently added one on top.

To fix this, in each of the add statements:

add(Component, BorderLayout.CENTER/BorderLayout.SOUTH/BorderLayout.NORTH/etc...)


Note that they have to be in different parts for them to be visible.
  • 1

Java / Android Development

107,178 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator