import java.awt.Color;
河池网站建设公司创新互联,河池网站设计制作,有大型网站制作公司丰富经验。已为河池上千家提供企业网站建设服务。企业网站搭建\外贸网站建设要多少钱,请找那个售后服务好的河池做网站的公司定做!
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Demo1 extends JFrame implements
MouseMotionListener,KeyListener,MouseListener {
private JPanel content;
private JPanel bottom;
private JLabel show;
static String keyCode="";
public Demo1(){
content=new JPanel();
content.setLayout(null);
show=new JLabel();
show.setForeground(Color.white);
bottom=new JPanel();
content.setBounds(0, 0, 800, 600);
content.setBackground(Color.black);
content.add(show);
content.setFocusable(true);
content.addKeyListener(this);
content.addMouseMotionListener(this);
content.addMouseListener(this);
setTitle
("读取键盘输入的字符并将其显示到鼠标所指的位置");
setSize(800, 600);
setLocationRelativeTo(getOwner());
add(content);
add(bottom);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
keyCode+=String.valueOf((char)e.getKeyCode());
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
new Demo1();
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if(keyCode!=""){
show.setText(keyCode);
show.setBounds(e.getX()+20, e.getY(), 800, 30);
}
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
1、首先打开IDEA软件,进入Java代码编辑主界面。
2、其次点击编辑窗口上方的“文件”菜单项。
3、最后在下拉子菜单中,点击“设置”选项,选择需要的设置即可。
this.addMouseListener(new
MouseAdapter()
{
@Override
public
void
mousePressed(MouseEvent
e)
{
x
=
e.getX();
y
=
e.getY();
System.out.println("Xbefore:"+e.getX());
}
});
this.addMouseMotionListener(new
MouseMotionListener()
{
public
void
mouseDragged(MouseEvent
e)
{
JButton
btn
=
(JButton)
e.getSource();
setLocation(btn.getX()
+
e.getX()
-
x,
btn.getY()
+
e.getY()
-
y);
}
public
void
mouseMoved(MouseEvent
e)
{
}
});
上面的代码是给JButton添加的鼠标事件,移动的是按钮。。。
你这样不行啊,一闪一闪的根本看不到显示效果或者更本没有显示,绘图的方法要写在paintComponent(Graphics g)里面才能长久保存。
我知道你想在mouseClicked里面做什么,但是在这里进行绘图,repaint没有立即被执行,所以有可能没有任何显示;如果repaint执行了,也很有可能立刻被后面的绘图所覆盖。最好的办法是在这里出发一个标志,然后调用repaint函数,在paintComponent函数中对这个标志进行绘制,决定到底该怎么画。
手写了一段代码,没有编译,你可以参考一下:
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class JXFrame extends JFrame
{
boolean mouseCliked = false;
int x,y;
public void paintComponent(Graphics g)
{
if(mouseCliked )
{
g.setColor(255,255,0);//必须有这一步,否则画笔颜色为默认背景色,是根本看不出任何线条效果的。
g.drawLine(0,0,x,y);
}
}
public void setPoint(int x,int y)
{
this.x = x;
this.y = y;
}
}
package colin;
import java.awt.*;
import javax.swing.*;
class frame{
frame(){
JXFrame f = new JXFrame();
f.addMouseListener(new Mouseevent(f));
f.setSize(500,600);
f.setVisible(true);
}
public static void main(String args[]){
new frame();
}
}
package colin;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Mouseevent implements MouseListener{
JXFrame parent;
public Mouseevnt(JXFrame parent){
this.parent = parent;
}
public void mousePressed(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseEntered(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseExited(MouseEvent e){
this.parent.mouseCliked = false;
}
public void mouseClicked(MouseEvent e){
this.parent.setPoint(e.getX(),e.getY());
this.parent.mouseCliked = true;
this.parent.repaint();
}
}
楼主提问的水平很高啊,题目的要求都没有,如何回答。。。
相关类都在
import java.awt.*;
import javax.swing.* ;
动作事件相关类在import java.awt.event.*;中,在代码头import就可以看了
给一个相关的代码吧,包括所有的鼠标事件。
下面代码包括
按下,释放,进入区域,离开区域,点击 这5个事件
鼠标的相关事件会在窗口上方的文本区显示。。。
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseEvent extends JFrame implements MouseListener{
JPanel jp = new JPanel();
JTextField jtf = new JTextField();
public TestMouseEvent(){
setTitle("Mouse Event");
getContentPane().add(jtf,BorderLayout.NORTH);
getContentPane().add(jp,BorderLayout.CENTER);
jp.addMouseListener(this);
}
public static void main(String[] args){
TestMouseEvent frame = new TestMouseEvent();
frame.setSize(200,200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void mousePressed(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出按下";
jtf.setText(s);
}
public void mouseReleased(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出释放";
jtf.setText(s);
}
public void mouseClicked(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出点击";
jtf.setText(s);
}
public void mouseEntered(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出进入";
jtf.setText(s);
}
public void mouseExited(MouseEvent e){
int x = e.getX();
int y = e.getY();
String s = "鼠标在坐标"+"("+x+","+y+")"+"出离开";
jtf.setText(s);
}
}
使用组件的paint函数用于绘图, 使用MouseListener来响应鼠标的点击
效果图
代码
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.*;
import javax.swing.*;
public class DemoWin extends JFrame {
public DemoWin() {
MyPanel mp = new MyPanel();
mp.addMouseListener(mp);
add(mp);
// 窗口属性设置
setTitle("Demo");// 标题
setSize(300, 280);// 窗口大小
setLocationRelativeTo(null);// 窗口居中
setDefaultCloseOperation(EXIT_ON_CLOSE);// 窗口点击关闭时,退出程序
}
public static void main(String[] args) {
DemoWin win = new DemoWin(); // 创建窗口
win.setVisible(true);// 显示窗口
}
class MyPanel extends JPanel implements MouseListener {
int times;// 记录点击的次数
int x;// 记录鼠标X轴的位置
int y;// 记录鼠标Y轴的位置
@Override
public void paint(Graphics g) {
super.paint(g);
if (times == 0) {
g.setColor(Color.BLUE);// 颜色
g.fillOval(150, 150, 50, 50);// 150,150代表位置 50,50代表宽高
} else if (times == 1) {
g.setColor(Color.RED);
g.fillRect(150, 150, 50, 50);
} else {
g.setColor(Color.RED);
g.fillRect(x, y, 50, 50);
}
repaint();
}
public void mouseClicked(MouseEvent e) {
// if (e.getButton()==MouseEvent.BUTTON1) {//单击左键时有效..
// times++;// 记录点击的次数
// x = e.getX();
// y = e.getY();
// }
}
public void mousePressed(MouseEvent e) {// 鼠标按下就有效
times++;// 记录点击的次数
x = e.getX();
y = e.getY();
}
public void mouseReleased(MouseEvent e) {// 鼠标释放
}
public void mouseEntered(MouseEvent e) {// 鼠标移入
}
public void mouseExited(MouseEvent e) {// 鼠标移出
}
}
}