0
在這一刻我想添加到我的項目鍵盤支持,我有問題。我的聽衆沒有發現任何事件。我在代碼中做了簡單的打印,但沒有任何事情發生我從來沒有遇到過這樣的問題。KeyEvent永遠不會觸發(java swing)
這是我的課GUI:
public class GUI extends JPanel {
/**
*
*/
private static final long serialVersionUID = 1L;
private ComponentAbstract korzen;
private GUI self;
public GUI() {
self=this;
this.stworz_PanelLogowania();
this.addKeyListener(new KeyListener() {
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("typed");
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("pressed");
}
});
this.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
korzen.tryClick(e.getX(), e.getY());
repaint();
}
});
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
korzen.repaint();
System.out.println("omatko");
korzen.draw((Graphics2D)g);
}
private void zmien_panel(ComponentAbstract newkorzen){
korzen=newkorzen;
self.repaint();
}
private void stworz_PanelLogowania(){
LinearPanel lp=new LinearPanel(220, 50, 300, 300);
//lp.setOrientarion(Orientation.VERTICAL);
lp.addComponent(new Label(0, 0, 350, 40, "Witamy w castorama APP"));
lp.setPadding(2);
lp.addComponent(new TextBox(0, 0, 350, 40));
korzen=lp;
System.out.println("kuniec");
}
}
有趣的鼠標監聽器可以完美運行。 編輯:之前有KeyAdapter但結果是一樣的。