我有一个固定的学校作业。
我的目标是实现MVC,但现在我只是在视图中进行所有操作(暂时简化一下)。
所以-我有一个框架,它有一个面板。
面板上有一个形状列表。每当用户按下addLine / addRect按钮时,就会引发一个事件并将行/矩形添加到此列表中。
paintComponent函数绘制列表中的所有形状(所有形状都知道如何绘制自己)。
到目前为止,一切都很好-它有效!
此分配的唯一其他要求是,只要用户单击绘图区域中的一个点,就会删除所有包含该点的形状。
每个形状都有其自己的Contains(p)函数。
因此,我决定将MouseListener添加到面板中,该面板将获取点击的X,Y坐标,并将从形状列表中删除相关的形状。
我不知道这是否是个好主意,但目前这不是我的问题。
我的问题是MouseListener无法响应单击-我知道这是因为在mouseClicked实现的内部有一个断点,而调试器从未到达该断点。
我的问题是为什么?
这是我的代码:还有更多我需要解决的问题,但现在他们不关心我
//MyFrame.java
public class MyFrame extends JFrame {
private MyJPanel panel ;
public MyFrame() throws MyShape.IllegalArgumentException {
initUI();
}
private void initUI() throws IllegalArgumentException {
setLayout(new FlowLayout());
panel = new MyJPanel();
add(panel);
setTitle("Shapes Editor");
setSize(600, 600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
setVisible(true);
}
}
//MyPanel.java
public class MyJPanel extends JPanel {
ArrayList<MyShapeAbstract> pic;
DrawingArea drawingArea;
ButtonsPanel buttonsPanel;
public ButtonsPanel getButtonsPanel() {
return buttonsPanel;
}
public void setButtonsPanel(ButtonsPanel buttonsPanel) {
this.buttonsPanel = buttonsPanel;
}
public ArrayList<MyShapeAbstract> getPic() {
return pic;
}
public void setPic(ArrayList<MyShapeAbstract> pic) {
this.pic = pic;
}
public MyJPanel() throws MyShape.IllegalArgumentException {
initUI();
}
private void initUI() throws MyShape.IllegalArgumentException {
setLayout(new FlowLayout());
pic = new ArrayList<MyShapeAbstract>();
add(buttonsPanel = new ButtonsPanel(this));
add(drawingArea = new DrawingArea(this));
drawingArea.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
Point p = null;
try {
p = new Point(e.getX(), e.getY());
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
}
Iterator<MyShapeAbstract> iter = pic.iterator();
while (iter.hasNext()){
MyShapeAbstract shape = iter.next();
if(shape.contains(p))
{
iter.remove();
}
}
drawingArea.repaint();
pic.clear();
repaint();
}
});
JButton addLineButton = buttonsPanel.getAddLineButton();
addLineButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
addLine();
drawingArea.repaint();
repaint();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
}
}
});
add(addLineButton);
JButton addRectButton = buttonsPanel.getAddRectButton();
addRectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
addRect();
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
}
drawingArea.repaint();
repaint();
}
});
add(addRectButton);
}
@Override
public void repaint() {
Graphics g = getGraphics();
if(g!=null && pic != null)
for(MyShapeAbstract shape : pic){
shape.draw(g);
}
}
private void addRect() throws IllegalArgumentException {
Random r = new Random();
pic.add(new MyRectangle(new Point(r.nextInt(200), r.nextInt(200)), new Point(r.nextInt(200),r.nextInt(200))));
}
private void addLine() throws IllegalArgumentException {
Random r = new Random();
pic.add(new MyLine(new Point(r.nextInt(200), r.nextInt(200)), new Point(r.nextInt(200),r.nextInt(200))));
}
}
class DrawingArea extends JPanel{
public DrawingArea(MyJPanel parent) {
this.parent = parent;
setLayout(new FlowLayout());
}
@Override
public void repaint() {
Graphics g = getGraphics();
if(g!=null && parent.getPic() != null)
for(MyShapeAbstract shape : parent.getPic()){
shape.draw(g);
}
}
MyJPanel parent;
}
class ButtonsPanel extends JPanel{
//ArrayList<JButton> buttons = new ArrayList<JButton>();
MyJPanel parent;
private JButton addLineButton = new JButton("addLineButton");
private JButton addRectButton = new JButton("addRectButton");
ButtonsPanel(final MyJPanel parent){
this.parent = parent;
setLayout(new FlowLayout());
add(addLineButton);
add(addRectButton);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
Point p = null;
try {
p = new Point(e.getX(), e.getY());
} catch (IllegalArgumentException e1) {
e1.printStackTrace();
}
Iterator<MyShapeAbstract> iter = parent.getPic().iterator();
while (iter.hasNext()){
MyShapeAbstract shape = iter.next();
if(shape.contains(p))
{
iter.remove();
}
}
repaint();
}
});
}
@Override
public void repaint() {
Graphics g = getGraphics();
if(g!=null && parent.getPic() != null)
for(MyShapeAbstract shape : parent.getPic()){
shape.draw(g);
}
}
public JButton getAddLineButton() {
return addLineButton;
}
public void setAddLineButton(JButton addLineButton) {
this.addLineButton = addLineButton;
}
public JButton getAddRectButton() {
return addRectButton;
}
public void setAddRectButton(JButton addRectButton) {
this.addRectButton = addRectButton;
}
}
最佳答案
我在您的“按钮面板”中添加了以下内容:
ButtonsPanel(final MyJPanel parent){
this.parent = parent;
setLayout(new FlowLayout());
add(addLineButton);
add(addRectButton);
//show me the Panel size :)
this.setBackground(Color.RED);
红色小矩形是鼠标侦听器工作的区域。所以也许您的意思是
parent.addMouseListener
或drawingArea..addMouseListener
(但是您的drawingArea也很小)?只需将技巧与背景结合使用,然后检查您的区域并调整大小或您真正想拥有监听器的面板即可。也许还添加MyShapeAbstract,MyLine,MyRectangle,以便可以测试完整的代码。