简答题 Java
题号 #2246032

本题中,鼠标在窗口中单击一下,就在单击的位置生成一个小矩形,如果在小矩形上双击鼠标左键,则删除小矩形。
import java.awt.*;
import java.awt.event.*;
import javax swing.*;
class MousePanel extends JPanel extends MouseMo-
tionListener
{public MousePanel()
{addMouseListener(new MouseAdapter()
{public void mousePressed(MouseEvent evt)
{int X=evt.getX();
int Y=evt.getY();
current=find(x,y);
if(current<0)
add(x,y);
}
public void mouseClicked(MouseEvent evt)
{int X=evt.getX();
int Y=evt.getY();
if(evt.getClickCount()>=2)
{remove(current);
}
} 
});
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{super.paintComponent();
for(int i=0;i<nsquares;i++)
draw(g,i);
}
public int find(int X,int y)
(for(int i=0;i<nsquares;i++)
if(squares[i].x-SQUARELENGTH/2<=
x&&
X<=squares[i].x+SQuARELENGTH/2
&&squares[i].Y-SQUARELENGTH/2<
=Y
&&y<=squares[i].Y+SQUARELENGTH
/2)
return i ;
return-1 ;
}
public void draw(Graphics g,int i)
{g.drawRect(squares[i].X-SQUARE-
LENGTH/2。
squares[i].Y-SQUARELENGTH/2,
SQUARELENGTH,
SQUARELENGTH);
}
public void add(int X,int Y)
{if(nsquares<MAXNSQUARES)
{squares[nsquares]=new Point(x,y);
current=nsquares ;
nsquares++;
repaint();
}
}
public void remove(int n)
{if(n<0 ‖ n>=nsquares)return;
Nsquares- -;
squares[n]=squares[nsquares];
if(current= =n)current= -l;
repaint();
}
public void mouseMoved(MouseEvent evt)
{}
public void mouseDragged(MouseEvent evt)
{}
private static final int SQUARELENGTH=10:
private static final int MAXNSQUARES=100;
private Point[]squares=new Point[MAX-
NSQUARES];
private int nsquares=0;
private int current=-l;
}
class MouseFrame extends JFramc
{public MouseFrame()
{setTitle("java3");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(MousePanel());
} 
}
public class java3
{public static void main(String[]args)
{JFrame frame=new MouseFrame();
frame.show();
}
}

进入在线模考

参考答案

暂无

答案解析

第l处:extends JPanel implements MouseMotionListener.
第2处:super.paintComponent(g)
第3处:contentPane.add(new MousePanel())
【解析】第1处是继承Jpanel实现鼠标移动监听器接口;第2处以g为参数重新绘制组件;第3处在contentPane内容面板中添加一个MousePanel鼠标面板。

你可能感兴趣的试题

2 本题的功能是通过鼠标确定两个点,然后画两点间的直线。窗口中有一个按钮“Draw line”,单击该按钮后,它就处于按下状态,然后用鼠标在窗口中单击一下,在单击的地方就会出现一个坐标圆点,用鼠标在另外一个地方单击一下又会出现另外一个圆点,并且此时在两个坐标圆点间画出一条直线,且“Draw line”处于可用状态,再单击这个按钮就可以画另外一条直线。
Import java.awt.* ;
import java.awt.event.*;
import javax.swing.*;
class EventQueuePanel extends JPanel implements Ac-
tionListener
{EventQueuePanel()
{JButton button=new JButton("Draw line");
add(button);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
(Graphics g=getGraphics();
      P=getClick();
g.drawOval(P.x-2,P.Y-2,4,4);
Point q=getClick();
g.drawOval(q.x-2,q.y-2,4,4);
g.drawLine(P.X,P.Y,q.x,q.y);
g.dispose();
}
public Point getClick()
{EventQueue eq=Toolkit.getDefaultToolkit().
getSystemEventQueue();
while(true)
{try
{AWTEvent evt=eq.getNextEvent();
if(evt.getID()= =MouseEvent.MOUSE.
PRESSED)
{MouseEvent mevt=(MouseEvent)evt;
Point P=      ();
Point top=getRootPane().getLocation();
P.X-=top.x;
P.Y-=top.Y;
return P;
}
}
catch(InterruptedException e)
{}
}
}
private int Y=60;
}
class EventQueueFrame extends JFrame
{public EventQueueFrame()
{setTitle("java2");
setSize(300,200);
addWindowListener(new WindowAdapter()
{public void windowClosing(WindowEvent e)
{System.exit(0);
}
});
Container contentPane=getContentPane();
contentPane.add(new EventQueuePanel());
}
}
public class java2
{public static void main(String[]args)
{Frame frame=new EventQueueFrame();
frame.show();
}
}
3 本题是一个Applet,功能是用鼠标画不同颜色的图形。页面中有5个按钮“画红色图形”、“画绿色图形”、“画蓝色图形”、“橡皮”和“清除”,单击前三个按钮中的一个,按住鼠标左键或右键在面板中拖动,就能两出对应颜色的线条,单击“橡皮”按钮,按住鼠标左键或右键在面板中拖动就能将面板中的图形擦除掉,单击“清除”按钮,就能将面板中所有的图形清除掉。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class java3 extends Applet implements ActionListener
{int x=-1,y=-1,rubberNote=0,clearNote=0;
Color C=new Color(255,0,O);
int con=3;
Button b_red,b_blue,b_green,b_clear,b_quit;
public void init()
{
addMouseMotionListener(this);
b_red=new Button("画红色图形");
b_blue=new Button("画蓝色图形");
b_green=new Button("画绿色图形");
b_quit=new Button("橡皮");
b_clear=new Button("清除");
add(b_red);
add(b_green);
add(b_blue);
add(b_quit); 
add(b_clear);
b_red.addActionListener(this);
b_green.addActionListener(this);
b_blue.addActionListener(this);
b_quit.addActionListener(this);
b_dear.addActionListener(this);
} 
public void paint()
(if(x!=-l&&y!=-l&rubberNote= =
0R&clearNote= =0)
{g.setColor(c);
g.filloval(X,Y,con,con);
}
else if(rubberNote= =1&&clearNote= =O)
{g.clearRect(x,Y,10,10);
} 
else if(clearNote= =1&&rubberNote= =O)
{g.clearRect(0,0,getSize().width,getSize().
height);
}
}
public void mouseDragged(MouseEvent e)
{x=(int)e.getX();y=(int)e.getY();repaint();
}
public void mouseMoved(MouseEvent e){)
public void update(Graphics g)
{paint(g);
}
public void actionPerformed(Event e)
{if(e.getSource()= =b-red)
{rubberNote=0;clearNote=0;c=new Color
(255,0,0);
}
else if(e.getSource()= =b_green)
{rubberNore=0;clearNote=0;C=new Color(0,
255,0);
}
else if(e.getSource()= =b_blue)
{rubberNote=0;clearNote=0;C=new Color(0,
0,255);
}
if(e.getSource()= =b—quit)
{rubberNote=1;clearNote=0;
}
if(e.getSource()= =b—clear)
{clearNote=1;rubberNote=0;repaint();
}
}
}
找试题 · 找答案 · 找资料 在线模考