-2

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
Nick Wilson
  • 61
  • 1
  • 10

2 Answers2

2

you should put constructor outside of method.like this.and make new object inside main method.

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

public class Dorm extends JFrame implements ItemListener {


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

        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); 

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

     public static void main(String[] args){
         new Dorm();
     }

  } 
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
1

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