我有一个我无法解决的问题。 按钮 B1 和 B2 完美运行,但是当我尝试在 B3 上实现操作侦听器时(与我添加到按钮 1 和 2 的方式完全相同),它不起作用。我被难住了。我从字面上复制并粘贴了我为 b1 实现 b2 和 b3 的方式,但它不起作用!!这很奇怪,因为它只有在我单击 B3 然后右键单击它时才有效,但我应该单击 B3(命名边缘)并在任何地方左键单击以使消息出现。
B1 将出现一个矩形,B2 将出现椭圆,右键单击形状将删除它。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Tutorial extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
public static void storeShape(Shape shape){
shapes.add(shape);
}
public static void removeShape(int index){
shapes.remove(index);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
if(shape != null)
for ( Shape shape1 : shapes){
g2.draw(shape1);
}
}
private static ArrayList<Shape> shapes = new ArrayList<Shape>();
private Object lastbuttonpressed;
static Shape shape;
public static void main (String[] args){
Tutorial t = new Tutorial();
JFrame f = new JFrame();
f.setTitle("Tutorial");
f.setSize(600, 400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(t);
JPanel p = new JPanel(new GridBagLayout());
JButton b1 = new JButton("Rectangle");
JButton b2 = new JButton("Ellipse");
JButton b3 = new JButton("Edge");
JButton b4 = new JButton("Label");
GridBagConstraints gbc2 = new GridBagConstraints();
gbc2.insets = new Insets(5,5,5,5);
gbc2.gridx = 0;
gbc2.gridy = 0;
p.add(b1,gbc2);
gbc2.gridx = 1;
gbc2.gridy = 0;
p.add(b2,gbc2) ;
gbc2.gridx = 2;
gbc2.gridy = 0;
p.add(b3,gbc2);
gbc2.gridx = 0;
gbc2.gridy = 1;
p.add(b4,gbc2);
f.add(p, BorderLayout.NORTH);
JTextField textField = new JTextField(" text field");
gbc2.gridx = 1;
gbc2.gridy = 1;
p.add(textField, gbc2);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.lastbuttonpressed = e.getSource();
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.lastbuttonpressed = e.getSource();
}
});
b3.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
t.lastbuttonpressed = e.getSource();
}
});
f.addMouseListener(new MouseListener() {
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
if (SwingUtilities.isLeftMouseButton(e)){
if (t.lastbuttonpressed == b1){
shape = new Rectangle(e.getX()-50,e.getY()-120,100,50);
storeShape(shape);
t.repaint();
}
else if ( t.lastbuttonpressed == b2){
shape = new Ellipse2D.Double(e.getX()-50,e.getY()-120,100,50);
storeShape(shape);
t.repaint();
}
}
else if (t.lastbuttonpressed == b3){
JOptionPane.showMessageDialog(null, "button 3");
t.repaint();
}
else if (SwingUtilities.isRightMouseButton(e)){
for (int i=0; i<shapes.size(); i++){
if (shapes.get(i).contains(e.getX()-50,e.getY()-100)){
shapes.remove(i);
t.repaint();
}
}
}
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent e) {
}
});
f.setVisible(true);
}
}
您的格式有点偏离,很难阅读。看起来您正在检查正在按下的 b3:
否则如果(t.lastbutton按下== b3){
在此之外,如果检查:
if (SwingUtilities.isLeftMouseButton(e)){