请回答的详细点,初学~还有eclipse里statusBar也显示错误,但是程序是对的,书上的,哪位大虾知道eclipse的请说一下,谢啦~代码是://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() + ")"); } }
在statusBar处显示statusBar cannot be resolved
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.Timer; import javax.swing.event.MouseInputAdapter;public class DrawingPanel extends MouseInputAdapter implements ActionListener { private Frame frame; // overall window frame private Panel panel; // drawing surface private Label 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"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.setLayout(new BorderLayout()); frame.add(panel, BorderLayout.NORTH); 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(); } private void setDefaultCloseOperation(int exitOnClose) { // TODO Auto-generated method stub
} // 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() + ")"); } }稍微给你改了下,你有些变量名拼写错误。那个setIcon那个不知道怎么改。只能帮到这里了。加油
貌似还是不行 这是Import后面的javax文件中提示的错误: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 这是怎么回事呢?
//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() + ")");
}
}
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.Timer;
import javax.swing.event.MouseInputAdapter;public class DrawingPanel extends MouseInputAdapter implements ActionListener {
private Frame frame; // overall window frame
private Panel panel; // drawing surface
private Label 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");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setLayout(new BorderLayout());
frame.add(panel, BorderLayout.NORTH);
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();
} private void setDefaultCloseOperation(int exitOnClose) {
// TODO Auto-generated method stub
} // 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() + ")");
}
}稍微给你改了下,你有些变量名拼写错误。那个setIcon那个不知道怎么改。只能帮到这里了。加油
这是Import后面的javax文件中提示的错误: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
这是怎么回事呢?