All pastes #793411 Raw Edit

Heidi

public text v1 · immutable
·published 2007-11-22 06:59 UTC
rendered paste body
package package05;


import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.ComponentOrientation;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Fallstudie extends Applet implements ActionListener, Runnable {

	Thread t;
	
	TextField tfPreis, tfGewicht;
	
	private final Sorte[] sorte = {	
			new Sorte("Erdbeeren", 12), new Sorte("Pfirsiche", 8),
			new Sorte("Bohnen", 6), new Sorte("Zitronen", 16),
			new Sorte("Schaschlik", 48), new Sorte("Melonen", 20) 
	};
	
	public void init()
	{
		t = new Thread(this);
		setLayout(new BorderLayout());
		Panel firstPanel = new Panel(new FlowLayout());
		Panel lastPanel = new Panel(new FlowLayout());
		
		tfPreis = new TextField(12);
		tfPreis.setEditable(false);
		firstPanel.add(new Label("Preis"));
		firstPanel.add(tfPreis);
		
		Panel mainPanel = new Panel(new GridLayout(3,2));
		mainPanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		
		for(int i=0;i<sorte.length;i++)
			mainPanel.add(setButton(sorte[i]));
		
		tfGewicht = new TextField(12);
		lastPanel.add(new Label("Gewicht"));
		lastPanel.add(tfGewicht);
		
		this.add(BorderLayout.NORTH, firstPanel);
		this.add(BorderLayout.CENTER, mainPanel);
		this.add(BorderLayout.SOUTH, lastPanel);
		
		setVisible(true);
	}
	
	public Button setButton(Sorte s)
	{
		String buttonText = s.getName();
		Button b = new Button(buttonText);
		b.setActionCommand(buttonText);
		b.addActionListener(this);
		return b;
	}
	
	public void start()
	{
		t.run();
	}
	
	public void run()
	{
		
	}
	
	public void actionPerformed(ActionEvent ae) 
	{
		String actionCommand = ae.getActionCommand();
		for(int i=0;i<sorte.length;i++)
			if(sorte[i].getName().equalsIgnoreCase(actionCommand))
			{
				try
				{
					tfPreis.setText((sorte[i].getQuotient()*Double.parseDouble(tfGewicht.getText().replace(",", ".")))+" €");
				}
				catch(NumberFormatException e)
				{
					tfPreis.setText("ERROR");
				}
			}
		
	}
}