java - 如何使自定义按钮的 toString() 与 MouseEntered() 一起使用?

标签 java swing

我正在尝试访问 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/

相关文章:

java - 单击按钮时出现运行时错误,并且显示错误发生在 Action Listener 行中

Netbeans 中的 java @action 方法参数

java - 如何在 JScrollPane 中隐藏但不禁用滚动条?

java - 对象数组中不可访问的公共(public)方法

java - 什么是 org.hibernate.criterion.Example 类?

java - 用于解析未知长度消息的分隔符(最佳实践?)java

java - 小程序出现奇怪的 ClassNotFound 错误

java - GridBagLayout 故障排除

java - 简单逻辑的计算器问题

java - SwingWorker 的 ExecutorService 参数