Hallo Leute...
ich habe da ein kleines problem mit meinen KeyListener.
Wenn ich an den Button btn4 einen KeyListener hänge, reagiert er nicht. Der ActionListener geht.
Ich füge das JPanel (Code unten) einem JFrame hinzu...
woran kann das liegen?
ich habe da ein kleines problem mit meinen KeyListener.
Wenn ich an den Button btn4 einen KeyListener hänge, reagiert er nicht. Der ActionListener geht.
Ich füge das JPanel (Code unten) einem JFrame hinzu...
woran kann das liegen?
Code:
import javax.swing.*;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.awt.*;
public class FunctionKeys extends JPanel{
public FunctionKeys(){
GridBagLayout FuncLayout = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
Dimension PaneDim = new Dimension(300,200);
this.setLayout(FuncLayout);
this.setBackground(new Color(58,110,165));
this.setPreferredSize(PaneDim);
Dimension BtnDim = new Dimension(60,30);
JButton btn1 = new JButton("F9");
JButton btn2 = new JButton("F10");
JButton btn3 = new JButton("F11");
JButton btn4 = new JButton("F12");
btn1.setForeground(Color.WHITE);
btn2.setForeground(Color.WHITE);
btn3.setForeground(Color.WHITE);
btn4.setForeground(Color.WHITE);
btn1.setPreferredSize(BtnDim);
btn2.setPreferredSize(BtnDim);
btn3.setPreferredSize(BtnDim);
btn4.setPreferredSize(BtnDim);
constraints.gridwidth = GridBagConstraints.LINE_START;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
this.add(btn1,constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
JLabel btn1_text = new JLabel("Storno Einzelposition");
btn1_text.setForeground(Color.WHITE);
this.add(btn1_text, constraints);
constraints.gridwidth = GridBagConstraints.LINE_START;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
this.add(btn2,constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
JLabel btn2_text = new JLabel("Storno alle Positionen");
btn2_text.setForeground(Color.WHITE);
this.add(btn2_text,constraints);
constraints.gridwidth = GridBagConstraints.LINE_START;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
this.add(btn3,constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
JLabel btn3_text = new JLabel("Buchungsvorgang einleiten");
btn3_text.setForeground(Color.WHITE);
this.add(btn3_text,constraints);
constraints.gridwidth = GridBagConstraints.LINE_START;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
this.add(btn4,constraints);
constraints.gridwidth = GridBagConstraints.REMAINDER;
constraints.gridheight = 1;
constraints.insets = new Insets(5,5,5,5);
JLabel btn4_text = new JLabel("Programm beenden");
btn4_text.setForeground(Color.WHITE);
this.add(btn4_text,constraints);
btn4.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
System.out.println(e.getKeyChar());
}
public void keyTyped(KeyEvent e){
System.out.println(e.getKeyChar());
}
public void keyReleased(KeyEvent e){
System.out.println(e.getKeyChar());
}
});
btn4.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
}
}