Search code examples
javajframejcheckbox

JCheckBox, something wrong with my constructor.


The "public Dorm()" area in the middle is giving me an error. Am I missing something before declaring the constructor? I don't see how it is an illegal start of expression.

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

public class Dorm extends JFrame implements ItemListener {

public static void main(String[] args){

FlowLayout flow = new FlowLayout();
JLabel label = new JLabel("Please select items you would like for your dorm   room: ");

  JCheckBox Internet = new JCheckBox("Internet", false);
  JCheckBox Cable = new JCheckBox("\nCable", false);
  JCheckBox Microwave = new JCheckBox("\nMicrowave", false);
  JCheckBox Refridgerator = new JCheckBox("\nRefridgerator", false); 

     public Dorm(){
        super("Dorm Selections");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new FlowLayout());

        Internet.addItemListener(this);
        Cable.addItemListener(this);
        Microwave.addItemListener(this);
        Refridgerator.addItemListener(this);
        add(labale);
        add(Internet);
        add(Cable);
        add(Microwave);
        add(Retriderator);
     }   

  } 
 }

Dorm.java:22: error: illegal start of expression
     public Dorm(){
     ^
Dorm.java:22: error: ';' expected
     public Dorm(){
                  ^
2 errors

Solution

  • Your constructor is inside the main method. Move the constructor to the class level.