2015-05-29 59 views
-3

我想使用eclipse和java在標籤上顯示消息。我在創建JFrame時遇到的第一個問題。這是eclipse不會自動添加導入包。即。 import javax.swing.JLabel。第二個問題是,一旦我鍵入導入包,我仍然會收到錯誤消息,例如「無法解析導入javax.swing.Jlabel」。因此,在下面的代碼中,我必須手動輸入一些導入內容,並在從設置面板中拖動對象並將其放入設計視圖時自動放入一些導入內容。eclipse不自動添加軟件包

import java.awt.EventQueue; 
import javax.swing.*; 

import javax.swing.JFrame; 
import javax.swing.JButton; 
import javax.swing.Jlabel; 
import java.awt.Font; 
import java.awt.Color; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 


public class frame1 { 

    private JFrame frame; 

    /** 
    * Launch the application. 
    */ 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      public void run() { 
       try { 
        frame1 window = new frame1(); 
        window.frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 

    /** 
    * Create the application. 
    */ 
    public frame1() { 
     initialize(); 
    } 

    /** 
    * Initialize the contents of the frame. 
    */ 
    private void initialize() { 
     frame = new JFrame(); 
     frame.setBounds(100, 100, 889, 622); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().setLayout(null); 

     JButton btnNewButton = new JButton("showMessage"); 
     btnNewButton.addActionListener(new ActionListener() { 
      public void actionPerformed(ActionEvent arg0) { 

       //lbldisplayMessage.setText 
      } 
     }); 
     btnNewButton.setForeground(new Color(153, 51, 51)); 
     btnNewButton.setBackground(Color.CYAN); 
     btnNewButton.setFont(new Font("Script MT Bold", Font.BOLD, 16)); 
     btnNewButton.setBounds(360, 449, 145, 40); 
     frame.getContentPane().add(btnNewButton); 

     JLabel lblNewLabel = new JLabel("The Message Goes here"); 
     lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11)); 
     lblNewLabel.setBounds(28, 124, 160, 50); 
     frame.getContentPane().add(lblNewLabel); 

     JLabel lbldisplayMessage = new JLabel(""); 
     lbldisplayMessage.setBounds(162, 124, 200, 50); 
     frame.getContentPane().add(lbldisplayMessage); 
    } 
} 
+5

'javax.swing.JLabel中;'必須是'javax.swing.JLabel中;'。不要手動輸入輸入,使用熱鍵組織輸入(默認情況下,按Ctrl + Shift + o)。也讀[that](http://stackoverflow.com/questions/6293475/organize-imports-automatically) – alex2410

+0

http://www.shortcutworld.com/en/win/Eclipse.html –

+0

謝謝alex2410,我不知道那 – JayRod

回答

1

只是爲了您的信息, 日食不會自動添加軟件包

- 你必須手動導入它或使用熱鍵

對於我M端口,你可以使用

CTRL + SHIFT + O

+0

當您在輸入幾個初始字符後使用「ctrl + space」的幫助時,它會自動導入包 – bajrangi

-3

確保您的JRE庫添加到類路徑:

Project > Build Path > Configure build path... > Libraries tab > Add library > JRE System Library 
+0

問題出現在'JLabel'中的L而不是庫中。 – Blip

+0

我以爲他在每次導入時都會遇到他的錯誤:/ .... – flafoux

+1

他在他的問題中有* *「導入javax.swing.Jlabel無法解析。」* – Blip