Java:如何通过单击箭头键摆脱特定的 JPanel?
这是我创建图形文本的内部类。我希望能够按箭头键并使其消失。我确信它以某种方式涉及删除方法,但我无法理解。这方面很新。
// STARTUP TEXT
class TextPanel extends JPanel implements KeyListener{
// CONSTRUCTOR
public TextPanel(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
// PAINT METHOD
public void paintComponent(Graphics g2){
super.paintComponent(g2);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
g2.setColor(Color.BLACK);
g2.setFont(new Font("TimesRoman", Font.PLAIN, 14));
g2.drawString("Press an arrow key to start", this.getWidth()/4, this.getHeight()/2);
}
This is my inner class that creates the graphic text. I want to be able to press an arrow key and have it disappear. I'm sure it involves the remove method somehow, but I'm in over my head. Very new at this.
// STARTUP TEXT
class TextPanel extends JPanel implements KeyListener{
// CONSTRUCTOR
public TextPanel(){
addKeyListener(this);
setFocusable(true);
setFocusTraversalKeysEnabled(false);
}
// PAINT METHOD
public void paintComponent(Graphics g2){
super.paintComponent(g2);
g2.setColor(Color.WHITE);
g2.fillRect(0, 0, this.getWidth(), this.getHeight());
g2.setColor(Color.BLACK);
g2.setFont(new Font("TimesRoman", Font.PLAIN, 14));
g2.drawString("Press an arrow key to start", this.getWidth()/4, this.getHeight()/2);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
AFAIK 您必须使用 Key Bindings 来响应箭头键要删除面板(我认为是从框架中),请使用面板的
setVisible(false)
或框架的remove(component)
方法。AFAIK You have to use Key Bindings to respond to arrow key then to remove panel (I think from frame) use either
setVisible(false)
of panel orremove(component)
method of frame.