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:
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;
public class Fenster extends JFrame implements KeyListener, ActionListener{//mit buttons bewegen
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 void keyPressed(KeyEvent key) {
}
public void keyReleased(KeyEvent key) {
}
public void keyTyped(KeyEvent key){
}
public void actionPerformed(ActionEvent key) {
}
public Fenster(){
}
public static void main(String[] args) throws InterruptedException{
// 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:
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;
Point[][] matrixLocations;
JButton einlesen=new JButton("einlesen");
JButton fertig=new JButton("fertig");
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);
this.getContentPane().setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b1=new JButton();//vier striche
b2=new JButton();
b3=new JButton();
b4=new JButton();
b1.setBackground(Color.CYAN);
b2.setBackground(Color.CYAN);
b3.setBackground(Color.CYAN);
b4.setBackground(Color.CYAN);
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);
matrixLocations=new Point[matrix.get(0).size()][matrix.size()];
for(int i=0;i<matrix.size();i++){
for(int p=0;p<matrix.get(0).size();p++){
JButton button=new JButton();
if(matrix.get(i).get(p)==1)
button.setBackground(Color.WHITE);
else
button.setBackground(Color.BLACK);
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=new JButton("einlesen");
fertig=new JButton("fertig");
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;
public void keyTyped(KeyEvent key){
int length=matrixLocations[0].length-1;int width=matrixLocations.length-1;int size=matrixLocations[0][1].y-matrixLocations[0][0].y;
System.out.println(key.toString());
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'){
System.out.println("eingabe");
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);
Point point=matrixLocations[p][i];
if(point.x>=Math.min(b3.getX(), b4.getX())&&point.x<Math.max(b3.getX(), b4.getX())&&point.y>=Math.min(b1.getY(), b2.getY())&&point.y<Math.max(b1.getY(), b2.getY())){
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);
System.out.println("öffne einlesen");
}else{
System.out.println("auswahl treffen");
}
}
}
public void actionPerformed(ActionEvent event) {
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);
Point point=matrixLocations[p][i];
if(point.x>=Math.min(b3.getX(), b4.getX())&&point.x<Math.max(b3.getX(), b4.getX())&&point.y>=Math.min(b1.getY(), b2.getY())&&point.y<Math.max(b1.getY(), b2.getY())){
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);
System.out.println("öffne einlesen");
}else{
System.out.println("auswahl treffen");
}
}
else if(event.getSource()==fertig){
System.out.println("schließen");
dispose();
}
}
}
FensterMatrix2
Java:
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;
Point[][] matrixLocations;
JButton ok=new JButton("ok");
JButton abbruch=new JButton("abbruch");
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);
this.getContentPane().setBackground(Color.LIGHT_GRAY);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
matrixLocations=new Point[matrix.get(0).size()][matrix.size()];
// 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++){
JButton button=new JButton();
if(matrix.get(i).get(p)==1)
button.setBackground(Color.WHITE);
else
button.setBackground(Color.BLACK);
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=new JLabel(text);
derText.setBounds(200, 100, 100, 20);
add(derText);
textField=new JTextField(20);
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;
public void actionPerformed(ActionEvent event) {
if(event.getSource()==abbruch){
dispose();
}else if(event.getSource()==ok){//matrix + string speichern;
if(textField.getText().length()>0){
String g=textField.getText();
System.out.println(g);
Objekt.add(g);
matrixSpeicher.add(matrix);
dispose();
FensterMatrix1 f=new FensterMatrix1(gehoertZu.matrix, buttonSize);
}else{
System.out.println("eingabe tätigen");
}
}
}
}
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