-1

在java中,我正在制作一个应用程序,当它被激活时(如果wantCommand = true),它将读取您按下的任何键,并将其添加到字符串中,然后使用Java.awt.Graphics,它将字符串绘制到屏幕...有人可以帮我编写代码以获取您输入的字符串(如果您弄乱了您输入的内容,也可能删除)这是我到目前为止的内容:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;

public class slashCommand {
    public static boolean wantCommand = false;
    public static String commandLine = "kill";
    public static String getText () {
        //get the string in a for loop or something maybe
        return commandLine;
    }
    public static void render(Graphics g) {
        if (wantCommand) {
            g.setColor(new Color(0, 0, 0, 130));
            g.fillRect(Inventory.inv_bag[0].x - 100, Inventory.inv_bag[0].y + 116, Component.size.width, 10);
            g.setColor(new Color(255, 255, 255));
            g.setFont(new Font("Arial", Font.PLAIN, 10));
            g.drawString("/ " + commandLine, Inventory.inv_bag[0].x - 69, Inventory.inv_bag[0].y + 125);
        }
    }
}
4

2 回答 2

2

你似乎还有很长的路要走。你需要:

  • 最重要的是一个 GUI,最好是 Swing 而不是 AWT,这样您的按键和字符串绘图就有一个应用程序来与之交互和显示数据。
  • 附加到具有焦点的组件的 KeyListener。请注意,我认为 Key Bindings 不能很好地解决这个问题。
  • 具有重写paintComponent(Graphics g)方法的 JPanel。
于 2014-02-12T20:29:09.950 回答
0

尝试向 GUI 注册一个 keyPressed 事件监听器;该事件告诉您按下哪个键可以传递

private void formKeyPressed(java.awt.event.KeyEvent evt)                                
{                                    
   System.out.println(evt.getKeyChar());
}         
于 2014-02-12T20:31:16.507 回答