Veröffentlicht 22. Mai 200223 j Hi! Bin gerade mal wieder dabei mich in Java einzuarbeiten... Im Moment: AWT und Events. Wie kann ich über einen Button ein TextArea leeren? MfG backdraft
22. Mai 200223 j Hallo, javax.swing.JTextArea area = new JTextArea(); javax.swing.JButton button = new JButton(); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { area.setText(""); } });[/PHP] Gruß Jaraz
22. Mai 200223 j Dann lag ich ja doch gar nicht so falsch... Hab das so: btn_leeren.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { txta_Protokoll.setText(""); } });[/PHP] Da kommt aber eine Fehlermeldung: E:\Java\jcreator\Server\Server.java:48: local variable txta_Protokoll is accessed from within inner class; needs to be declared final´ txta_Protokoll.setText(""); MfG backdraft
22. Mai 200223 j Sorry, du musst JTextArea als Klassenvariable definieren. Bei meinem Beispiel sieht es so aus als wenn JTextArea direkt vor der inner Class initialisiert wird. Gruß Jaraz
22. Mai 200223 j public Server() { Panel Panel1 = new Panel(); Panel1.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0)); Label lbl_Protokoll = new Label("Protokoll:"); lbl_Protokoll.setBackground(SystemColor.menu); Panel1.add(lbl_Protokoll); Panel Panel2 = new Panel(); Panel2.setLayout(new GridLayout(1, 2)); Button btn_leeren = new Button("Protokoll leeren"); Button btn_beenden = new Button("Server beenden"); Panel2.add(btn_leeren); Panel2.add(btn_beenden); TextArea txta_Protokoll = new TextArea("", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY); setLayout(new BorderLayout()); add("North", Panel1); add("Center", txta_Protokoll); add("South", Panel2); ActionListener al_Button_beenden = new ActionListener() { public void actionPerformed( ActionEvent e ) { System.exit(0); } }; btn_beenden.addActionListener(al_Button_beenden); btn_leeren.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { txta_Protokoll.setText(""); } }); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { dispose(); System.exit(0); } }); }[/PHP] txta_Protokoll ist das TextArea. MfG backdraft
23. Mai 200223 j Original geschrieben von backdraft Hat keiner ne Idee, was mir die Fehlermeldung sagen will? :confused: Wenn du mal das ausführen würdest, was ich dir geraten habe... ----------------------------- public TextArea txta_Protokoll; public Server() { . . . txta_Protokoll = new TextArea(.... Gruß Jaraz
29. Mai 200223 j Ich habe mal wieder ein Problem: E:\Java\jcreator\Server\Server.java:70: non-static variable txta_Protokoll cannot be referenced from a static context txta_Protokoll.setText(txta_Protokoll.getText() + "\n Warte auf Client-Anfrage..."); :confused: :confused: :confused: MfG backdraft
29. Mai 200223 j Definiere mal die Variable "static"! Also "static TextArea txta_Protokoll = new TextArea("", 10, 40, TextArea.SCROLLBARS_VERTICAL_ONLY);". Wenn du das nicht willst, mußt du deine Variable "txta_Protokoll" in deiner static-Methode definieren in der du sie benötigst. cu Peeter
Archiv
Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.