这个程序是关于创建组件并在JApplet中随机移动它们,并在需要时删除组件。每次鼠标点击都会创建一个新组件。创建组件后,我们可以单击它并将其拖动到我们想要的地方。双击该组件将其删除。我成功地创建,移动和删除组件。
例如,我通过在JApplet的不同位置随机单击三次,在JApplet上创建了三个组件。请点击这里查看创建的组件,我在屏幕上随机移动它们。我的目标是通过单击write按钮将所有组件在屏幕上的位置的最终坐标(x,y)写入到文本文件中。我不知道mouserreleased方法。请帮我一下。谢谢你!非常感谢你的帮助。
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.awt.geom.*;
import javax.swing.*;
这是我的主类
public class MouseTest extends JApplet
{
public void init()
{
EventQueue.invokeLater(new Runnable() {
public void run()
{
MousePanel panel = new MousePanel();
add(panel);
}
});
}
public static void main(String[] args)
{
EventQueue.invokeLater(new mainThread());
}
}
class mainThread implements Runnable
{
public void run()
{
JPanel panel = new MousePanel();
JFrame frame = new JFrame("MouseTest");
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,200);
frame.setVisible(true);
}
}
class MousePanel extends JPanel
{
public MousePanel()
{
squares = new ArrayList<Rectangle2D>();
current = null;
addMouseListener(new MouseHandler());
addMouseMotionListener(new MouseMotionHandler());
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// draw all squares
for (Rectangle2D r : squares)
g2.draw(r);
}
/**
* Finds the first square containing a point.
* @param p a point
* @return the first square that contains p
*/
public Rectangle2D find(Point2D p)
{
for (Rectangle2D r : squares)
{
if (r.contains(p)) return r;
}
return null;
}
/**
* Adds a square to the collection.
* @param p the center of the square
*/
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();
}
/**
* Removes a square from the collection.
* @param s the square to remove
*/
public void remove(Rectangle2D s)
{
if (s == null) return;
if (s == current) current = null;
squares.remove(s);
repaint();
}
private static final int SIDELENGTH = 10;
private ArrayList<Rectangle2D> squares;
private Rectangle2D current;
// the square containing the mouse cursor
这些方法用于定义、拖动和删除对象
private class MouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent event)
{
// add a new square if the cursor isn't inside a square
current = find(event.getPoint());
if (current == null) add(event.getPoint());
}
public void mouseClicked(MouseEvent event)
{
// remove the current square if double clicked
current = find(event.getPoint());
if (current != null && event.getClickCount() >= 2) remove(current);
}
}
private class MouseMotionHandler implements MouseMotionListener
{
public void mouseMoved(MouseEvent event)
{
// set the mouse cursor to cross hairs if it is inside
// a rectangle
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();
// drag the current rectangle to center it at (x, y)
current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2, SIDELENGTH, SIDELENGTH);
repaint();
}
}
}
}
您为什么不断更改需求????参见原问题:如何找到最终坐标(x &y)对象在JApplet中的位置,并将它们写入文本文件
首先,您想在拖动完成后写出移动组件的位置。
然后您想要在拖动完成后写出"所有"组件的位置。
现在您想要在单击"按钮"后写出所有组件的位置。
我不知道mouserreleased方法。
这有什么关系吗?这是你在上一个问题中对你的要求的建议。由于您的需求已更改,因此不再需要它。
我的目标是通过单击write按钮将所有组件在屏幕上的位置的最终坐标(x,y)写入到文本文件中。
然后你需要给按钮添加一个ActionListener。你已经给了一个链接到How to Write an ActionListener
的Swing教程,所以我想问题是什么代码应该包含在ActionListener?
你在上一个问题中说:
- 我知道如何获得组件的位置
- 我知道如何将数据写入文件
所以唯一剩下的问题是如何得到面板上的所有组件?为此,您可以在包含所有拖动组件的面板上使用Container.getComponents()
方法。ActionListener
的基本代码是这样的:
for (Component component: panel.getComponents())
{
Point location = component.getLocation();
// write the location to the file
}
我将允许您添加打开/关闭文件的代码