JCheckBox,我的构造函数有问题。

JCheckBox, something wrong with my constructor.

中间的"public Dorm()"区域报错。在声明构造函数之前我是否遗漏了什么?我不明白这是一个非法的表达式开始。

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

您的构造函数在 main 方法中。 将构造函数移动到 class 级别。

你应该将构造函数放在 method.like 之外 this.and 在 main 方法中创建新对象。

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

  }