程序應該沒什麼問題,可就是編譯的時候就出現問題了,問題:內些錯誤到底怎麼看呢?其實我還有很多看不懂...但還是請前輩們先幫我解決這兩個問題吧...謝謝大家了啊!
package mouse;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class MouseFrameText {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
EventQueue.invokeLater(new Runnable()
{
public void run()
{
JFrame frame=new MouseFrame();
frame.setTitle("MouseFrame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class MouseFrame extends JFrame
{
public MouseFrame()
{
add(new MouseComponent());
pack();
}
}
class MouseComponent extends JComponent
{
private static final int SIDELENGTH=10;
private ArrayListsquares;
private Rectangle2D current;
public MouseComponent()
{
squares=new ArrayList<>();
current=null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
Graphics2D g2=(Graphics2D) g;
for(Rectangle2D r:squares)
g2.draw(r);
}
public Rectangle2D find(Point2D p)
{
for(Rectangle2D r:squares)
{
if(r.contains(p))
return r;
}
return null;
}
public void add(Point2D p)
{
double x=p.getX();
double y=p.getY();
current=new Rectangle2D.Double(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
squares.add(current);
repaint();
}
public void remove(Rectangle2D s)
{
if(s==null) return;
if(s==current) current=null;
squares.remove(s);
repaint();
}
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
current=find(event.getPoint());
if(current==null) add(event.getPoint());
}
public void mouseClicked(MouseEvent event)
{
current=find(event.getPoint());
if(current!=null&&event.getClickCount()>=2)
remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
if(find(event.getPoint())==null)
setCursor(Cursor.getDefaultCursor());
else
setCursor(Cursor.getPredefinedCursor(Cursor.CROSSHAIR_CURSOR));
}
public void mouseDragged(MouseEvent event)
{
if(current!=null)
{
int x=event.getX();
int y=event.getY();
current.setFrame(x-SIDELENGTH/2,y-SIDELENGTH/2,SIDELENGTH,SIDELENGTH);
repaint();
}
}
}
}
你看,那個紅色的字體就是控制台的輸出,你完成的應該是關於圖形界面的功能,你可以在這個類裡某行開始執行的時候,雙擊左側邊,會出來一個點,這就是
斷點,可以幫助你查看錯誤,你看它說17行,意思就是程序執行到17行就不行了,這裡有錯誤,錯誤是Bad type oper....就是前幾行寫的那個,
你再百度這個錯誤,查看相關內容,最後我想說,你這個代碼貼的沒有高亮,我看著別扭,可能是換了markdown,所以看著不舒服,希望對你有用!!!