我想使用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