2015-05-29 144 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