- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试访问 toString() 以便在 MouseEntered() 事件中调用我的按钮,但它不起作用。任何帮助表示赞赏。它不是在标签上打印名称 + ""+ x + ""+ y ,而是打印类似 base.EmptySpace$1@ee2694
的内容public class EmptySpace extends JButton {
private int x;
private static int y;
private static String name;
public EmptySpace(String text, int x, int y){
super(text);
this.name = text;
this.x = x;
this.y = y;
addMouseListener(new MouseAdapter(){
@Override
public void mouseEntered(MouseEvent e){
Board.toStringText.setText(this.toString());
}
});
}
public String toString(){
return name + " " + x + " " + y;
}
public class Board extends JFrame{
private EmptySpace[][] buttons;
private JPanel toolTips;
private JPanel toStringPane;
protected static JLabel toStringText;
private static int lengthy;
private static int lengthx;
public Board(int x, int y){
lengthy = y;
lengthx = x;
}
public void printBoard(){
int x;
int y;
JPanel pane = new JPanel();
pane.setLayout(new GridLayout(lengthy, lengthx));
buttons = new EmptySpace[lengthx][lengthy];
for (int i = 0; i < lengthx*lengthy; i++) {
if(i<lengthx){
x = i+1;
}else x = i % lengthx+1;
if(i<lengthx){
y=1;
}else y = i/lengthx+1;
String xString = Integer.toString(x);
String yString = Methods.getChar(y);
buttons[x-1][y-1] = new EmptySpace(xString+yString,x,y);
buttons[x-1][y-1].setPreferredSize(new Dimension(25, 25));
buttons[x-1][y-1].setBackground(Color.WHITE);
buttons[x-1][y-1].setText("");
buttons[x-1][y-1].setToolTipText(xString+yString);
pane.add(buttons[x-1][y-1]);
}
System.out.println(buttons[1][9].toString());
toolTips = new JPanel();
toolTips.setPreferredSize(new Dimension(200,500));
toolTips.setBackground(Color.WHITE);
toStringPane = new JPanel();
toStringPane.setBackground(Color.GRAY);
toStringText = new JLabel();
toStringText.setText("test");
toStringText.setPreferredSize(new Dimension(175,50));
toStringText.setForeground(Color.black);
toolTips.add(toStringPane);
toStringPane.add(toStringText);
JFrame frame = new JFrame("Trenches");
SpringLayout layout = new SpringLayout();
frame.setLayout(layout);
layout.putConstraint(SpringLayout.WEST, pane, (1280-lengthx*25)/2, SpringLayout.WEST, frame);
layout.putConstraint(SpringLayout.NORTH, pane, (720-lengthy*30)/3, SpringLayout.NORTH, frame);
layout.putConstraint(SpringLayout.WEST, toolTips, ((1280-lengthx*25)-200)/8, SpringLayout.WEST, frame);
layout.putConstraint(SpringLayout.NORTH, toolTips, (720-lengthy*30)/3, SpringLayout.NORTH, frame);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(pane);
frame.add(toolTips);
frame.setSize(1280, 720);
frame.setVisible(true);
}
}
最佳答案
MouseListener 未使用 EmptySpace.toString
方法 - mouseEntered
实现中的调用 toString
引用了 MouseAdapter 实例。要引用适当的 JButton
实例并调用它的 toString
方法,您可以获取事件源:
Board.toStringText.setText(e.getSource().toString());
或者引用外部类实例:
Board.toStringText.setText(EmptySpace.this.toString());
关于java - 如何使自定义按钮的 toString() 与 MouseEntered() 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30009433/
我试图在鼠标输入鼠标离开事件时替换图像属性源。单击时,图像应保持事件状态。我需要 mouseleave 事件在单击后停止。到目前为止,点击后鼠标离开仍然继续,切换回图像,代码如下: jQue
我经常看到这样的代码: $("#thing").on("mouseenter",function(){ Do stuff }); 就个人而言,我几乎总是这样写: $("#thing").mouseen
我用 mouseenter 和 mouseleave 制作了一些积木 hover MouseEnter(e) { setTimeout(() => { //check if m
使用菜单目标: https://github.com/hfknight/jQuery-menu-aim/blob/master/jquery.menu-aim.js 在响应方面遇到问题。它使用 mou
我有一个盒子,悬停时会播放加载动画,然后在 2 秒后展开: http://jsfiddle.net/frank_o/WwD5V/23/embedded/result/ 但是如果我按照下面的 V 形 A
我是 Jquery 和 JS 的新手,所以希望有人能够提供帮助!我已经构建了我的第一个图像 slider ,除了手持设备之外,它都运行良好。基本上,当宽度降至某个点以下时,幻灯片会重叠,但是当我将鼠标
我正在编写一个函数,当鼠标进入 DOM 的特定部分以显示菜单时,删除包含内容 display:hidden; 的类。现在,当页面加载并且我最初将鼠标悬停在该区域上时,该事件不会触发。但是,如果我将鼠标
我有一组 3 个菜单项,我正在尝试向其中添加 mouseenter 事件。我似乎根本无法让它发挥作用。我还尝试循环遍历 .menu-item 类,但没有任何反应。 这让我发疯。有谁知道为什么这不起作用
我正在创建一个自定义光标“预告片”(请参阅 fiddle :https://jsfiddle.net/alexdlf/hx6yzu4w/) 通常,圆圈 (.cursor) 应跟随光标。将鼠标悬停在
我有两个对话气泡每两秒更改一次内容。我试图每次将鼠标放在气泡上时,我的changeComment函数都会停止,这样用户将有更多时间阅读评论,当鼠标离开气泡时,函数将再次启动。 我在这里有我的演示:ht
我编写了一段代码,当鼠标悬停在特定元素上时,我需要触发功能。 对于以下所有静态代码,其工作正常 $("table td").on("mouseenter",function(){ consol
我有一个由 5 个 JLabel 对象组成的数组,并且我已向所有对象添加了鼠标监听器,在 mouseEntered() 函数中,我将标签设为不透明(true)更改其背景颜色,但此代码无法正常工作,请帮
我正在尝试进行 mouseEntered 测试来更改方形颜色,但是 MouseListener mouseEntered 不会执行。鼠标有响应,但仅对单击、按下和释放有响应。所以我不确定发生了什么事。
我在尝试制作一个更改其图标的突出显示“标签”时遇到问题,好吧,所以当为一个 jLabel 调用 MouseEntered 事件时,附近的每个标签的事件也会被调用,并且它们的图标也会被更改。我尝试通过使
我必须调用一个由特定元素上的 mouseenter 事件触发的函数。 如何仅当鼠标在特定元素上“停留”超过 5 秒时才触发 mouseenter 事件? 最佳答案 你可以使用定时器(setTimeou
我有两个可排序列表,一个是嵌套的,鼠标输入对嵌套可排序列表的 li 元素有效果。我的问题是,当用户在作为输入字段的子元素上的列表中快速移动鼠标时,mouseenter 和 mouseleave 函数的
我有一些小div,单击它们时,会显示一个更大的div,覆盖所有内容 - 有点像灯箱。为了隐藏大 div,我单击它(没有“关闭此”链接)。 这是点击代码: // First click - draw i
我有一个网站,当您将鼠标悬停在图像上时,图像上会显示一段文本。 达到了预期的效果,除了以下事实:如果您的光标位于图像上的新文本元素上,则所有内容都会开始闪烁。 任何帮助将不胜感激。 这是 fiddle
当元素上触发 mouseenter 事件时,我使用此指令执行某些操作。但是,当我快速拖动鼠标时,在元素上某些元素会被跳过,而不会触发 mouseenter 事件。 我实际上想在鼠标移动时突出显示网格的
基本上,我有一个包含大约 12 个列表项的无序列表。我想在鼠标悬停在其中任何一个上时对它们应用一种样式,而在鼠标不在任何一个上时应用不同的样式。 例如,以下是我当前的代码,它只有在悬停 li 时才具有
我是一名优秀的程序员,十分优秀!