vendredi 22 mai 2015

KeyListener funktioniert nicht richitg

Hallihallo,
also erstmal der code

Fenster
Java Code:

  1. package fenster;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Point;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.util.ArrayList;
  10.  
  11.  
  12. import javax.swing.JButton;
  13. import javax.swing.JFrame;
  14.  
  15.  
  16. import com.sun.javafx.scene.control.skin.ButtonSkin;
  17.  
  18.  
  19. public class Fenster extends JFrame implements KeyListener, ActionListener{//mit buttons bewegen
  20.  
  21.  
  22. private static final long serialVersionUID = 1L;
  23. static ArrayList<ArrayList<Integer>> m=new ArrayList<ArrayList<Integer>>();
  24. public static ArrayList<String> Objekt=new ArrayList<String>();//objektspeicher aus fenstermatrix2
  25. public static ArrayList<ArrayList<ArrayList<Integer>>> matrixSpeicher=new ArrayList<ArrayList<ArrayList<Integer>>>();
  26.  
  27. public void keyPressed(KeyEvent key) {
  28.  
  29. }
  30. public void keyReleased(KeyEvent key) {
  31.  
  32. }
  33. public void keyTyped(KeyEvent key){
  34.  
  35. }
  36. public void actionPerformed(ActionEvent key) {
  37.  
  38. }
  39.  
  40.  
  41.  
  42. public Fenster(){
  43.  
  44. }
  45.  
  46. public static void main(String[] args) throws InterruptedException{
  47. // Fenster f=new FensterScreenshot(Color.BLACK);
  48. 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);
  49. 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);
  50. 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);
  51. Fenster matrixFenster=new FensterMatrix1(m, 50);
  52. Fenster matrixFenster2=new FensterMatrix1(m, 50);
  53. }
  54.  
  55.  
  56.  
  57. }


FensterMatrix1
Java Code:

  1. package fenster;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Point;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.event.KeyListener;
  8. import java.util.ArrayList;
  9. import java.util.Arrays;
  10. import java.util.concurrent.TimeUnit;
  11.  
  12.  
  13. import javax.swing.*;
  14. import java.awt.*;
  15.  
  16.  
  17. import jdk.jfr.events.ExceptionThrownEvent;
  18.  
  19.  
  20. public class FensterMatrix1 extends Fenster {
  21. int buttonSize;
  22. JButton b1,b2,b3,b4;
  23. ArrayList<ArrayList<Integer>> matrix;
  24. Point[][] matrixLocations;
  25. JButton einlesen=new JButton("einlesen");
  26. JButton fertig=new JButton("fertig");
  27.  
  28. public FensterMatrix1(ArrayList<ArrayList<Integer>> matrix, int buttonSize){// mit 1-4 striche bewegen --> alle kästchen werden aufgedeckt
  29.  
  30.  
  31. setLayout(null);
  32. // setUndecorated(true);//vor setVisible
  33. // opacity=0.5f;
  34. // setOpacity(opacity);
  35. // ArrayList<ArrayList<Integer>> matrix;//matrix elemente alle gleich lang
  36.  
  37.  
  38. this.buttonSize=buttonSize;
  39. this.matrix=matrix;
  40. setVisible(true);
  41. setSize(buttonSize*matrix.get(0).size()+200,buttonSize*matrix.size()+100);
  42. setTitle("nicht erkannt");
  43. setLocationRelativeTo(null);
  44. setResizable(false);
  45. this.getContentPane().setBackground(Color.LIGHT_GRAY);
  46. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  47.  
  48.  
  49.  
  50.  
  51. b1=new JButton();//vier striche
  52. b2=new JButton();
  53. b3=new JButton();
  54. b4=new JButton();
  55. b1.setBackground(Color.CYAN);
  56. b2.setBackground(Color.CYAN);
  57. b3.setBackground(Color.CYAN);
  58. b4.setBackground(Color.CYAN);
  59. b1.setBorderPainted(false);
  60. b2.setBorderPainted(false);
  61. b3.setBorderPainted(false);
  62. b4.setBorderPainted(false);
  63. b1.setBounds(0, buttonSize, this.getWidth(), 1);
  64. b2.setBounds(0, buttonSize, this.getWidth(), 1);
  65. b3.setBounds(buttonSize, 0, 1, this.getHeight());
  66. b4.setBounds(buttonSize, 0, 1, this.getHeight());
  67. add(b1);
  68. add(b2);
  69. add(b3);
  70. add(b4);
  71.  
  72.  
  73.  
  74. matrixLocations=new Point[matrix.get(0).size()][matrix.size()];
  75. for(int i=0;i<matrix.size();i++){
  76. for(int p=0;p<matrix.get(0).size();p++){
  77. JButton button=new JButton();
  78. if(matrix.get(i).get(p)==1)
  79. button.setBackground(Color.WHITE);
  80. else
  81. button.setBackground(Color.BLACK);
  82. button.setBounds(buttonSize*(p+1), buttonSize*(i+1), buttonSize, buttonSize);
  83. button.setRolloverEnabled(false);
  84. button.setBorderPainted(false);
  85. // button.setBorder(null);
  86. add(button);
  87. matrixLocations[p][i]=button.getLocation();
  88. }
  89. }
  90.  
  91. einlesen=new JButton("einlesen");
  92. fertig=new JButton("fertig");
  93. einlesen.setBounds(this.getWidth()-130, this.getHeight()-100, 100, 20);
  94. fertig.setBounds(this.getWidth()-130, this.getHeight()-70, 100, 20);
  95. add(einlesen);
  96. add(fertig);
  97.  
  98.  
  99.  
  100. // Listener listener=new Listener();
  101. einlesen.addActionListener(this);
  102. fertig.addActionListener(this);
  103. addKeyListener(this);
  104. }
  105.  
  106. int count1=0;int count2=0;int count3=0;int count4=0;
  107. public void keyTyped(KeyEvent key){
  108. int length=matrixLocations[0].length-1;int width=matrixLocations.length-1;int size=matrixLocations[0][1].y-matrixLocations[0][0].y;
  109. System.out.println(key.toString());
  110. if(key.getKeyChar()=='1'){
  111. if(count1<length+1){
  112. count1++;
  113. b1.setLocation(b1.getX(), matrixLocations[0][length].y+(count1-length)*size);
  114. }
  115. }else if(key.getKeyChar()=='!'){
  116. if(count1>0){
  117. count1--;
  118. b1.setLocation(b1.getX(), matrixLocations[0][length].y+(count1-length)*size);
  119. }
  120. }else if(key.getKeyChar()=='2'){
  121. if(count2<length+1){
  122. count2++;
  123. b2.setLocation(b2.getX(), matrixLocations[0][length].y+(count2-length)*size);
  124. }
  125. }else if(key.getKeyChar()=='"'){
  126. if(count2>0){
  127. count2--;
  128. b2.setLocation(b2.getX(), matrixLocations[0][length].y+(count2-length)*size);
  129. }
  130. }else if(key.getKeyChar()=='3'){
  131. if(count3<width+1){
  132. count3++;
  133. b3.setLocation(matrixLocations[width][0].x+(count3-width)*size,b3.getY() );
  134. }
  135. }else if(key.getKeyChar()=='§'){
  136. if(count3>0){
  137. count3--;
  138. b3.setLocation(matrixLocations[width][0].x+(count3-width)*size,b3.getY() );
  139. }
  140. }else if(key.getKeyChar()=='4'){
  141. if(count4<width+1){
  142. count4++;
  143. b4.setLocation(matrixLocations[width][0].x+(count4-width)*size,b4.getY() );
  144. }
  145. }else if(key.getKeyChar()=='$'){
  146. if(count4>0){
  147. count4--;
  148. b4.setLocation(matrixLocations[width][0].x+(count4-width)*size,b4.getY() );
  149. }
  150. }else if(key.getKeyChar()=='\n'){
  151. System.out.println("eingabe");
  152. ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
  153. for(int i=0;i<matrix.size();i++){//matrix.length=die höhe; matrix[0].length = breite
  154. ArrayList<Integer> al=new ArrayList<Integer>();
  155. for(int p=0;p<matrix.get(0).size();p++){
  156. // System.out.println("ml"+matrixLocations.length);//ml.length=breite; ml[0].lenth=höhe
  157. // System.out.println("ml[0]"+matrixLocations[0].length);
  158. // System.out.println("i: "+i+" p: "+p);
  159. Point point=matrixLocations[p][i];
  160. 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())){
  161. al.add(matrix.get(i).get(p));
  162. }
  163. }
  164. if(al.size()>0){
  165. list.add(al);
  166. }
  167. }
  168. if(list.size()>0){
  169. dispose();
  170. FensterMatrix2 f=new FensterMatrix2("Wert eingeben",buttonSize,list,this);
  171. System.out.println("öffne einlesen");
  172. }else{
  173. System.out.println("auswahl treffen");
  174. }
  175. }
  176. }
  177.  
  178. public void actionPerformed(ActionEvent event) {
  179. if(event.getSource()==einlesen){
  180. ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
  181. for(int i=0;i<matrix.size();i++){//matrix.length=die höhe; matrix[0].length = breite
  182. ArrayList<Integer> al=new ArrayList<Integer>();
  183. for(int p=0;p<matrix.get(0).size();p++){
  184. // System.out.println("ml"+matrixLocations.length);//ml.length=breite; ml[0].lenth=höhe
  185. // System.out.println("ml[0]"+matrixLocations[0].length);
  186. // System.out.println("i: "+i+" p: "+p);
  187. Point point=matrixLocations[p][i];
  188. 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())){
  189. al.add(matrix.get(i).get(p));
  190. }
  191. }
  192. if(al.size()>0){
  193. list.add(al);
  194. }
  195. }
  196. if(list.size()>0){
  197. dispose();
  198. FensterMatrix2 f=new FensterMatrix2("Wert eingeben",buttonSize,list,this);
  199. System.out.println("öffne einlesen");
  200. }else{
  201. System.out.println("auswahl treffen");
  202. }
  203. }
  204.  
  205. else if(event.getSource()==fertig){
  206. System.out.println("schließen");
  207. dispose();
  208. }
  209. }
  210.  
  211. }


FensterMatrix2
Java Code:

  1.  
  2.  
  3. package fenster;
  4.  
  5.  
  6. import java.awt.Color;
  7. import java.awt.Point;
  8. import java.awt.event.ActionEvent;
  9. import java.awt.event.KeyEvent;
  10. import java.awt.event.KeyListener;
  11. import java.util.ArrayList;
  12. import java.util.Arrays;
  13. import java.util.concurrent.TimeUnit;
  14. import javax.swing.*;
  15. import java.awt.*;
  16. import jdk.jfr.events.ExceptionThrownEvent;
  17.  
  18. public class FensterMatrix2 extends Fenster{
  19.  
  20. FensterMatrix1 gehoertZu;
  21. int buttonSize;
  22. ArrayList<ArrayList<Integer>> matrix;
  23. Point[][] matrixLocations;
  24. JButton ok=new JButton("ok");
  25. JButton abbruch=new JButton("abbruch");
  26.  
  27.  
  28. JLabel derText;
  29. JTextField textField;
  30.  
  31.  
  32.  
  33.  
  34. public FensterMatrix2(String text, int buttonSize, ArrayList<ArrayList<Integer>> matrix, FensterMatrix1 gehoertZu){
  35. this.gehoertZu=gehoertZu;
  36. // System.out.println(matrix.get(0).size()+"matrixSize!!!!!!!!!");
  37. this.buttonSize=buttonSize;
  38. setLayout(null);
  39. this.matrix=matrix;
  40. setVisible(true);
  41. // setSize(buttonSize*matrix.size()+200,buttonSize*matrix.get(0).size()+100);
  42. // setSize(buttonSize*matrix.get(0).size()+200,buttonSize*matrix.size()+100);
  43. setSize(400,400);
  44. setTitle("");
  45. setLocationRelativeTo(null);
  46. setResizable(false);
  47. this.getContentPane().setBackground(Color.LIGHT_GRAY);
  48. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55. matrixLocations=new Point[matrix.get(0).size()][matrix.size()];
  56. // matrixLocations=new Point[matrix.size()][matrix.get(0).size()];
  57. for(int i=0;i<matrix.size();i++){
  58. for(int p=0;p<matrix.get(0).size();p++){
  59. JButton button=new JButton();
  60. if(matrix.get(i).get(p)==1)
  61. button.setBackground(Color.WHITE);
  62. else
  63. button.setBackground(Color.BLACK);
  64. button.setBounds(buttonSize*(p+1), buttonSize*(i+1), buttonSize, buttonSize);
  65. // button.setBounds(buttonSize*(i+1), buttonSize*(p+1), buttonSize, buttonSize);
  66. button.setRolloverEnabled(false);
  67. button.setBorderPainted(false);
  68. // button.setBorder(null);
  69. add(button);
  70. matrixLocations[p][i]=button.getLocation();
  71. // matrixLocations[i][p]=button.getLocation();
  72. }
  73. }
  74.  
  75.  
  76. derText=new JLabel(text);
  77. derText.setBounds(200, 100, 100, 20);
  78. add(derText);
  79. textField=new JTextField(20);
  80. textField.setBounds(200, 150, 100, 20);
  81. textField.addActionListener(this);
  82. add(textField);
  83. ok.setBounds(200, 200, 100, 20);
  84. abbruch.setBounds(200, 250, 100, 20);
  85. ok.addActionListener(this);
  86. abbruch.addActionListener(this);
  87. add(ok);
  88. add(abbruch);
  89. }
  90.  
  91.  
  92.  
  93. int count1=0;int count2=0;int count3=0;int count4=0;
  94.  
  95. public void actionPerformed(ActionEvent event) {
  96. if(event.getSource()==abbruch){
  97. dispose();
  98. }else if(event.getSource()==ok){//matrix + string speichern;
  99. if(textField.getText().length()>0){
  100. String g=textField.getText();
  101. System.out.println(g);
  102. Objekt.add(g);
  103. matrixSpeicher.add(matrix);
  104. dispose();
  105. FensterMatrix1 f=new FensterMatrix1(gehoertZu.matrix, buttonSize);
  106. }else{
  107. System.out.println("eingabe tätigen");
  108. }
  109. }
  110. }
  111.  
  112. }


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

0 commentaires:

Enregistrer un commentaire