【发布时间】:2015-05-29 12:58:08
【问题描述】:
我正在尝试使用 eclipse 和 java 在标签上显示一条消息。创建JFrame 时遇到的第一个问题。就是eclipse不会自动添加导入包。 IE。 import javax.swing.JLabel。第二个问题是,一旦我键入导入包,我仍然会收到错误消息,例如“无法解析导入 javax.swing.Jlabel”。所以在下面的代码中,当我从调色板中拖动对象并将其放到设计视图中时,我必须手动放入一些导入,而有些则自动放入其中。
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;。不要手动编写导入,使用热键来组织导入(Windows 默认为 ctrl+shift+o)。另请阅读that -
谢谢alex2410,我不知道