# Funktion ausführen wenn Button gedrückt wird.



## HansGoldhahn (19. Apr 2010)

Hallo, habe ein ich nenn es mal "Anfänger-Problem" ;(

Ich habe eine funktion die Ausgeführt werden soll, wenn ein Button gedrückt wird. 

Butten erstellen - is da
Funktion erstellen - is da
Funktion wird momentan von einem TestListener(this) ausgeführt

wie kann ich das ganze ummstricken das es von einem buttonklick ausgeführt wird.


[Java]
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.applet.*;

public class Text_io extends Panel implements Runnable, TextListener {
private tcpip gtp;
	String oldmessage = new String("");
	TextArea input_box  = new TextArea("", 3, 60, 3);
  	TextArea eingabe_box  = new TextArea("", 5, 60, 3);
	Label ausgabe = new Label("- - - - - - -ausgabe 1 - - - - - - - -");
  	TextArea output_box = new TextArea("", 10, 60, 3);
	Button taste_0 = new Button ("0");
	Thread timer;
	String eingabb = new String("Das ist nur ein Versuch --- Hallo");
  	public Text_io(tcpip tp) {
		gtp = tp;

		setLayout(new GridBagLayout());
    		GridBagConstraints c = new GridBagConstraints();
		c.insets =  new Insets(5,5,5,5);
		setBackground(java.awt.Color.lightGray);
		setSize(561,380);

		c.gridx = 0; c.gridy = 2; c.gridwidth = 1; c.gridheight = 1;
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		add((new Label("Zum ADA-C senden:")), c);

		//input_box
		input_box.addTextListener(this);
		c.gridx = 1; c.gridy = 2; c.gridwidth = 3; c.gridheight = 1;
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(input_box,c);

		// NEU
		//eingabe_box
		eingabe_box.addKeyListener(new KeyAdapter() {
    		public void keyTyped(KeyEvent e) {
      		char c = e.getKeyChar();      
      		if (!((Character.isDigit(c) ||
         		(c == KeyEvent.VK_BACK_SPACE) ||
         		(c == KeyEvent.VK_DELETE) ||
			(c == KeyEvent.VK_ENTER)))) {
           	// getToolkit().beep();
           	e.consume();
      		}
    		}
  		});


		// weiter

		c.gridx = 1; c.gridy = 3; c.gridwidth = 3; c.gridheight = 1;
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(eingabe_box,c);	
		add(taste_0);
		//taste_0.addActionListener(this);

		c.gridx = 0; c.gridy = 4; c.gridwidth = 1; c.gridheight = 1;
		c.weightx = 0.0; c.weighty = 0.0; c.anchor = GridBagConstraints.WEST;
		c.fill = GridBagConstraints.NONE;
		add((new Label("Empfangen:")), c);

		c.gridx = 1; c.gridy = 4; c.gridwidth = 3; c.gridheight = 1;
		c.weightx = 0.5; c.weighty = 0.0; c.anchor = GridBagConstraints.CENTER;
		c.fill = GridBagConstraints.BOTH;
		add(output_box,c);
		output_box.setEditable(false);
		timer = new Thread(this);
		timer.start();
	}
		//NEU




//weiter
	public void run() {
		int i;
		byte[] in;
		Thread me = Thread.currentThread();
		while (timer == me) {
			try {
				Thread.currentThread().sleep(200);
			}
                        catch (InterruptedException e) { }
	  		if ( (gtp != null) && ((i = gtp.available()) > 0) ) {
				in = gtp.receive();
						/* remove non-printing bytes */
				for (i = 0; i < in.length; i++) {
					if (in_ < 0x20)
						in = 0x20;
				}
				output_box.append((new String(in)));
			}
		}
	}

    	public void textValueChanged(TextEvent e) {
		int len, i;
		String str = new String("");
		String message = eingabe_box.getText();
		len = message.length(); // - oldmessage.length();
		if (len < 0) {
			for (i = 0; i < -len; i++)
				str += "\b";
			// System.out.println("Backspace");
		}
		else if (len > 0) {
			str = message.substring(oldmessage.length());
			// System.out.println("len = "+str.length()+" str = "+str);
		}
		oldmessage = "";
		if ( (len != 0) && (gtp != null) )
			gtp.send(str);
    	}
}
[/code]_


----------



## fastjack (19. Apr 2010)

sieh mal hier unter actionPerformed

How to Use Buttons, Check Boxes, and Radio Buttons (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Swing Components)


----------



## HansGoldhahn (19. Apr 2010)

Danke, hat sich erledigt, funktioniert.


----------

