发生致命异常

时间:2011-03-18 17:48:09

标签: java

我有一个非常好的大小项目,我正在这里工作,我正在调整一个部分,现在我正在使用当我尝试运行代码时弹出的错误框。它之前正在工作,我改变的唯一是“创建承包商”。我认为错误要么在该部分,要么在底部。我正在使用Eclipse来运行代码,在运行它之后,我不仅会弹出该框,而且代码底部会显示一条消息:java.lang.NoSuchMethodError:主要异常在线程“main”中

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

public class test extends JFrame implements ActionListener{
private JTabbedPane jtabbedPane;
private JPanel General;
private JPanel Pools;
private JPanel HotTub;
private JPanel Customers;
private JPanel Contractors;

JTextField lengthTextPool, widthTextPool, depthTextPool, volumeTextPool,
lengthTextHotTub, widthTextHotTub, depthTextHotTub, volumeTextHotTub;

JTextArea NameTextCustomers, ExistTextCustomers;


public test(){
setTitle("Volume Calculator");
setSize(300, 200);

JPanel topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

createGeneral();
createPools();
createHotTub();
createCustomers();
createContractors();

jtabbedPane = new JTabbedPane();
jtabbedPane.addTab("General", General);
jtabbedPane.addTab("Pool", Pools);
jtabbedPane.addTab("Hot Tub", HotTub);
jtabbedPane.addTab("Customers", Customers);
jtabbedPane.addTab("Contractors", Contractors);
topPanel.add(jtabbedPane, BorderLayout.CENTER);
              }

/*        GENERAL        */

public void createGeneral(){
    General = new JPanel();
    General.setLayout( null );

    JLabel DateLabel = new JLabel("Today's Date");
    DateLabel.setBounds(10, 15, 150, 20);
    General.add( DateLabel );
    JFormattedTextField date = new JFormattedTextField(
            java.util.Calendar.getInstance().getTime());
            date.setEditable(false);
            date.setBounds(90,15,150,20);
            General.add(date);

            JButton Exit = new JButton("Exit");
            Exit.setBounds(20,50,80,20);
            Exit.addActionListener(this);
            Exit.setBackground(Color.white);
            General.add(Exit);
                                     }
/*        CREATE POOL        */

public void createPools(){
    Pools = new JPanel();
    Pools.setLayout( null );
JLabel lengthLabelPool = new JLabel("Length of pool (ft):");
    lengthLabelPool.setBounds(10, 15, 260, 20);
    Pools.add( lengthLabelPool );
lengthTextPool = new JTextField();
    lengthTextPool.setBounds(180, 15, 150, 20);
    Pools.add( lengthTextPool );
JLabel widthLabelPool = new JLabel("Width of pool (ft):");
    widthLabelPool.setBounds(10, 40, 260, 20);
    Pools.add( widthLabelPool );
widthTextPool = new JTextField();
    widthTextPool.setBounds(180, 40, 150, 20);
    Pools.add( widthTextPool );
JLabel depthLabelPool = new JLabel("Average Depth of pool (ft):");
    depthLabelPool.setBounds( 10, 65, 260, 20 );
    Pools.add( depthLabelPool );
depthTextPool = new JTextField();
    depthTextPool.setBounds(180, 65, 150, 20);
    Pools.add( depthTextPool );
JLabel volumeLabelPool = new JLabel("The pool's volume is:(ft ^3");
    volumeLabelPool.setBounds(10, 110, 260, 20);
    Pools.add( volumeLabelPool );
    volumeTextPool = new JTextField();
    volumeTextPool.setBounds(180, 110, 150, 20);
    volumeTextPool.setEditable(false);
Pools.add(volumeTextPool);

JButton calcVolumePool = new JButton("Calculate Pool Volume");
    calcVolumePool.setBounds(20,250,180,20);
    calcVolumePool.addActionListener(this);
    calcVolumePool.setBackground(Color.white);
    Pools.add(calcVolumePool);

JButton Exit = new JButton("Exit");
    Exit.setBounds(220,250,80,20);
    Exit.addActionListener(this);
    Exit.setBackground(Color.white);
    Pools.add(Exit);
                       }

/*        CREATE HOT TUB        */

public void createHotTub(){
    HotTub = new JPanel();
    HotTub.setLayout( null );
JLabel lengthLabel2 = new JLabel("Length of Hot Tub (ft):");
    lengthLabel2.setBounds(10, 15, 260, 20);
    HotTub.add( lengthLabel2 );
lengthTextHotTub = new JTextField();
    lengthTextHotTub.setBounds(180, 15, 150, 20);
    HotTub.add( lengthTextHotTub );
JLabel widthLabelHotTub = new JLabel("Width of Hot Tub (ft):");
    widthLabelHotTub.setBounds(10, 40, 260, 20);
    HotTub.add( widthLabelHotTub );
widthTextHotTub = new JTextField();
    widthTextHotTub.setBounds(180, 40, 150, 20);
    HotTub.add( widthTextHotTub );
JLabel depthLabelHotTub = new JLabel("Average Depth of Hot Tub (ft):");
    depthLabelHotTub.setBounds( 10, 65, 260, 20 );
    HotTub.add( depthLabelHotTub );
depthTextHotTub = new JTextField();
    depthTextHotTub.setBounds(180, 65, 150, 20);
    HotTub.add( depthTextHotTub );
JLabel volumeLabelHotTub = new JLabel("The Hot Tub's volume is:(ft ^3");
    volumeLabelHotTub.setBounds(10, 110, 260, 20);
    HotTub.add( volumeLabelHotTub );
    volumeTextHotTub = new JTextField();
    volumeTextHotTub.setBounds(180, 110, 150, 20);
    volumeTextHotTub.setEditable(false);
    HotTub.add(volumeTextHotTub);

JButton calcVolume = new JButton("Calculate Hot Tub Volume");
    calcVolume.setBounds(20,250,180,20);
    calcVolume.addActionListener(this);
    calcVolume.setBackground(Color.white);
    HotTub.add(calcVolume);

JButton Exit = new JButton("Exit");
    Exit.setBounds(220,250,80,20);
    Exit.addActionListener(this);
    Exit.setBackground(Color.white);
    HotTub.add(Exit);
                       }
/*        CREATE CUSTOMERS        */

public void createCustomers(){
    Customers = new JPanel();
    Customers.setLayout( null );

    NameTextCustomers = new JTextArea();
    NameTextCustomers.setBounds(10, 10, 350, 150);
    NameTextCustomers.setLineWrap(true);
    Customers.add(NameTextCustomers);

    JButton Exit = new JButton("Exit");
    Exit.setBounds(30,170,80,20);
    Exit.addActionListener(this);
    Exit.setBackground(Color.white);
    Customers.add(Exit);

    JButton AddCustomers = new JButton("Add Customer");
    AddCustomers.setBounds(130,170,120,20);
    AddCustomers.setBackground(Color.white);
    Customers.add(AddCustomers);

    JButton Refresh = new JButton("Refresh");
    Refresh.setBounds(260,170,80,20);
    Refresh.setBackground(Color.white);
    Customers.add(Refresh);

    ExistTextCustomers = new JTextArea();
    ExistTextCustomers.setBounds(10, 200, 350, 60);
    ExistTextCustomers.setLineWrap(true);
    Customers.add(ExistTextCustomers);

}
/*        CREATE CONTRACTORS        */

public JPanel createContractors(){ 
    final JTextArea contArea = new JTextArea(6, 30); 
    final JTextArea contMessage; 
    JButton addContractor = new JButton("Add Contractor"); 

    addContractor.setMnemonic('a'); 

JPanel contPanel = new JPanel(); 
contArea.setText("Select Add Contractor to add contractor. Select Refresh to refresh this pane."); 
contArea.setForeground(Color.orange); 
contArea.setLineWrap(true); 
contArea.setWrapStyleWord(true); 
JButton contRefButton = new JButton("Refresh"); 
contMessage = new JTextArea(2, 30); 
contMessage.setLineWrap(true); 
contMessage.setWrapStyleWord(true); 

addContractor.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ // action if button is used 
new Contractor("Contractor"); 
}// end actionPerformed()//end action performed 
}); // end performed action 

contPanel.add(contArea); 
contPanel.add(addContractor); 
contPanel.add(contRefButton); 
contPanel.add(contMessage); 
contRefButton.setMnemonic('R'); 

contRefButton.addActionListener(new ActionListener() 
{ 
public void actionPerformed(ActionEvent e) 
{ 
contMessage.setText(""); 
try
{ 
File contOpen = new File("contractor.txt"); 
FileReader contAreaIn = new FileReader(contOpen); 
contArea.read(contAreaIn, contOpen.toString()); 
contMessage.setText("The file exists and can be read from."); 
} 
catch (IOException e3) 
{ 
contMessage.setText("The file can't be read." + e3.getMessage()); 
} 
} 
}); 

return contPanel; 
} 

class Contractor extends JFrame 
{ 
private String[] states = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", 
"FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", 
"MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", 
"NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", 
"TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" }; 
private JComboBox StateList = new JComboBox(states); 
private JTextField NameText = new JTextField(25); 
private JTextField AddressText = new JTextField(25); 
private JTextField CityText = new JTextField(25); 
private JTextField ZipText = new JTextField(9); 
private JTextField PhoneText = new JTextField(10); 
private JTextField PopMessageText = new JTextField(30); 
private static final long serialVersionUID = 1L; 

private AddContButtonHandler addContHandler = new AddContButtonHandler(); 

public Contractor(String who) 
{ 
popUpWindow(who); 
} 

public void popUpWindow(final String who) { 

final JFrame popWindow; 
popWindow = new JFrame(who); 
popWindow.setSize(425, 350); 
popWindow.setLocation(100, 100); 
popWindow.setVisible(true); 
setDefaultCloseOperation(EXIT_ON_CLOSE); 

Container A = new Container(); 

popWindow.add(A); 

A.setLayout(new FlowLayout()); 

JPanel Name = new JPanel(); 
JPanel Address = new JPanel(); 
JPanel City = new JPanel(); 
JPanel SZP = new JPanel(); 
JPanel File = new JPanel(); 
JPanel Message = new JPanel(); 

Name.add(new JLabel(who + "Name")); 
Name.add(NameText); 
Address.add(new JLabel("Address")); 
Address.add(AddressText); 
City.add(new JLabel("City")); 
City.add(CityText); 
SZP.add(new JLabel("State")); 
StateList.setSelectedIndex(0); 
SZP.add(StateList); 
SZP.add(new JLabel("Zip")); 
SZP.add(ZipText); 
SZP.add(new JLabel("Phone")); 
SZP.add(PhoneText); 
JButton addwho = new JButton("Add" + who); 
addwho.setMnemonic('A'); 
JButton close = new JButton("Close"); 
close.setMnemonic('C'); 
JButton deleteFile = new JButton("Delete File"); 
deleteFile.setMnemonic('D'); 
File.add(addwho); 
File.add(close); 
File.add(deleteFile); 
        PopMessageText.setEditable(false); 
        PopMessageText.setHorizontalAlignment(JTextField.CENTER); 

Message.add(PopMessageText); 
    A.add(Name); 
    A.add(Address); 
    A.add(City); 
    A.add(SZP); 
    A.add(File); 
    A.add(Message); 

deleteFile.setToolTipText("Delete File"); 
addwho.setToolTipText("Add "+ who); 
close.setToolTipText("Close"); 

if (who == "Contractor") 
    addwho.addActionListener(addContHandler); // registers listener 

close.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
    NameText.setText(""); 
    AddressText.setText(""); 
    CityText.setText(""); 
    ZipText.setText(""); 
    PhoneText.setText(""); 
    PopMessageText.setText(""); 
    popWindow.dispose(); 
                                          } 
                   }); 

deleteFile.addActionListener(new ActionListener() { 
public void actionPerformed(ActionEvent e) { 
PopMessageText.setText(""); 
if (who == "Contractor") { 
    File file = new File("Contractor.txt"); 
boolean contFileDeleted = file.delete(); 
if (contFileDeleted) { 
    PopMessageText 
    .setText("Contractor file deleted"); 
               } else { 
                       PopMessageText 
                       .setText("Error deleting file"); 
        } 
                         } 
                                        } 
                         }); 
}

class AddContButtonHandler implements ActionListener { 
public void actionPerformed(ActionEvent addContHandler) { 
int StateIndex; 
try { 
File file = new File("Contractor.txt"); 

boolean success = file.createNewFile(); 

if (success) { 
   PopMessageText 
    .setText("Contractor.txt file created file added"); 
                            } else if (file.canWrite()) { 
    PopMessageText 
    .setText("Writing info. to Contractor.txt, file added"); 
                            } else { 
                              PopMessageText.setText("Cannot create file: Contractor.txt"); 
       } 
try { 
FileWriter fileW = new FileWriter("Contractor.txt", true); 
fileW.write(NameText.getText()); 
fileW.write(","); 
fileW.write(AddressText.getText()); 
fileW.write(","); 
fileW.write(CityText.getText()); 
fileW.write(","); 
StateIndex = StateList.getSelectedIndex(); 
fileW.write(states[StateIndex]); 
fileW.write(","); 
fileW.write(ZipText.getText()); 
fileW.write(","); 
fileW.write(PhoneText.getText()); 
fileW.write("\r\n"); 
fileW.close(); 
PopMessageText.setText("Contractor has been added!"); 

FileReader fileR = new FileReader("Contractor.txt"); 
BufferedReader buffIn = new BufferedReader(fileR); 

buffIn.readLine(); 
buffIn.close(); 
   }

catch (IOException e1) { 
JOptionPane.showMessageDialog(null, e1.getMessage(), 
"Error", 2);
                       }
    NameText.setText(""); 
    AddressText.setText(""); 
    CityText.setText(""); 
    ZipText.setText(""); 
    PhoneText.setText(""); 
} catch (IOException e1) { 
                         }

}
}


public void actionPerformed(ActionEvent event){
JButton button = (JButton)event.getSource();
String buttonLabel = button.getText();
if ("Exit".equalsIgnoreCase(buttonLabel)){
Exit_pressed(); return;
    }
    if ("Calculate Pool Volume".equalsIgnoreCase(buttonLabel)){
        Calculate_VolumePool(); return;
    }
        if ("Calculate Hot Tub Volume".equalsIgnoreCase(buttonLabel)){
            Calculate_VolumeHotTub(); return;
    }
                                             }
private void Exit_pressed(){
System.exit(0);
                           }
private void Calculate_VolumePool(){
String lengthStringPool, widthStringPool, depthStringPool;
    int lengthPool=0;
    int widthPool=0;
    int depthPool=0;
lengthStringPool = lengthTextPool.getText();
widthStringPool = widthTextPool.getText();
depthStringPool = depthTextPool.getText();
if (lengthStringPool.length() < 1 || widthStringPool.length() < 1 || depthStringPool.length() < 1 ){
    volumeTextPool.setText("Enter All 3 Numbers"); return;
    }
        lengthPool = Integer.parseInt(lengthStringPool);
        widthPool = Integer.parseInt(widthStringPool);
        depthPool = Integer.parseInt(depthStringPool);
            if (lengthPool != 0 || widthPool != 0 || depthPool != 0 ){
                volumeTextPool.setText((lengthPool * widthPool * depthPool) + "");
    } else{
        volumeTextPool.setText("Enter All 3 Numbers"); return;
      }
                               }

private void Calculate_VolumeHotTub(){
    String lengthStringHotTub, widthStringHotTub, depthStringHotTub;
        int lengthHotTub=0;
        int widthHotTub=0;
        int depthHotTub=0;
    lengthStringHotTub = lengthTextHotTub.getText();
    widthStringHotTub = widthTextHotTub.getText();
    depthStringHotTub = depthTextHotTub.getText();
    if (lengthStringHotTub.length() < 1 || widthStringHotTub.length() < 1 || depthStringHotTub.length() < 1 ){
        volumeTextHotTub.setText("Enter All 3 Numbers"); return;
        }
            lengthHotTub = Integer.parseInt(lengthStringHotTub);
            widthHotTub = Integer.parseInt(widthStringHotTub);
            depthHotTub = Integer.parseInt(depthStringHotTub);
                if (lengthHotTub != 0 || widthHotTub != 0 || depthHotTub != 0 ){
                    volumeTextHotTub.setText((lengthHotTub * widthHotTub * depthHotTub) + "");
        } else{
            volumeTextHotTub.setText("Enter All 3 Numbers"); return;
          }
                                   }
public void main(String[] args){
JFrame frame = new test();
frame.setSize(380, 350);
frame.setVisible(true);
}
}
public void actionPerformed(ActionEvent e) {

}
}

3 个答案:

答案 0 :(得分:5)

main()必须是静态的.......

编辑:我建议您在IDE中使用自动格式化程序修复格式。这会使主方法的错误放置更加明显。

答案 1 :(得分:2)

将main方法移至class“test”(来自Contractor)并将声明更改为static。顺便说一句。关于java命名约定类名应该以大写字母

开头

更改您的469行:

public void main(String[] args) // bad

要:

public static void main(String[] args) // good

答案 2 :(得分:1)

public void main(String[] args){

方法main应该是静态的! : - )