vendredi 22 mai 2015
KeyListener funktioniert nicht richitg
Posted on 07:58 by verona
Hallihallo,
also erstmal der code
Fenster
FensterMatrix1
FensterMatrix2
also mein Problem ist folgendes:
Ich starte das Programm über Fenster. Dann öffnet sich FensterMatrix1 und ich kann, so wie ich das auch alles will, über den KeyListener 4 balken bewegen mit denen ich meine Auswahl treffe. Dann klicke ich "einlesen", MatrixFenster2 öffnet sich und MatrixFenster1.1 schließt sich. Nach meiner Eingabe+button"ok" in MF2 schließt sich MF2 und MF1.2 öffnet sich. Soweit so gut, aber jetzt geht der KeyListener in MF1 nicht mehr.
Ich habe ja schon eine syso gemacht und es scheint tatsächlich so, dass der KeyListener einfach nicht mehr da ist. Es gibt bei MF1.2 keine syso mehr, egal welche taste ich drücke, bei MF1.1 hingegen wird bei jedem Tastenanschlag eine syso gemacht.
Also woran kann das liegen, dass der KeyListener bei MF1.2 nicht mehr funktioniert?
Nooil
also erstmal der code
Fenster
Java Code:
-
package fenster;
-
-
import java.awt.Color;
-
import java.awt.Point;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.ActionListener;
-
import java.awt.event.KeyEvent;
-
import java.awt.event.KeyListener;
-
import java.util.ArrayList;
-
-
-
import javax.swing.JButton;
-
import javax.swing.JFrame;
-
-
-
import com.sun.javafx.scene.control.skin.ButtonSkin;
-
-
-
-
-
private static final long serialVersionUID = 1L;
-
static ArrayList<ArrayList<Integer>> m=new ArrayList<ArrayList<Integer>>();
-
public static ArrayList<String> Objekt=new ArrayList<String>();//objektspeicher aus fenstermatrix2
-
public static ArrayList<ArrayList<ArrayList<Integer>>> matrixSpeicher=new ArrayList<ArrayList<ArrayList<Integer>>>();
-
-
-
}
-
-
}
-
-
}
-
-
}
-
-
-
-
public Fenster(){
-
-
}
-
-
// Fenster f=new FensterScreenshot(Color.BLACK);
-
m.add(new ArrayList<Integer>());m.get(0).add(0);m.get(0).add(0);m.get(0).add(1);m.get(0).add(1);
-
m.add(new ArrayList<Integer>());m.get(1).add(1);m.get(1).add(1);m.get(1).add(0);m.get(1).add(0);
-
m.add(new ArrayList<Integer>());m.get(2).add(1);m.get(2).add(0);m.get(2).add(1);m.get(2).add(1);
-
Fenster matrixFenster=new FensterMatrix1(m, 50);
-
Fenster matrixFenster2=new FensterMatrix1(m, 50);
-
}
-
-
-
-
}
FensterMatrix1
Java Code:
-
package fenster;
-
-
import java.awt.Color;
-
import java.awt.Point;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.KeyEvent;
-
import java.awt.event.KeyListener;
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.concurrent.TimeUnit;
-
-
-
import javax.swing.*;
-
import java.awt.*;
-
-
-
import jdk.jfr.events.ExceptionThrownEvent;
-
-
-
public class FensterMatrix1 extends Fenster {
-
int buttonSize;
-
JButton b1,b2,b3,b4;
-
ArrayList<ArrayList<Integer>> matrix;
-
-
public FensterMatrix1(ArrayList<ArrayList<Integer>> matrix, int buttonSize){// mit 1-4 striche bewegen --> alle kästchen werden aufgedeckt
-
-
-
setLayout(null);
-
// setUndecorated(true);//vor setVisible
-
// opacity=0.5f;
-
// setOpacity(opacity);
-
// ArrayList<ArrayList<Integer>> matrix;//matrix elemente alle gleich lang
-
-
-
this.buttonSize=buttonSize;
-
this.matrix=matrix;
-
setVisible(true);
-
setSize(buttonSize*matrix.get(0).size()+200,buttonSize*matrix.size()+100);
-
setTitle("nicht erkannt");
-
setLocationRelativeTo(null);
-
setResizable(false);
-
-
-
-
-
b1.setBorderPainted(false);
-
b2.setBorderPainted(false);
-
b3.setBorderPainted(false);
-
b4.setBorderPainted(false);
-
b1.setBounds(0, buttonSize, this.getWidth(), 1);
-
b2.setBounds(0, buttonSize, this.getWidth(), 1);
-
b3.setBounds(buttonSize, 0, 1, this.getHeight());
-
b4.setBounds(buttonSize, 0, 1, this.getHeight());
-
add(b1);
-
add(b2);
-
add(b3);
-
add(b4);
-
-
-
-
for(int i=0;i<matrix.size();i++){
-
for(int p=0;p<matrix.get(0).size();p++){
-
if(matrix.get(i).get(p)==1)
-
else
-
button.setBounds(buttonSize*(p+1), buttonSize*(i+1), buttonSize, buttonSize);
-
button.setRolloverEnabled(false);
-
button.setBorderPainted(false);
-
// button.setBorder(null);
-
add(button);
-
matrixLocations[p][i]=button.getLocation();
-
}
-
}
-
-
einlesen.setBounds(this.getWidth()-130, this.getHeight()-100, 100, 20);
-
fertig.setBounds(this.getWidth()-130, this.getHeight()-70, 100, 20);
-
add(einlesen);
-
add(fertig);
-
-
-
-
// Listener listener=new Listener();
-
einlesen.addActionListener(this);
-
fertig.addActionListener(this);
-
addKeyListener(this);
-
}
-
-
int count1=0;int count2=0;int count3=0;int count4=0;
-
int length=matrixLocations[0].length-1;int width=matrixLocations.length-1;int size=matrixLocations[0][1].y-matrixLocations[0][0].y;
-
if(key.getKeyChar()=='1'){
-
if(count1<length+1){
-
count1++;
-
b1.setLocation(b1.getX(), matrixLocations[0][length].y+(count1-length)*size);
-
}
-
}else if(key.getKeyChar()=='!'){
-
if(count1>0){
-
count1--;
-
b1.setLocation(b1.getX(), matrixLocations[0][length].y+(count1-length)*size);
-
}
-
}else if(key.getKeyChar()=='2'){
-
if(count2<length+1){
-
count2++;
-
b2.setLocation(b2.getX(), matrixLocations[0][length].y+(count2-length)*size);
-
}
-
}else if(key.getKeyChar()=='"'){
-
if(count2>0){
-
count2--;
-
b2.setLocation(b2.getX(), matrixLocations[0][length].y+(count2-length)*size);
-
}
-
}else if(key.getKeyChar()=='3'){
-
if(count3<width+1){
-
count3++;
-
b3.setLocation(matrixLocations[width][0].x+(count3-width)*size,b3.getY() );
-
}
-
}else if(key.getKeyChar()=='§'){
-
if(count3>0){
-
count3--;
-
b3.setLocation(matrixLocations[width][0].x+(count3-width)*size,b3.getY() );
-
}
-
}else if(key.getKeyChar()=='4'){
-
if(count4<width+1){
-
count4++;
-
b4.setLocation(matrixLocations[width][0].x+(count4-width)*size,b4.getY() );
-
}
-
}else if(key.getKeyChar()=='$'){
-
if(count4>0){
-
count4--;
-
b4.setLocation(matrixLocations[width][0].x+(count4-width)*size,b4.getY() );
-
}
-
}else if(key.getKeyChar()=='\n'){
-
ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
-
for(int i=0;i<matrix.size();i++){//matrix.length=die höhe; matrix[0].length = breite
-
ArrayList<Integer> al=new ArrayList<Integer>();
-
for(int p=0;p<matrix.get(0).size();p++){
-
// System.out.println("ml"+matrixLocations.length);//ml.length=breite; ml[0].lenth=höhe
-
// System.out.println("ml[0]"+matrixLocations[0].length);
-
// System.out.println("i: "+i+" p: "+p);
-
al.add(matrix.get(i).get(p));
-
}
-
}
-
if(al.size()>0){
-
list.add(al);
-
}
-
}
-
if(list.size()>0){
-
dispose();
-
FensterMatrix2 f=new FensterMatrix2("Wert eingeben",buttonSize,list,this);
-
}else{
-
}
-
}
-
}
-
-
if(event.getSource()==einlesen){
-
ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
-
for(int i=0;i<matrix.size();i++){//matrix.length=die höhe; matrix[0].length = breite
-
ArrayList<Integer> al=new ArrayList<Integer>();
-
for(int p=0;p<matrix.get(0).size();p++){
-
// System.out.println("ml"+matrixLocations.length);//ml.length=breite; ml[0].lenth=höhe
-
// System.out.println("ml[0]"+matrixLocations[0].length);
-
// System.out.println("i: "+i+" p: "+p);
-
al.add(matrix.get(i).get(p));
-
}
-
}
-
if(al.size()>0){
-
list.add(al);
-
}
-
}
-
if(list.size()>0){
-
dispose();
-
FensterMatrix2 f=new FensterMatrix2("Wert eingeben",buttonSize,list,this);
-
}else{
-
}
-
}
-
-
else if(event.getSource()==fertig){
-
dispose();
-
}
-
}
-
-
}
FensterMatrix2
Java Code:
-
-
-
package fenster;
-
-
-
import java.awt.Color;
-
import java.awt.Point;
-
import java.awt.event.ActionEvent;
-
import java.awt.event.KeyEvent;
-
import java.awt.event.KeyListener;
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.concurrent.TimeUnit;
-
import javax.swing.*;
-
import java.awt.*;
-
import jdk.jfr.events.ExceptionThrownEvent;
-
-
public class FensterMatrix2 extends Fenster{
-
-
FensterMatrix1 gehoertZu;
-
int buttonSize;
-
ArrayList<ArrayList<Integer>> matrix;
-
-
-
JLabel derText;
-
JTextField textField;
-
-
-
-
-
public FensterMatrix2(String text, int buttonSize, ArrayList<ArrayList<Integer>> matrix, FensterMatrix1 gehoertZu){
-
this.gehoertZu=gehoertZu;
-
// System.out.println(matrix.get(0).size()+"matrixSize!!!!!!!!!");
-
this.buttonSize=buttonSize;
-
setLayout(null);
-
this.matrix=matrix;
-
setVisible(true);
-
// setSize(buttonSize*matrix.size()+200,buttonSize*matrix.get(0).size()+100);
-
// setSize(buttonSize*matrix.get(0).size()+200,buttonSize*matrix.size()+100);
-
setSize(400,400);
-
setTitle("");
-
setLocationRelativeTo(null);
-
setResizable(false);
-
-
-
-
-
-
-
// matrixLocations=new Point[matrix.size()][matrix.get(0).size()];
-
for(int i=0;i<matrix.size();i++){
-
for(int p=0;p<matrix.get(0).size();p++){
-
if(matrix.get(i).get(p)==1)
-
else
-
button.setBounds(buttonSize*(p+1), buttonSize*(i+1), buttonSize, buttonSize);
-
// button.setBounds(buttonSize*(i+1), buttonSize*(p+1), buttonSize, buttonSize);
-
button.setRolloverEnabled(false);
-
button.setBorderPainted(false);
-
// button.setBorder(null);
-
add(button);
-
matrixLocations[p][i]=button.getLocation();
-
// matrixLocations[i][p]=button.getLocation();
-
}
-
}
-
-
-
derText.setBounds(200, 100, 100, 20);
-
add(derText);
-
textField.setBounds(200, 150, 100, 20);
-
textField.addActionListener(this);
-
add(textField);
-
ok.setBounds(200, 200, 100, 20);
-
abbruch.setBounds(200, 250, 100, 20);
-
ok.addActionListener(this);
-
abbruch.addActionListener(this);
-
add(ok);
-
add(abbruch);
-
}
-
-
-
-
int count1=0;int count2=0;int count3=0;int count4=0;
-
-
if(event.getSource()==abbruch){
-
dispose();
-
}else if(event.getSource()==ok){//matrix + string speichern;
-
if(textField.getText().length()>0){
-
Objekt.add(g);
-
matrixSpeicher.add(matrix);
-
dispose();
-
FensterMatrix1 f=new FensterMatrix1(gehoertZu.matrix, buttonSize);
-
}else{
-
}
-
}
-
}
-
-
}
also mein Problem ist folgendes:
Ich starte das Programm über Fenster. Dann öffnet sich FensterMatrix1 und ich kann, so wie ich das auch alles will, über den KeyListener 4 balken bewegen mit denen ich meine Auswahl treffe. Dann klicke ich "einlesen", MatrixFenster2 öffnet sich und MatrixFenster1.1 schließt sich. Nach meiner Eingabe+button"ok" in MF2 schließt sich MF2 und MF1.2 öffnet sich. Soweit so gut, aber jetzt geht der KeyListener in MF1 nicht mehr.
Ich habe ja schon eine syso gemacht und es scheint tatsächlich so, dass der KeyListener einfach nicht mehr da ist. Es gibt bei MF1.2 keine syso mehr, egal welche taste ich drücke, bei MF1.1 hingegen wird bei jedem Tastenanschlag eine syso gemacht.
Also woran kann das liegen, dass der KeyListener bei MF1.2 nicht mehr funktioniert?
Nooil
KeyListener funktioniert nicht richitg
Categories: KeyListener funktioniert nicht richitg
Inscription à :
Publier les commentaires (Atom)
0 commentaires:
Enregistrer un commentaire