对于我的游戏,我需要从我的面板中删除对象。我想将Square Objects存储在ArrayList中,如果用迭代器点击鼠标,则删除它们 但这三个方块并没有涂在面板上。谁有人知道为什么他们不被画?
最诚挚的问候。
public class Test {
JFrame f = new JFrame();
JPanel panel =new JPanel();
ArrayList<Square> list = new ArrayList<Square>();
public Test() {
f.setSize(300,300);
f.addMouseListener(new Mouseklick());
panel.setBackground(Color.YELLOW);
f.add(p);
// adding square to list and panel
Square a = new Square(20, 20); list.add(a); panel.add(a) ;
Square b = new Square(40, 40); list.add(a); panel.add(b);
Square c = new Square(60, 60); list.add(a); panel.add(c);
panel.repaint();
f.setVisible(true);
}
class Square extends JLabel {
public Square(int x , int y) { // Coordinates
setBounds(x, y, 20,20);
}
@Override
public void paint(Graphics g) {
g.setColor(Color.RED);
g.fillRect(0, 0, 20, 20);
}
}
class Mouseklick extends MouseAdapter {
public void mousePressed(MouseEvent e) {
Iterator<Square> iter = list.iterator();
while(iter.hasNext()){
Square q = iter.next();
if( q.getX()==20){ // remove square with x Coord=20
iter.remove();
panel.repaint();
}
}
}
}
public static void main(String[] args) {
new Test();
}
}
答案 0 :(得分:1)
您的JPanel
由layout manager控制,该{{3}}正在决定您的组件的放置和尺寸,因为您没有提供适当的尺寸提示,标签的尺寸已根据他们的尺寸而定默认大小为0x0
此外,您只是将a
添加到面板...
Square a = new Square(20, 20);
list.add(a);
panel.add(a) ;
Square b = new Square(40, 40);
list.add(a);
panel.add(b);
Square c = new Square(60, 60);
list.add(a);
panel.add(c);
由于组件只能驻留在单个容器上,因此实际上只有一个组件添加到容器中。
由于布局管理有些复杂,因此更好的解决方案是遵循完全自定义绘制的路线。
也就是说,利用可用的几何类并将它们直接绘制到JPanel
本身。
以下示例使用了Rectangle
类,这个类的两个不错的功能是......
这几乎就是你真正想做的事情
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private List<Rectangle> list = new ArrayList<Rectangle>();
public TestPane() {
list.add(new Rectangle(20, 20, 20, 20));
list.add(new Rectangle(40, 40, 20, 20));
list.add(new Rectangle(60, 60, 20, 20));
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Iterator<Rectangle> iter = list.iterator();
while (iter.hasNext()) {
Rectangle q = iter.next();
if (q.contains(e.getPoint())) {
iter.remove();
}
}
repaint();
}
});
}
@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
for (Rectangle rect : list) {
g2d.fill(rect);
}
g2d.dispose();
}
}
}