答:
代码在这你自己看合不合适。
有些地方需要注意:这个按钮出现文本的要求,当鼠标从上大到下或者从右到左出现在按钮上方,按钮感知不敏感,也就是不会很好显示文字,但是从左到右或者从下到上没任何问题。还有你的第一个要求有点不是很懂,如果有使用场景就好,但是功能也做出来了,你看看合不合适。
代码在这,你自己最好运行试试
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GUIProgram {
public static void main(String[] args) {
new CustomJFrame();
}
}
class CustomJFrame extends JFrame {
private boolean textFocus = false;
private JButton innerButton;
public CustomJFrame() {
setTitle("Custom JFrame Test");
setSize(300, 400);
setLayout(null);
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
JTextField textField1 = new JTextField("");
textField1.setColumns(2);
textField1.setSize((int) (this.getSize().width * 0.8), 20);
textField1.setLocation((int) (this.getSize().width * 0.1), 10);
textField1.addFocusListener(new CustomTextFieldFocusListener(textField1, this));
textField1.addKeyListener(new CustomKeyboardListener(this, textField1));
add(textField1);
JTextField textField2 = new JTextField("");
textField2.setColumns(2);
textField2.setSize((int) (this.getSize().width * 0.8), 20);
textField2.setLocation((int) (this.getSize().width * 0.1), 40);
textField2.addFocusListener(new CustomTextFieldFocusListener(textField1, this));
textField2.addKeyListener(new CustomKeyboardListener(this, textField2));
add(textField2);
innerButton = new JButton("****");
innerButton.setLocation((int) (this.getSize().width * 0.1), 60);
innerButton.setSize(50, 30);
innerButton.setVisible(false);
addMouseMotionListener(new CustomButtonFocusListener(innerButton));
add(innerButton);
}
public boolean isTextFocus() {
return textFocus;
}
public void setTextFocus(boolean textFocus) {
this.textFocus = textFocus;
}
public JButton getInnerButton() {
return innerButton;
}
}
class CustomTextFieldFocusListener implements FocusListener{
private JTextField textField;
private CustomJFrame frame;
public CustomTextFieldFocusListener(JTextField textField, CustomJFrame frame) {
this.textField = textField;
this.frame = frame;
}
@Override
public void focusGained(FocusEvent e) {
frame.setTextFocus(true);
System.out.println(e);
}
@Override
public void focusLost(FocusEvent e) {
frame.setTextFocus(false);
String text = textField.getText();
try {
int num = Integer.parseInt(text);
if (num < 0 || num > 11) {
JOptionPane.showMessageDialog(frame, "输入只能是0-11", "tips", JOptionPane.WARNING_MESSAGE);
}
} catch (NumberFormatException n) {
JOptionPane.showMessageDialog(frame, "输入只能是0-11", "tips", JOptionPane.WARNING_MESSAGE);
}
}
}
class CustomKeyboardListener extends KeyAdapter {
private CustomJFrame frame;
private JTextField jTextField;
public CustomKeyboardListener(CustomJFrame frame, JTextField jTextField) {
this.frame = frame;
this.jTextField = jTextField;
}
@Override
public void keyTyped(KeyEvent e) {
// System.out.println(e);
if (frame.isTextFocus()) {
frame.getInnerButton().setVisible(true);
}
}
}
class CustomButtonFocusListener extends MouseMotionAdapter {
private JButton button;
public CustomButtonFocusListener(JButton button) {
this.button = button;
}
@Override
public void mouseMoved(MouseEvent e) {
// System.out.println(e.getPoint());
// System.out.println(button.getLocation());
// System.out.println("----------------");
if (hover(e.getPoint())) {
button.setText("*");
} else if (!button.getText().equals("")) {
button.setText("");
}
}
boolean hover(Point p) {
//这里由于point计算有点不同,鼠标的Location计算好像算上了窗口顶部白色的部分
p.setLocation(p.getX(), p.getY() - 40);
Point bp = button.getLocation();
Dimension bs = button.getSize();
if (p.getX() >= bp.getX() && p.getX() <= (bp.getX() + bs.width) && p.getY() >= bp.getY() && p.getY() <= (bp.getY() + bs.height)) {
return true;
}
return false;
}
}