java swing, disappearing frame/component is driving me nuts (code inside)

mchammer187

Diamond Member
Nov 26, 2000
9,114
0
76
I wrote a applet but the all the button checkboxes and radio buttons and text fields just disappear on me when i run it

it is supposed to take in text from a JTextbox and have 2 check boxes for bold and italic and 3 radio buttons for size 14, 16, or 18

it works correctly except for the buttons either disapper or never show up but i can click where they are supposed to be and they work

any help would be appreciated


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

public class app extends Applet implements ActionListener{

protected static int fontStyle=0;
static int SIZE=14;
JTextField nameField;
JRadioButton A ;
JRadioButton B ;
JRadioButton C;
JButton doit;
JCheckBox boldCheck;
JCheckBox italicsCheck;

public void init(){
setLayout(new FlowLayout());
nameField = new JTextField ("", 30);
ButtonGroup group1 = new ButtonGroup();
A = new JRadioButton("14",true);
B = new JRadioButton("16",false);
C = new JRadioButton("18",false);
doit = new JButton("do it");

boldCheck = new JCheckBox("bold",false);
italicsCheck = new JCheckBox("italics",false);


doit.addActionListener(this);


group1.add(A);
group1.add(B);
group1.add(C);

ActionListener radioListener = new ActionListener (){
public void actionPerformed(ActionEvent evt){
Object source = evt.getSource();
if (source == A) SIZE = 14;
if (source == B) SIZE = 16;
if (source == C) SIZE = 18;

setFont(new Font("Courier", fontStyle, SIZE));
}
};

A.addActionListener(radioListener);
B.addActionListener(radioListener);
C.addActionListener(radioListener);

ActionListener listener1= new ActionListener(){
public void actionPerformed(ActionEvent evt){
fontStyle = 0;
if (italicsCheck.isSelected()){
fontStyle+=Font.ITALIC;
System.out.println("italic is on");
}


if (boldCheck.isSelected()){
fontStyle+=Font.BOLD;
System.out.println("bold is on");
}

setFont(new Font("Courier", fontStyle , SIZE));
}
};


boldCheck.addActionListener(listener1);
italicsCheck.addActionListener(listener1);


add(A);
add(B);
add(C);
add(nameField);
add(doit);
add(boldCheck);
add(italicsCheck);


setSize (500,500);
}

public void paint(Graphics g){
g.setFont(new Font("Helvetica", fontStyle, SIZE));
g.drawString(nameField.getText().trim(),30,100);
show();
}

public void actionPerformed(ActionEvent evt){
if(evt.getSource() == doit){
repaint();
}
}
}