2011-06-24 94 views
-3

Possible Duplicate:
Errors running Java program使用Eclipse創建一個Java程序,不能使用NetBeans

我目前正在寫使用eclipse java程序運行它。我聽說使用netbeans製作java gui更容易,所以我想用netbeans來完成這個java程序。但是,當我使用netbeans打開我的項目並運行它時,它給我一個錯誤。試圖使用betbeans運行相同的程序,在日食下運行良好。

這是錯誤:

run: 
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException 
    at javax.swing.ImageIcon.<init>(ImageIcon.java:181) 
    at MainForm.addComponentsToPane(MainForm.java:28) 
    at MainForm.createAndShowGUI(MainForm.java:112) 
    at MainForm.access$000(MainForm.java:15) 
    at MainForm$4.run(MainForm.java:125) 
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:641) 
    at java.awt.EventQueue.access$000(EventQueue.java:84) 
    at java.awt.EventQueue$1.run(EventQueue.java:602) 
    at java.awt.EventQueue$1.run(EventQueue.java:600) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:611) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122) 
BUILD SUCCESSFUL (total time: 8 seconds) 

這是即時通訊的代碼搭售運行:

import java.awt.Color; 
import java.awt.Container; 
import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ImageIcon; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 




public class MainForm { 

    public static void addComponentsToPane(final JFrame frame, Container pane) { 
     Color colorGreen = new Color(0, 100, 0); 
     Color colorBrown = new Color(150, 100, 0); 
     //Color colorBlue = new Color (0, 0, 150); 
     //Font font = new Font("Verdana", Font.BOLD, 12); 

     pane.setLayout(null); 
     pane.setBackground(new Color (255, 255, 170)); 

     //add image and necessary labels on top left corner 
     //SHA Image and label 
     ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));  
     JLabel label = new JLabel("Office of Traffic & Safety", image, JLabel.LEFT); 
     label.setVerticalTextPosition(JLabel.BOTTOM); 
     label.setHorizontalTextPosition(JLabel.CENTER); 
     label.setFont(new Font("Times New Roman", Font.PLAIN, 11)); 
     pane.add(label); 
     label.setBounds(50, 10, 130, 100); 
     label.setBorder(null); 

     //university of maryland image and label\\\ 
     image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif")); 
     label = new JLabel("Univ. of Maryland", image, JLabel.LEFT); 
     label.setVerticalTextPosition(JLabel.BOTTOM); 
     label.setHorizontalTextPosition(JLabel.CENTER); 
     label.setFont(new Font("Times New Roman", Font.PLAIN, 11)); 
     pane.add(label); 
     label.setBounds(190, 10, 130, 90);  
     label.setBorder(null); 

     //critical lane label 
     label = new JLabel("Critical Lane Volume"); 
     label.setFont(new Font("Arial Narrow", Font.BOLD, 30)); 
     label.setForeground(colorGreen); 
     pane.add(label); 
     label.setBounds(50, 90, 250, 50); 
     label.setBorder(null); 

     label = new JLabel("<html>Please choose the analysis type:</html>"); 
     label.setFont(new Font("Arial", Font.BOLD, 18)); 
     label.setForeground(colorBrown); 
     pane.add(label); 
     label.setBounds(25, 130, 300, 70); 
     label.setBorder(null); 

     //back and exit buttons 
     JButton button1 = new JButton("DIAT"); 
     JButton button2 = new JButton ("Intersection"); 
     JButton button3 = new JButton ("Exit"); 
     pane.add(button1); 
     pane.add(button2); 
     pane.add(button3); 
     button1.setBounds(75, 200, 200, 50); 
     button2.setBounds(75, 270, 200, 50); 
     button3.setBounds(75, 340, 200, 50); 

     //add attap label at bottom left 
     image = new ImageIcon(MainForm.class.getResource("attap_logo.gif"));  
     label = new JLabel(image); 
     pane.add(label); 
     label.setBounds(30, 380, 270, 90);  
     label.setBorder(null); 

     button1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       frame.setVisible(false); 
       frame.dispose(); 
       InterchangeLoad.main(null); 
      } 
     }); 

     button2.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       frame.setVisible(false); 
       frame.dispose(); 
       MultipleIntersectionLoad.main(null); 
      } 
     }); 

     button3.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent arg0) { 
       frame.setVisible(false); 
       frame.dispose(); 
      } 
     }); 

    } 


    private static void createAndShowGUI() { 
     //Create and set up the window. 
     JFrame frame = new JFrame("Critical Lane Volume"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     //Set up the content pane. 
     addComponentsToPane(frame, frame.getContentPane()); 

     frame.setSize(350, 500); 
     frame.setResizable(false); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 

     //Schedule a job for the event-dispatching thread: 
     //creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       createAndShowGUI(); 
      } 
     }); 

    } 

} 
+1

是否所有的.gif圖像都可用? –

+0

您的文件「SHA_logo.gif」是否存在? – talnicolas

+2

你爲什麼要問同樣的事情兩次? http://stackoverflow.com/questions/6417805/errors-running-java-program – cirne100

回答

1

它沒有找到您的圖像。你確定你的圖片處於相同的相對位置嗎?它們與MainForm.class文件位於同一位置嗎?此外,我會對意見表示異議,即使用NetBeans創建GUI比使用Eclipse更容易。我已經使用了兩者,並且都可以使用,但我建議您避免使用任何代碼生成軟件,直到您瞭解Swing。例如,你會學會如何使用Swing/AWT佈局管理器,並避免使用null佈局/ setBounds。

+0

你是對的,該圖像沒有出現在左側面板項目下。這很奇怪,即時通訊使用netbeans打開我的所有圖像應該在的文件夾,但只有一些圖像顯示,而其他人不知道,我應該怎麼做的任何建議? – user638234

+0

@ user638234:沒有什麼奇怪的。 NetBeans在其他地方保存類文件,圖像需要使用類文件,而不是java文件。 –

1

程序找不到文件「SHA_logo.gif」,因此您的new ImageIcon(...)new ImageIcon(null)並且導致NullPointerException。

相關問題