在eclipse环境中,编了个java程序,其中有错误,是设置问题本人菜鸟,高手请进~1、Label处:Lable cannot be resolved to a type2、setIcon处:The method setIcon(ImageIcon) is undefined for the type Label3、statusBar处:statusBar cannot be resolved4、Frame.EXIT_ON_CLOSE处:Frame.EXIT_ON_CLOSE cannot be resolved[/b]5、The method BorderLayout() is undefined for the type DrawingPanel这是程序://A simple interface for drawing persistent images.
//Initial version with events.import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;public class DrawingPanel extends MouseInputAdapter 
         implements ActionListener {
private Frame frame;         //overall window frame
private Panel panel;         //drawing surface
private Lable statusbar;     //status bar 
private Graphics g;           //drawing pen

//constructs a drawing panel of given size
public DrawingPanel(int width,int height) {
//set up the empty image onto which we will draw
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);
g = image.getGraphics();
g.setColor(Color.BLACK);

//enclose the image in a label inside a panel
Label label = new Label();
label.setIcon(new ImageIcon(image));
panel = new Panel(new FlowLayout());
panel.setBackground(Color.WHITE);
panel.setPreferredSize(new Dimension(width,height));
panel.add(label);

//the status bar that shows the mouse position
statusBar = new Label(" ");

//attach listener to observe mouse movement
panel.addMouseListener(this);
panel.addMouseMotionListener(this);

//set up the JFrame
frame = new Frame("Drawing Panel");
frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(panel,BorderLayout());
frame.add(statusBar,BorderLayout.SOUTH);
frame.pack();
frame.setVisible(true);

//start a repaint timer to refresh the screen
Timer timer = new Timer(250,this);
timer.start();
}

//obtains the Graphics object to draw on the panel
public Graphics getGraphics() {
return g;
}

//sets the background color of the drawing panel
    public void setBackground(Color c) {
     panel.setBackground(c);
    }
    
    //shows or hides the drawing panel on the screen
    public void setVisible(boolean visible) {
     frame.setVisible(visible);
    }
    
    //used for timer that repeatedly repaint screen
    public void actionPerformed(ActionEvent e) {
     panel.repaint();
    }
    
    //draws status bar text when mouse moves
    public void mouseMoved(MouseEvent e) {
     statusBar.setText("(" + e.getX() + "," + e.getY() + ")");
    }
}

解决方案 »

  1.   

    这是awt和java转换问题 不是eclipse问题
      

  2.   


    楼主 这程序哪里来的啊。
    Lable  应该不是 java.awt.*;中的 java.awt.*;中的
    Lable  没有setIcon 方法 。
    statusBar是什么啊。。 写错了吧 应该的statusbar 吧
    Frame 应该也不是java.awt.*; 中 的吧  java.awt.*; 中的Frame  没有EXIT_ON_CLOSE 属性。。
      

  3.   

    帮你改好了,好几个方法是swing组件里,却用awt里的组件去调用,当然出错import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Label;
    import java.awt.Panel;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.image.BufferedImage;import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.Timer;
    import javax.swing.event.MouseInputAdapter;public class DrawingPanel extends MouseInputAdapter implements ActionListener {

    private JFrame frame; // overall window frame
    private Panel panel; // drawing surface
    private JLabel statusbar; // status bar
    private Graphics g; // drawing pen // constructs a drawing panel of given size
    public DrawingPanel(int width, int height) {
    // set up the empty image onto which we will draw
    BufferedImage image = new BufferedImage(width, height,
    BufferedImage.TYPE_INT_ARGB);
    g = image.getGraphics();
    g.setColor(Color.BLACK); // enclose the image in a label inside a panel
    JLabel label = new JLabel();
    label.setIcon(new ImageIcon(image));
    panel = new Panel(new FlowLayout());
    panel.setBackground(Color.WHITE);
    panel.setPreferredSize(new Dimension(width, height));
    panel.add(label); // the status bar that shows the mouse position
    statusbar = new JLabel(" "); // attach listener to observe mouse movement
    panel.addMouseListener(this);
    panel.addMouseMotionListener(this); // set up the JFrame
    frame = new JFrame("Drawing Panel");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setLayout(new BorderLayout());
    frame.add(panel, BorderLayout.CENTER);
    frame.add(statusbar, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true); // start a repaint timer to refresh the screen
    Timer timer = new Timer(250, this);
    timer.start();
    } // obtains the Graphics object to draw on the panel
    public Graphics getGraphics() {
    return g;
    } // sets the background color of the drawing panel
    public void setBackground(Color c) {
    panel.setBackground(c);
    } // shows or hides the drawing panel on the screen
    public void setVisible(boolean visible) {
    frame.setVisible(visible);
    } // used for timer that repeatedly repaint screen
    public void actionPerformed(ActionEvent e) {
    panel.repaint();
    } // draws status bar text when mouse moves
    public void mouseMoved(MouseEvent e) {
    statusbar.setText("(" + e.getX() + "," + e.getY() + ")");
    }
    }
      

  4.   

    ls的不错
    你这是程序问题  不是eclipse的问题
      

  5.   

    先谢谢5楼~可是用了你那个程序出现了错误!
    javax.swing.ImageIcon;错误:Access restriction: The type ImageIcon is not accessible due to restriction on required library C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jarjavax.swing.JFrame;错误:Access restriction: The type JFrame is not accessible due to restriction on required library C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jarjavax.swing.JLabel;错误:Access restriction: The type JLabel is not accessible due to restriction on required library C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jarjavax.swing.Timer;错误:Access restriction: The type Timer is not accessible due to restriction on required library C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jarjavax.swing.event.MouseInputAdapter;错误:Access restriction: The type MouseInputAdapter is not accessible due to restriction on required library C:\Program Files\Java\jdk1.7.0\jre\lib\rt.jar都是一类问题~后面还有问题,这是怎么回事呢?
      

  6.   

    (⊙o⊙)嗯,O(∩_∩)O~,谢啦,Jlabel哪写错了?
      

  7.   

    菜鸟来说两句:
    private Lable statusbar; //status bar   这条语句把Label写成 Lable的低级错误!
    label.setIcon(new ImageIcon(image));  //label这个对象是无法调用setIcon();方法的
    frame.setDefaultCloseOperation(Frame.EXIT_ON_CLOSE);//这是javax.swing.JFrame;的方法吧
    frame.add(panel,BorderLayout());//创建BorderLayout类的对象怎么会缺少new 这个关键字呢?代码写的太少了,连这个也不能看出么 ?
    总结:这不是eclipse的问题 ,楼主可以多熟悉一下API帮助文档。调整jdk版本可以在  窗口》首选项》java》编译器。