can a JCheckbox be added to a JFrame?

23 Views Asked by At

I am new to programming, and I am wondering why I am getting an error when I try to add the checkBox oject to the JFrame object in the program:

import javax.swing.*;  

public class MyCheckBoxExample   
{  
    public static void main(String[] args)  
   {  
        // Create a JFrame  
        JFrame frame = new JFrame("JCheckBox Example");  
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        frame.setSize(300, 200);  
        frame.setLayout(new java.awt.FlowLayout());  

        // Create a JCheckBox  
        JCheckBox checkBox = new JCheckBox("I am a checkbox");  

        // Add the JCheckBox to the JFrame  
        frame.add(checkBox); // error on 'add'  

        // Make the JFrame visible  
        frame.setVisible(true);  
        }  
 }  

I tried casting (Component) to the checkBox object, but the program still has a red underline under 'add' .

0

There are 0 best solutions below