Werte von Buttons in Applets

haldjo1

Erfahrenes Mitglied
Hi,
Wie kann ich den Wert eines Buttons auslesen? Hier ist ein beispiel:

Button Btrechneplus = new Button("+");

wenn jetzt dieser Button gedrückt wird will ich dass 2 Zahlen mit den variablen da, und db addiert werden. im selben Layout sind noch 3 andere Buttons für subtraktion, multiplikation und division.
 
Du must das ganze mit einem ActionEvent machen
hier ein Beispiel.
PHP:
final String test = "ein kleiner test";

System.out.println(test);
JButton plusButton = new JButton("Test");
plusButton.addActionListener(new ActionListener(){
	public void actionPerformed(ActionEvent e){
		test = "hallo";
		
		// mach was du machen willst wenn er den knopf drückt...
	}
});		
System.out.println(test);

hier wird nun ein String mit ... initialisiert
wenn er den knopf drückt wird die Variable umgeschrieben!
Achso achte darrauf das du die Variable final deklarierst :)

Es gibt natürlich auch noch die möglichkeit eine Klasse zu erstellen, dann musst du aber mit e.getActionCommand() die Button beschriftung abgleichen.
Da du aber mehrere hast empfehle ich dir diese Methode.
 
Zuletzt bearbeitet:
Servus!

Code:
/*
 * Created on 22.12.2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package pack;

/**
 * @author Administrator
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
/*
 * AWTTest7.java
 *
 * Created on 22. Dezember 2003, 22:30
 */
import java.awt.Button;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 *
 * @author  Administrator
 */
public class AWTTest7 extends java.awt.Frame {

	/** Creates new form AWTTest7 */
	public AWTTest7() {
		initComponents();

		MyActionListener mal = new MyActionListener();
		button1.addActionListener(mal);
		button2.addActionListener(mal);
		button3.addActionListener(mal);
		button4.addActionListener(mal);
		textField1.addActionListener(mal);
		textField2.addActionListener(mal);
		textField3.addActionListener(mal);
		textField4.addActionListener(mal);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	private void initComponents() {
		java.awt.GridBagConstraints gridBagConstraints;

		panel1 = new Panel();
		button1 = new Button();
		button2 = new Button();
		button3 = new Button();
		button4 = new Button();
		textField1 = new TextField();
		textField2 = new TextField();
		textField3 = new TextField();
		textField4 = new TextField();

		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent evt) {
				exitForm(evt);
			}
		});

		panel1.setLayout(new GridBagLayout());

		button1.setLabel("button1");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		panel1.add(button1, gridBagConstraints);

		button2.setLabel("button2");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 2;
		panel1.add(button2, gridBagConstraints);

		button3.setLabel("button3");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 4;
		panel1.add(button3, gridBagConstraints);

		button4.setLabel("button4");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 6;
		panel1.add(button4, gridBagConstraints);

		textField1.setText("textField1");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 0;
		panel1.add(textField1, gridBagConstraints);

		textField2.setText("textField2");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 2;
		panel1.add(textField2, gridBagConstraints);

		textField3.setText("textField3");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 4;
		panel1.add(textField3, gridBagConstraints);

		textField4.setText("textField4");
		gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 1;
		gridBagConstraints.gridy = 6;
		panel1.add(textField4, gridBagConstraints);

		add(panel1, java.awt.BorderLayout.CENTER);

		pack();
	}


	/** Exit the Application */
	private void exitForm(WindowEvent evt) {
		System.exit(0);
	}

	/**
	 * @param args the command line arguments
	 */
	public static void main(String args[]) {
		new AWTTest7().show();
	}

	class MyActionListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			Object src = event.getSource();
			if (src instanceof Button) {
				Button b = (Button) src;
				System.out.println(b.getLabel());
			} else if (src instanceof TextField) {
				TextField tf = (TextField) src;
				System.out.println(tf.getText());
			}
		}
	}

	// Variables declaration - do not modify
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Panel panel1;
	private TextField textField1;
	private TextField textField2;
	private TextField textField3;
	private TextField textField4;
	// End of variables declaration

}

Gruß Tom
 

Neue Beiträge

Zurück