我正在尝试使用 Java 的 KeyListener 在我键入时更新 JLabel。本质上,我正在制作自己的文本字段。这是我所拥有的:
/**
* Constructor for objects of class Dictionary
*/
public Dictionary()
{
frame = new JFrame();
frame.setTitle("Shori Dictionary");
frame.setLayout(new GridBagLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private void createWord()
{
frame.remove(pane);
pane = new PaintPane(field.getImage());
pane.setLayout(new BorderLayout());
frame.add(pane);
frame.pack();
newWord = new JLabel(text);
newWord.setFont(newWord.getFont().deriveFont(Font.BOLD, 28));
newWord.setForeground(Color.BLACK);
newWord.setHorizontalTextPosition(JLabel.LEFT);
newWord.setVerticalAlignment(JLabel.TOP);
newWord.setVerticalTextPosition(JLabel.TOP);
newWord.setBorder(BorderFactory.createEmptyBorder(445, 150, 0, 0));
pane.add(newWord);
frame.pack();
frame.setLocationRelativeTo(null);
pane.setFocusable(true);
updateInteraction();
}
private void keyPress()
{
pane.addKeyListener(new KeyListener()
{
public void keyTyped(KeyEvent e) {
for(int i = 97; i <= 122; i++){
//Cycles through every lowercase letter
if(e.getKeyChar() == (char)(i)&& pane.returnImage() == field.getImage()){
text += (char)(i);
break;
}
}
//Even in the Debugger, these next if-elses have never worked
if(e.getKeyCode() == KeyEvent.VK_SPACE&& pane.returnImage() == field.getImage()) text += " ";
else if(e.getKeyCode() == KeyEvent.VK_BACK_SPACE&& pane.returnImage() == field.getImage()){
int x = text.length();
text = text.substring(0,x-1); //Not sure if this works, haven't been able to test it yet
}
else if(e.getKeyCode() == KeyEvent.VK_ENTER&& pane.returnImage() == field.getImage()){
//do something with the text
text = "";
//exit the word creator
}
newWord.setText(text);
newWord.repaint(); //Apparently this isn't necessary...
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
);
}
private void mouseAction()
{
pane.addMouseListener(new MouseListener()
{
public void mouseClicked(MouseEvent arg0) {
//cover page
if(open.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) displayPages();
else if(search.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) searchWord();
else if(enter.contains(arg0.getPoint())&& pane.returnImage() == cover.getImage()) createWord();
//inner pages
else if(nextPage.contains(arg0.getPoint())&& pane.returnImage() == pages.getImage()) pageFlip("next");
else if(prevPage.contains(arg0.getPoint())&& pane.returnImage() == pages.getImage()) pageFlip("previous");
else if(cancel.contains(arg0.getPoint())&& pane.returnImage() == field.getImage()) coverPage();
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void mouseEntered(MouseEvent arg0) {
}
public void mouseExited(MouseEvent arg0) {
}
public void mousePressed(MouseEvent arg0) {
}
public void mouseReleased(MouseEvent arg0) {
}
}
);
}
private void mouseMovement()
{
pane.addMouseMotionListener(new MouseMotionListener()
{
@Override
public void mouseMoved(MouseEvent e) {
if(search.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(enter.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(open.contains(e.getPoint())&& pane.returnImage() == cover.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(nextPage.contains(e.getPoint())&& pane.returnImage() == pages.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(prevPage.contains(e.getPoint())&& pane.returnImage() == pages.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else if(cancel.contains(e.getPoint())&& pane.returnImage() == field.getImage()){
frame.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
}
else{
frame.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
}
@Override
public void mouseDragged(MouseEvent e) {
}
}
);
}
private void updateInteraction(){
mouseMovement();
mouseAction();
keyPress();
}
public class PaintPane extends JPanel {
private Image background;
private Graphics g2d;
public PaintPane(Image image) {
background = image;
}
@Override
public Dimension getPreferredSize() {
return background == null ? new Dimension(0, 0) : new Dimension(background.getWidth(this), background.getHeight(this));
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (background != null) {
Insets insets = getInsets();
int width = getWidth() - 1 - (insets.left + insets.right);
int height = getHeight() - 1 - (insets.top + insets.bottom);
int x = (width - background.getWidth(this)) / 2;
int y = (height - background.getHeight(this)) / 2;
g.drawImage(background, x, y, this);
}
//g.fillRect(654, 798, 358, 77); //for testing rectangle positioning
}
public Image returnImage() {
return background;
}
}
我正在使用 BluJ 来编写它,它有一个内置的调试器。我只是尝试在 createWord()keyPress();
之前添加updateInteraction();
,然后运行调试器逐步完成每个方法。一切都很完美。然后我尝试不使用调试器,在我输入时它不会显示任何文本。所以,我再次打开了调试器。它根本没有检测到任何正在键入的键。我不知道为什么它只起作用了一次,但它肯定起作用了。这是我第一次使用 KeyListener、MouseListener 和 MouseMotionListener。有没有更好的方法让这个程序正常运行?