2011-03-18 139 views
0

我在这里工作的尺寸相当不错,我正在调整其中一部分,现在我得到了这个弹出的错误框,当我尝试运行代码时。这是以前的工作,唯一改变的是“创建承包商”。我想这个错误要么会出现在该部分中,要么一直处于底部。我使用Eclipse来运行代码,运行之后,我不仅得到了弹出框,而且还在代码底部显示了一条消息:java.lang.NoSuchMethodError:线程“main”中的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) { 

} 
} 
+1

我建议你把你的代码,以更多的文件 – smas 2011-03-18 17:58:14

回答

2

将您的主要方法移动到类“测试”(从承包商)并将声明更改为静态。顺便说一句。关于java的命名约定类名应以大写字母开头

改变你的469线:

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

要:

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

你能告诉我你正在谈论哪些代码行吗?我对编码相当陌生,它已经让我永远得到这一点。你是指底部的主体?谢谢 – Mike 2011-03-18 18:03:43

+0

@Mike,看到第一个答案张贴 - 你只有一个主要() - 这是你应该更新:) – KevinDTimm 2011-03-18 18:07:36

+0

@Mike,回答 – smas 2011-03-18 18:10:07

5

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

编辑:我建议你解决您的格式在你的IDE自动格式化。这会让主要方法的错误放置更加明显。

+3

彼得,这是OP和解决方案之间的太多时间:) – KevinDTimm 2011-03-18 17:50:33

+0

我不知道为什么每个人都喜欢投票只有信誉高的人 - 其他人也可以有权:) – smas 2011-03-18 18:39:27

+1

@smas,我发布第一个倾向于获得更多选票的人。后来的答案更详细。 – 2011-03-19 21:53:12

1
public void main(String[] args){ 

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

相关问题