简体   繁体   English

按下时更改JButton的文本

[英]Changing the text of JButton when pressed

I want to change the text of a JButton when it is pressed. 我想在按下JButton时更改其文本。 So far, the text does change when the button is pressed, but when I move my mouse outside the button, the text reverts to what it was before (in this case, an empty string). 到目前为止,文本确实在按下按钮时发生了变化,但是当我将鼠标移到按钮外时,文本恢复为以前的状态(在这种情况下为空字符串)。 How do I ensure that the text stays changed on the JButton? 如何确保文本在JButton上保持不变?

Here is my code: 这是我的代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Tile extends JButton implements ActionListener {
    private int xPos, yPos;
    private char value;

    Tile(int x, int y) {
        xPos = x;
        yPos = y;
        setFont(this.getFont().deriveFont(Font.PLAIN, 45f));
        addActionListener(this);
    }

    @Override
    public int getX() {
        return xPos;
    }

    @Override
    public int getY() {
        return yPos;
    }

    public char getValue() {
        return value;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        value = 'X';
        setText(Character.toString(value));
    }
}

the text does change when the button is pressed, but when I move my mouse outside the button, the text reverts to what it was before 按下按钮后,文本确实发生了变化,但是当我将鼠标移到按钮外时,文本恢复为以前的状态

Therefore, I guess you would need to read the apidocs about the selected state and aimed state of button. 因此,我想您需要阅读有关按钮的选定状态和目标状态的apidocs。

ButtonModel (Java Platform SE 8 ) ButtonModel(Java平台SE 8)
In details, the state model for buttons works as follows when used with the mouse: Pressing the mouse on top of a button makes the model both armed and pressed. 详细地说,当与鼠标一起使用时,按钮的状态模型的工作方式如下:在按钮顶部按下鼠标可使模型同时处于准备状态和按下状态。 As long as the mouse remains down, the model remains pressed, even if the mouse moves outside the button. 只要鼠标保持按下状态,即使鼠标移到按钮外部,模型也会保持按下状态。 On the contrary, the model is only armed while the mouse remains pressed within the bounds of the button (it can move in or out of the button, but the model is only armed during the portion of time spent within the button). 相反,仅在鼠标保持在按钮范围之内时才武装模型(模型可以移入或移出按钮,但是模型仅在按钮内花费的时间中处于武装状态)。 A button is triggered, and an ActionEvent is fired, when the mouse is released while the model is armed - meaning when it is released over top of the button after the mouse has previously been pressed on that button (and not already released). 当模型处于预备状态时释放鼠标时,将触发一个按钮,并触发一个ActionEvent,这意味着在先前已在该按钮上按下鼠标(但尚未释放)之后将其释放到按钮上方。 Upon mouse release, the model becomes unarmed and unpressed. 释放鼠标后,该模型将变为未武装且未受压的状态。

import java.awt.*;
import javax.swing.*;

public class TileTest {
  private JComponent makeUI() {
    JButton button1 = new JButton("A");
    button1.addChangeListener(e -> {
      JButton b = (JButton) e.getSource();
      ButtonModel m = b.getModel();
      boolean isPressedAndArmed = m.isPressed() && m.isArmed();
      b.setText(isPressedAndArmed ? "X" : "A");
    });

    JButton button2 = new JButton("B");
    button2.addChangeListener(e -> {
      JButton b = (JButton) e.getSource();
      ButtonModel m = b.getModel();
      boolean isPressed = m.isPressed();
      b.setText(isPressed ? "X" : "B");
    });

    JPanel p = new JPanel();
    p.add(button1);
    p.add(button2);
    return p;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(() -> {
      JFrame f = new JFrame();
      f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
      f.getContentPane().add(new TileTest().makeUI());
      f.setSize(320, 240);
      f.setLocationRelativeTo(null);
      f.setVisible(true);
    });
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM