我在使用Rectangle类制作的菜单中显示了文本。我想知道我是否可以这样做以便能够选择文本,并且您可以使用键盘箭头选择您的选择,然后按Enter键,然后它将执行下一个操作。这就是我到目前为止所拥有的:
public Rectangle menuBorder;
public Rectangle menu;
BufferedImage character;
private java.awt.Point characterLocation;
private java.awt.Point borderLocation;
private java.awt.Point menuLocation;
public int charW = 24;
public int charH = 36;
public int borderW = 210;
public int borderH = 80;
public int menuW = 190;
public int menuH = 60;
public boolean right = false;
public boolean left = false;
public boolean up = false;
public boolean down = false;
public Keying(Display f, Images i) throws IOException
{
character = ImageIO.read(getClass().getResource("starting mon.png"));
menuBorder = new Rectangle(10, 10, borderW, borderH);
menu = new Rectangle(10, 10, menuW, menuH);
characterLocation = new Point(175, 100);
borderLocation = new Point(175, 270);
menuLocation = new Point(185, 280);
//...
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
this.setBackground(Color.white);
g.drawImage(character, characterLocation.x, characterLocation.y, this);
g.setColor(Color.black);
g.fillRect(borderLocation.x, borderLocation.y, borderW, borderH);
g.setColor(Color.white);
g.fillRect(menuLocation.x, menuLocation.y, menuW, menuH);
g.setColor(Color.black);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.drawString("Battle", 190, 305);
g.drawString("Feed", 320, 305);
g.drawString("Pet", 200, 335);
g.drawString("Age", 325, 335);
}
//...
答案 0 :(得分:0)
首先,请参阅我关于绘画的第一条评论......
您将需要某种“状态”变量,以便您可以确定应该绘制哪种状态,这可以通过使用某种ListSelectionModel
来实现。
您可能需要MouseListener
和MouseMotionListener
来监控鼠标的状态,有关详细信息,请参阅How to Write a Mouse Listener。
您可能还想查看How to Use Key Bindings。
当然,您可以使用JList
并省去麻烦......请参阅How to Use Lists