Getting this error with the coordinates of the cursor using MouseClicked."The method getX() & getY() is undefined for the type MouseEvent"

huangapple go评论71阅读模式
英文:

Getting this error with the coordinates of the cursor using MouseClicked."The method getX() & getY() is undefined for the type MouseEvent"

问题

以下是您要翻译的内容:

Simple Addition subtraction application:

I checked for its solution and I found solutions regarding getX() but there were not related to MouseClicked!

I think its some type casting issue but I'm unable to resolve it and I verified all the imports and also even matched the code with the available online ones!

imports:

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
import com.sun.glass.events.MouseEvent;

Main Class:

public class Calcx {
public static void main(String[] args) {
Initialize o=new Initialize();
}
}

called class:

class Initialize extends JFrame implements ActionListener {
JFrame frame;
JTextField t1;
JButton b1,b2;
JTextField t2;
JLabel l,l1;
JPopupMenu p;
JMenuItem i,j;

Constructor:

public Initialize() {
setTitle("CalcX");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l=new JLabel("CalcX");
t1=new JTextField(5);
t2=new JTextField(5);
b1=new JButton("ADD");
b2=new JButton("SUBTRACT");
b2.addActionListener(this);
l1=new JLabel();
b1.addActionListener(this);
p=new JPopupMenu("Edit");
i= new JMenuItem("cut");
j= new JMenuItem("copy");
add(p);
p.add(i);
p.add(j);
add(l);
add(t1);
add(t2);
add(b1);
add(b2);
pack();
setVisible(true);

Error while using mouseClicked:

addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent a)
{
int x=a.getX(); Error
int y=a.getY(); Error
p.show(frame,x,y);
}
});

英文:

Simple Addition subtraction application:

I checked for its solution and I found solutions regarding getX() but there were not related to MouseClicked!

I think its some type casting issue but I'm unable to resolve it and I verified all the imports and also even matched the code with the available online ones!

imports:

    import java.awt.FlowLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JMenuItem;
    import javax.swing.JPopupMenu;
    import javax.swing.JTextField;
    import com.sun.glass.events.MouseEvent;

Main Class:

    public class Calcx {
    	public static void main(String[] args) {
    		Initialize o=new Initialize();
    		
    	}
    }

called class:

    class Initialize extends JFrame implements ActionListener {
    		JFrame frame;
    		JTextField t1;
    		JButton b1,b2;
    		JTextField t2;
    		JLabel l,l1;
    		JPopupMenu p;
    		JMenuItem i,j;

Constructor:

    			public Initialize() {
    				setTitle("CalcX");
    				setLayout(new FlowLayout());
    				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    				l=new JLabel("CalcX");
    				t1=new JTextField(5);
    				t2=new JTextField(5);
    				b1=new JButton("ADD");
    				b2=new JButton("SUBTRACT");
    				b2.addActionListener(this);
    				l1=new JLabel();
    				b1.addActionListener(this);
    				p=new JPopupMenu("Edit");
    				i= new JMenuItem("cut");
    				j= new JMenuItem("copy");
    				add(p);
    				p.add(i);
    				p.add(j);
                    add(l);
    				add(t1);
    				add(t2);
    				add(b1);
    				add(b2);
    				pack();
    				setVisible(true);


Error while using mouseClicked:

    				addMouseListener(new MouseAdapter()
    				{
    				public void mouseClicked(MouseEvent a)
    				{
    					 int x=a.getX();	*Error*
    					int y=a.getY();		*Error*
   					p.show(frame,x,y);
    				}
    				});	
    			}
    	
    }

答案1

得分: 2

import com.sun.glass.events.MouseEvent;

这不是与JDK一起使用的标准MouseEvent类。

您需要使用:

import java.awt.event.MouseEvent;

英文:
import com.sun.glass.events.MouseEvent;

That is not the standard MouseEvent class used with the JDK.

You need to use:

import java.awt.event.MouseEvent;

huangapple
  • 本文由 发表于 2020年5月30日 04:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/62094333.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定