mousereleased,mousereleasedevent
java中label 文字 如何 添加点击 执行反应?
实现MouseListener接口.
新增一个专门的"鼠标监听器"类,或者直接在你现有的类中实现"鼠标监听器".具体做法是在类声明语句中implements MouseListener.如下:
public class MouseAdp implements MouseListener{
public MouseAdp(){}
public void mouseClicked(MouseEvent e) {
/**鼠标点击事件(包括按下和弹起两个动作)处理方法.**/
System.out.println("你点了我!");
}
public void mouseEntered(MouseEvent e) {
/**鼠标移到组件上方法时事件处理方法.**/}
public void mouseExited(MouseEvent e) {
/**鼠标移开组件时事件处理方法.**/}
public void mousePressed(MouseEvent e) {
/**鼠标在组件上按下(但没弹起)时事件处理方法.**/}
public void mouseReleased(MouseEvent e) {
/**鼠标在组件上弹起事件处理方法.**/}
}
然后在你的JLabel实例上,作用这个监听器,如:
JLabel lab = new JLabel("点我");
lab.addMouseListener(new MouseAdp());
Java中如何判断鼠标进入哪个区域
需要考虑吗 你想对鼠标在哪个组件的点击做出响应就在哪个组件添加鼠标监视器就可以了啊 如果是一个区域的话也一样 只不过是你看不到而已 比如说center区域做出响应 你就在那里面就放一个panel 大小和区域面积相等就可以了 然后添加鼠标监视器
java中弹出式菜单怎么用show方法显示 import java.awt.*; import javax.swing.*; public class S{ public s
代码如下:为窗体增加鼠标监听,实现public void mouseReleased(MouseEvent e) {
if ((e.getModifiers()MouseEvent.BUTTON3_MASK)!=0
!e.isControlDown()!e.isShiftDown()) {
popup.show(jFrame, e.getX(),e.getY());
}
});
实现显示弹出菜单
为菜单增添时间监听,实现其功能。
完整代码如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.Popup;
public class TestPopup {
JPopupMenu popup;
JFrame jFrame;
public TestPopup(){
jFrame=new JFrame("测试");
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.setVisible(true);
jFrame.setBounds(300,200,400,500);
popup=new JPopupMenu();
JMenuItem mItem=new JMenuItem("复制");
JMenuItem mItem2=new JMenuItem("粘贴");
JMenuItem mItem3=new JMenuItem("剪切");
mItem.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("复制:菜单事件处理");
}
});
mItem2.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("粘贴:菜单事件处理");
}
});
mItem3.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("剪切:菜单事件处理");
}
});
popup.add(mItem);
popup.add(mItem2);
popup.add(mItem3);
//jFrame.add(popup);
jFrame.addMouseListener(new MouseListener(){
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
if ((e.getModifiers()MouseEvent.BUTTON3_MASK)!=0
!e.isControlDown()!e.isShiftDown()) {
popup.show(jFrame, e.getX(),e.getY());
}
}
});
}
public static void main(String[] args) {
new TestPopup();
}
}
有什么疑问可以hi我
java画乌龟
首先,手动画一个小乌龟,如下:
然后,按照Java绘图基本步骤一步步来。
swing 编程步骤:
1. 继承JFrame
2. 定义组件
3.创建组件(构造函数)
4.添加组件
5.对窗体设置
6.显示窗体
最终效果如下:
代码如下:
/**?
?*?功能:画一个乌龟?
?*/??
??
package?com.test1;??
??
import?java.awt.*;??
??
import?javax.swing.*;??
public?class?MyTortoise??extends?JFrame{??
????MyPanel2?mp?=?null;??
????//构造函数??
????public?MyTortoise(){??
????????mp?=?new?MyPanel2();??
??????????
????????this.add(mp);??
??????????
????????this.setTitle("小乌龟,丑丑哒");??
????????this.setSize(400,300);??
????????this.setVisible(true);??
????????this.setLocation(300,200);??
????????this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);??
????}??
??????????????
????public?static?void?main(String[]?args)?{??
????????MyTortoise?mtg?=?new?MyTortoise();??
????}?????
}??
??
//我的面板。只有JPanel有画图方法,JFrame没有,故必须在JFrame中添加JPanel??
class?MyPanel2?extends?JPanel{??
????//定义一个乌龟??
????Tortoise?t?=?null;??
??????
????//构造函数??
????public?MyPanel2(){????
????????t?=?new??Tortoise(100,100);??
????}??
??????
????//画乌龟??
????public?void?drawTortoise(int?x,?int?y,?Graphics?g){??
????????//1.画脸??
????????g.setColor(Color.green);??
????????g.fillOval(x+60,?y,?30,?15);??
????????//2.画左眼??
????????g.setColor(Color.black);??
????????g.fillOval(x+65,?y+3,?5,?5);??
????????//3.画右眼??
????????g.fillOval(x+78,?y+3,?5,?5);??
????????//4.画脖子??
????????g.setColor(Color.green);??
????????g.fillOval(x+70,?y,?10,?42);??
????????//5.画乌龟壳??
????????g.setColor(Color.red);??
????????g.fillOval(x+40,?y+40,?70,?100);??
????????//6.画左上脚??
????????g.setColor(Color.green);??
????????g.fillOval(x+15,?y+60,?30,?10);??
????????//7.画右上脚??
????????g.fillOval(x+105,?y+60,?30,?10);??
????????//8.画左下脚??
????????g.fillOval(x+15,?y+110,?30,?10);??
????????//9.画右下脚??
????????g.fillOval(x+105,?y+110,?30,?10);??
????????//10.画尾巴??
????????g.setColor(Color.black);??
????????g.drawLine(x+70,y+140,x+130,y+210);??
????????g.drawOval(x+95,?y+150,?30,?30);??
????}??
??
?????
????//覆盖JPanel的paint方法??
????//Graphics?是绘图的重要类。你可以把他理解成一只画笔??
????public?void?paint(Graphics?g){??
????????????//1.调用父类函数完成初始化任务??
????????????//这句话不能少??
????????????super.paint(g);??
????????????//2.画乌龟,调用方法即可??
????????????this.drawTortoise(50,?50,?g);??
????}??
??????
}??
??
//定义一个乌龟类??
class?Tortoise?{??
????????//表示乌龟的横坐标??
????????int?x?=?0;??
??
????????//表示乌龟的纵坐标??
????????int?y?=?0;??
??????????
????????public?int?getX()?{??
????????????return?x;??
????????}??
??
????????public?void?setX(int?x)?{??
????????????this.x?=?x;??
????????}??
??
????????public?int?getY()?{??
????????????return?y;??
????????}??
??
????????public?void?setY(int?y)?{??
????????????this.y?=?y;??
????????}??
????????public?Tortoise(int?x,?int?y){??
????????????this.x?=?x;??
????????????this.y?=?y;??
????????}??
}