-1

我在 Dr. Java 中创建了一个项目,编译后可以自动转换为 Jar 文件,然后再转换为 Jar 文件,.exe但是我不断收到此错误:

 Illegal class literal

当我尝试测试运行项目时,知道为什么会发生这种情况吗?

这是主要课程:

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

public class CoolingTowerFrame extends JFrame
{ 
  public JLabel testLabel = new JLabel("All Temperatures in (deg)F");
  public JLabel testCalculationsLabel = new JLabel("");
  public JPanel testPanel = new JPanel();
  public JPanel textFieldPanel = new JPanel();
  public JPanel flowRatePanel = new JPanel();
  public JPanel wetBulbTempPanel = new JPanel();
  public JPanel waterInTempPanel = new JPanel();
  public JPanel waterOutTempPanel = new JPanel();
  public JPanel buttonPanel = new JPanel();
  public JTextField flowRateField = new JTextField(10);
  public JTextField wetBulbTempField = new JTextField(10);
  public JTextField waterInTempField = new JTextField(10);
  public JTextField waterOutTempField = new JTextField(10);
  public JLabel flowRateLabel = new JLabel();
  public JLabel wetBulbTempLabel = new JLabel();
  public JLabel waterInTempLabel = new JLabel();
  public JLabel waterOutTempLabel = new JLabel();
  public JButton submitButton = new JButton("Check Calculations");
  public JButton clearButton = new JButton("Clear");
  public JButton unknownWetBulbButton = new JButton("Unknown");
  public JButton findButton = new JButton("Find Unit");
  public int roundCoolingTons;
  public int approach;
  public int range;
  public int wetBulbTemp;
  public int flowRate;

  public CoolingTowerFrame(String title, int frameWidth, int frameHeight)
  {
    super(title);
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimensions = toolkit.getScreenSize();
    int x = (dimensions.width - frameWidth)/2; 
    int y = (dimensions.height - frameHeight)/2;
    setBounds(x, y, frameWidth, frameHeight);

    submitButton.addActionListener(new ButtonListener());
    clearButton.addActionListener(new ButtonListener());
    unknownWetBulbButton.addActionListener(new ButtonListener());
    findButton.addActionListener(new ButtonListener());

    flowRateLabel.setFont(new Font("Arial", Font.BOLD, 12));
    flowRateLabel.setText("Flow Rate (GPM) :");
    flowRatePanel.add(flowRateLabel);
    flowRatePanel.add(flowRateField);
    textFieldPanel.add(flowRatePanel);

    wetBulbTempLabel.setFont(new Font("Arial", Font.BOLD, 12));
    wetBulbTempLabel.setText("Wet Bulb:");
    wetBulbTempPanel.add(wetBulbTempLabel);
    wetBulbTempPanel.add(wetBulbTempField);
    wetBulbTempPanel.add(unknownWetBulbButton);
    textFieldPanel.add(wetBulbTempPanel);

    waterInTempLabel.setFont(new Font("Arial", Font.BOLD, 12));
    waterInTempLabel.setText("Water In:");
    waterInTempPanel.add(waterInTempLabel);
    waterInTempPanel.add(waterInTempField);
    textFieldPanel.add(waterInTempPanel);

    waterOutTempLabel.setFont(new Font("Arial", Font.BOLD, 12));
    waterOutTempLabel.setText("Water Out:");
    waterOutTempPanel.add(waterOutTempLabel);
    waterOutTempPanel.add(waterOutTempField);
    textFieldPanel.add(waterOutTempPanel);

    add(textFieldPanel, BorderLayout.CENTER);

    buttonPanel.add(submitButton);
    buttonPanel.add(clearButton);
    buttonPanel.add(findButton);

    add(buttonPanel, BorderLayout.SOUTH); 

    testPanel.add(testLabel);
    testPanel.add(testCalculationsLabel);
    add(testPanel, BorderLayout.NORTH);
  }

  public CoolingTowerFrame(String stringWetBulb)
  {
    super("Cooling Tower Selection Program");
    Toolkit toolkit = Toolkit.getDefaultToolkit();
    Dimension dimensions = toolkit.getScreenSize();
    int x = (dimensions.width - 315)/2; 
    int y = (dimensions.height - 250)/2;
    setBounds(x, y, 315, 250); 
    setVisible(true);
    setResizable(false);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    wetBulbTempField.setText(stringWetBulb);
    wetBulbTempField.setEditable(false);

    submitButton.addActionListener(new ButtonListener());
    clearButton.addActionListener(new ButtonListener());
    unknownWetBulbButton.addActionListener(new ButtonListener());
    findButton.addActionListener(new ButtonListener());

    flowRateLabel.setFont(new Font("Arial", Font.BOLD, 12));
    flowRateLabel.setText("Flow Rate (GPM):");
    flowRatePanel.add(flowRateLabel);
    flowRatePanel.add(flowRateField);
    textFieldPanel.add(flowRatePanel);

    wetBulbTempLabel.setFont(new Font("Arial", Font.BOLD, 12));
    wetBulbTempLabel.setText("Wet Bulb:");
    wetBulbTempPanel.add(wetBulbTempLabel);
    wetBulbTempPanel.add(wetBulbTempField);
    wetBulbTempPanel.add(unknownWetBulbButton);
    textFieldPanel.add(wetBulbTempPanel);

    waterInTempLabel.setFont(new Font("Arial", Font.BOLD, 12));
    waterInTempLabel.setText("Water In:");
    waterInTempPanel.add(waterInTempLabel);
    waterInTempPanel.add(waterInTempField);
    textFieldPanel.add(waterInTempPanel);

    waterOutTempLabel.setFont(new Font("Arial", Font.BOLD, 12));
    waterOutTempLabel.setText("Water Out:");
    waterOutTempPanel.add(waterOutTempLabel);
    waterOutTempPanel.add(waterOutTempField);
    textFieldPanel.add(waterOutTempPanel);

    add(textFieldPanel, BorderLayout.CENTER);

    buttonPanel.add(submitButton);
    buttonPanel.add(clearButton);
    buttonPanel.add(findButton);

    add(buttonPanel, BorderLayout.SOUTH); 

    testPanel.add(testLabel);
    testPanel.add(testCalculationsLabel);
    add(testPanel, BorderLayout.NORTH);
  }


  private class ButtonListener implements ActionListener
  {
    public void actionPerformed(ActionEvent e)
    {
      if (e.getSource() == submitButton)
      {
        try
        {
          flowRate = Integer.parseInt(flowRateField.getText());
          wetBulbTemp = Integer.parseInt(wetBulbTempField.getText());
          int waterInTemp = Integer.parseInt(waterInTempField.getText());
          int waterOutTemp = Integer.parseInt(waterOutTempField.getText());

          testLabel.setText("");

          double coolingTons = calculateCoolingTons(flowRate, waterInTemp, waterOutTemp);
          Double roundedTemp = new Double(coolingTons);
          roundedTemp = Math.rint(roundedTemp);
          roundCoolingTons = roundedTemp.intValue();
          approach = calculateApproach(waterOutTemp, wetBulbTemp);
          range = calculateRange(waterInTemp, waterOutTemp);

          testCalculationsLabel.setText("Cooling Tons: " + roundCoolingTons + " Approach: " + approach + " Range: " + range);
        }
        catch(NumberFormatException ex)
        {
          testLabel.setText("Improper Imput");
          testCalculationsLabel.setText("Unable to perform calculations");
        }
      }
      else if (e.getSource() == findButton)
      {
        if ((roundCoolingTons == 0) && (approach == 0) && (range == 0))
        {
          testLabel.setText("Please Fill in Text Fields to Find Unit");
        }
        else
        {
          JFrame selectUnit = new WetBulbChart(flowRate, wetBulbTemp, roundCoolingTons, approach, range);
        }
      }
      else if (e.getSource() == clearButton)
      {
        testLabel.setText("All Temperatures in (deg)F");
        flowRateField.setText("");
        wetBulbTempField.setText("");
        wetBulbTempField.setEditable(true);
        waterInTempField.setText("");
        waterOutTempField.setText("");
        testCalculationsLabel.setText("");
      }
      else
      {
        JFrame wbf1 = new WetBulbByLocationFrame1();
      }
    }
  }

  public static double calculateCoolingTons(int flowrate, int waterin, int waterout)
  {
    int differential = (waterin - waterout);
    double tons = ((differential*flowrate)/30.00);
    tons = Math.rint(tons);
    return tons;
  }

  public static int calculateApproach(int waterout, int wetbulb)
  {
    int approach = (waterout - wetbulb);
    return approach;
  }

  public static int calculateRange(int waterin, int waterout)
  {
    int range = (waterin - waterout);
    return range;
  }

  public static void main(String []args)
  {
    JFrame frame = new CoolingTowerFrame("Cooling Tower Selection Program", 315, 250);
    frame.setVisible(true);
    frame.setResizable(false);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
}

这是我按下运行后的错误截图: 在此处输入图像描述

4

2 回答 2

0

问题出在我不完全理解的 drjava 上。我敢打赌,如果您在控制台中输入“运行 CoolingTowerFrame”,它会正常工作。

于 2013-05-24T19:16:37.790 回答
0

我也遇到过这个错误。快把我逼疯了。我会在 DrJava 中建立一个项目,其中包含一个以我的游戏名称命名的文件夹,其中包含两个文件夹:srcclass。我将 Game.class 文件设置为错误的主类。一旦我更正了项目属性 - 将主类更改为 Game.java,错误就消失了。

于 2017-03-11T20:27:17.777 回答