我想使用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);
}
}
'javax.swing.JLabel中;'必須是'javax.swing.JLabel中;'。不要手動輸入輸入,使用熱鍵組織輸入(默認情況下,按Ctrl + Shift + o)。也讀[that](http://stackoverflow.com/questions/6293475/organize-imports-automatically) – alex2410
http://www.shortcutworld.com/en/win/Eclipse.html –
謝謝alex2410,我不知道那 – JayRod