![]() |
Problem mit Mausereignis (JAVA)
Guuuden zusammen,
ich habe das Problem, dass meine Testbuttons (bisher "exit" und "cancel") nicht auf das Mausereignis Pressed reagieren: Wir haben keine Ahnung, woran das liegen können, haben alles mögliche versucht. :wall: Hier ist der Quelltext:
Code:
/**
*@author Dannyboy, Mike T. */ import java.io.*; import java.awt.*; // für Fenster import java.awt.event.*; // Für Adapter public class Mainframe extends Frame { //******************************************************************************************// // KONSTRUKTOREN Mainframe(String title) { this.f = new Frame(); f.setLayout(null); f.setSize(clientWidth, clientHeight); f.setTitle(title); f.addWindowListener(new MyWindowAdapter()); setDesign(); myMouse = new MyMouseAdapter(); f.addMouseListener(myMouse); f.show(); } //******************************************************************************************// public Frame f = null; // ATTRIBUTE (KONSTANTEN) final private int clientWidth = 750; // Breite des Clientbereichs final private int clientHeight = 550; // Höhe des Clientbereichs final private int leftAlign = 20; // Position der linken Zentrierung final private int labelheight = 25; // Standardhöhe eines Labels final private int buttonheight = 30; // Standardhöhe eines Buttons final private int textfieldheight = buttonheight; // Standardhöhe eines textfeldes // ATTRIBUTE (VARIABLEN) // Textfelder public TextField stdIn = null; public TextField rCode = null; // Textareas public TextArea stdOut = null; public TextArea stdErr = null; // Labels public Label stdIn_Lab = null; public Label stdErr_Lab = null; public Label stdOut_Lab = null; public Label rCode_Lab = null; // Buttons public Button ok = null; public Button cancel = null; public Button snap = null; public Button exit = null; MyMouseAdapter myMouse = null; //******************************************************************************************// // Methode legt das komplette Design des Formulars fest. private void setDesign() { final int middleX = clientWidth / 2; createLabel(stdIn_Lab, leftAlign, 25, 90, labelheight, "<Eingabe>"); createTextField(stdIn, leftAlign, 50, 275, textfieldheight, ""); createButton(ok, leftAlign, 90, 85, buttonheight, "OK"); createButton(snap, 115, 90, 85, buttonheight, "SNAP-MENU"); createButton(cancel, 210, 90, 85, buttonheight, "CANCEL"); createLabel(stdErr_Lab, leftAlign, 130, 100, labelheight, "<Standard Error>"); createTextArea(stdErr, leftAlign, 160, 290, 295, "<Dies ist Standard Error>"); createLabel(rCode_Lab, leftAlign, 465, 90, labelheight, "<return code>"); createTextField(rCode, leftAlign, 495, 275, textfieldheight, ""); createLabel(stdOut_Lab, middleX, 25, 90, labelheight, "<Standard Out>"); createTextArea(stdOut, middleX, 50, middleX - 20, 430, "<Dies ist Standard Out>"); createButton(exit, middleX, 495, middleX - 20, buttonheight, "Close Window"); } //******************************************************************************************// // GETTER //******************************************************************************************// // SETTER //******************************************************************************************// // Methode erstellt ein Textfeld (einzeilige Komponente) private void createTextField(TextField instance, int x, int y, int width, int height, String s) { instance = new TextField(s); f.add(instance); // instance.setBackground(bg); instance.setBounds(x, y, width, height); instance.show(); } //******************************************************************************************// // Methode erstellt eine Textarea (mehrzeilige Komponente) private void createTextArea(TextArea instance, int x, int y, int width, int height, String s) { instance = new TextArea(s); f.add(instance); // instance.setBackground(bg); instance.setBounds(x, y, width, height); instance.show(); } //******************************************************************************************// // Methode erstellt ein Label private void createLabel(Label instance, int x, int y, int width, int height, String s) { instance = new Label(s); f.add(instance); instance.setBounds(x, y, width, height); instance.show(); } //******************************************************************************************// // Methode erstellt einen Button private void createButton(Button instance, int x, int y, int width, int height, String s) { instance = new Button(s); f.add(instance); instance.setBounds(x, y, width, height); instance.addMouseListener(myMouse); instance.show(); } //******************************************************************************************// //******************************************************************************************// //******************************************************************************************// // Unterklasse um Window-Events (OOP-Ereignisse) zu steuern public class MyWindowAdapter extends WindowAdapter { public void windowClosing (WindowEvent e) { System.exit(0); } } //******************************************************************************************// //******************************************************************************************// //******************************************************************************************// // Unterklasse um Mouse-Events (OOP-Ereignisse) zu steuern public class MyMouseAdapter extends MouseAdapter { public void mousePressed(MouseEvent e) { Object o = e.getSource(); if (o == ok) ok_Pressed(e); else if (o == cancel) cancel_Pressed(e); else if (o == snap) snap_Pressed(e); else if (o == exit) exit_Pressed(e); } } //******************************************************************************************// //******************************************************************************************// //******************************************************************************************// // Mouse/Button Events public void ok_Pressed(MouseEvent e){} public void cancel_Pressed(MouseEvent e){ stdIn.setText(""); } public void snap_Pressed(MouseEvent e){} public void exit_Pressed(MouseEvent e){ System.exit(0); } //******************************************************************************************// public static void main(String[] args) { Mainframe testframe = new Mainframe("JAVIX 2.0"); } } |
Re: Problem mit Mausereignis (JAVA)
|
Re: Problem mit Mausereignis (JAVA)
Ja, da hast Du vollkommen Recht. :oops:
@Admins: Löscht mal bitte diesen Threat. Ist ausversehen ein Doppelpost. :oops: |
Re: Problem mit Mausereignis (JAVA)
Zitat:
Zitat:
|
Re: Problem mit Mausereignis (JAVA)
Ha! Ich hab's jetzt doch geschafft. :mrgreen:
Zwei Fehler hast du gemacht: 1. Du hast setDesign() aufegrufen, bevor du myMouse instanziert hast, so dass alle Buttons mit null als Listener gespeist wurden. 2. Du weist den Feldern ok, cancel, etc. nichts zu. Ja, instance wird in den Funktionen erzeugt, aber das hat keine Auswirkung auf die Variable, die du übergibst. Warum, frag mich jetzt bitte nicht, ich habe es auch schon so gemacht wie du und das Stichwort Call-by-Reference sollte eigentlich auxch bedeuten, dass dein Ansatz funktioniert. Das tut es nur nicht. Keine Ahnung, warum (in meinem Zustand nicht). Wie auch immer, so geht's:
Code:
// KONSTRUKTOREN
Mainframe(String title) { this.f = new Frame(); f.setLayout(null); f.setSize(clientWidth, clientHeight); f.setTitle(title); f.addWindowListener(new MyWindowAdapter()); myMouse = new MyMouseAdapter(); f.addMouseListener(myMouse); setDesign(); f.show(); } //******************************************************************************************// // Methode legt das komplette Design des Formulars fest. private void setDesign() { final int middleX = clientWidth / 2; stdIn_Lab = createLabel(leftAlign, 25, 90, labelheight, "<Eingabe>"); stdIn = createTextField(leftAlign, 50, 275, textfieldheight, ""); ok = createButton(leftAlign, 90, 85, buttonheight, "OK"); snap = createButton(115, 90, 85, buttonheight, "SNAP-MENU"); cancel = createButton(210, 90, 85, buttonheight, "CANCEL"); stdErr_Lab = createLabel(leftAlign, 130, 100, labelheight, "<Standard Error>"); stdErr = createTextArea(leftAlign, 160, 290, 295, "<Dies ist Standard Error>"); rCode_Lab = createLabel(leftAlign, 465, 90, labelheight, "<return code>"); rCode = createTextField(leftAlign, 495, 275, textfieldheight, ""); stdOut_Lab = createLabel(middleX, 25, 90, labelheight, "<Standard Out>"); stdOut = createTextArea(middleX, 50, middleX - 20, 430, "<Dies ist Standard Out>"); exit = createButton(middleX, 495, middleX - 20, buttonheight, "Close Window"); } //******************************************************************************************// // Methode erstellt ein Textfeld (einzeilige Komponente) private TextField createTextField(int x, int y, int width, int height, String s) { TextField instance = new TextField(s); f.add(instance); // instance.setBackground(bg); instance.setBounds(x, y, width, height); instance.show(); return instance; } //******************************************************************************************// // Methode erstellt eine Textarea (mehrzeilige Komponente) private TextArea createTextArea(int x, int y, int width, int height, String s) { TextArea instance = new TextArea(s); f.add(instance); // instance.setBackground(bg); instance.setBounds(x, y, width, height); instance.show(); return instance; } //******************************************************************************************// // Methode erstellt ein Label private Label createLabel(int x, int y, int width, int height, String s) { Label instance = new Label(s); f.add(instance); instance.setBounds(x, y, width, height); instance.show(); return instance; } //******************************************************************************************// // Methode erstellt einen Button private Button createButton(int x, int y, int width, int height, String s) { Button instance = new Button(s); f.add(instance); instance.setBounds(x, y, width, height); instance.addMouseListener(myMouse); instance.show(); return instance; } |
Re: Problem mit Mausereignis (JAVA)
Fehler Numero Uno kann ich nachvollziehen, aber den 2. Fehler ... :gruebel: :gruebel: :gruebel:
Das verwirrt mich, denn ich dachte, innerhalb einer Klasse sind alle (!) Argumente CALL BY REFERENCE. Na ja gut, dann mach' ich aus den Voids eben Funktionen. Dank' Dir, Mann. :thuimb: |
Alle Zeitangaben in WEZ +1. Es ist jetzt 16:20 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz