import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class View extends JFrame implements ActionListener, Beobachter {
Modelinterface model;
Controllerinterface1 controller;
private JTextField name;
public View(Controllerinterface1 controller, Modelinterface model) {
this .model = model;
this.controller = controller;
model.registriereBeobachter((Beobachter)this);
}
void buildConstraints(GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy)
{
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}
public void initiate()
{
JFrame f = new JFrame();
f.setVisible(true);
f.setSize(800, 300);
GridBagConstraints constraints = new GridBagConstraints();
GridBagLayout grid = new GridBagLayout();
f.setLayout(grid);
//name label
buildConstraints(constraints, 2, 8, 1, 1, 80, 80);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JLabel nameLabel = new JLabel(" Name: ", JLabel.LEFT);
f.add( nameLabel, constraints);
//name Textfeld
buildConstraints(constraints, 3, 8, 1, 1, 20, 20);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.WEST;
name = new JTextField(20);
name.setEditable(false);
f.add( name, constraints);
// button
buildConstraints(constraints, 2, 9, 1, 1, 19, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JButton button1 = new JButton("Hole Daten");
button1.setActionCommand("actionbutton1");
button1.addActionListener(this);
f.add( button1, constraints);
// button
buildConstraints(constraints, 3, 9, 1, 1, 19, 40);
constraints.fill = GridBagConstraints.NONE;
constraints.anchor = GridBagConstraints.EAST;
JButton button2 = new JButton("Setze Daten");
button2.setActionCommand("actionbutton2");
button2.addActionListener(this);
f.add( button2, constraints);
}
public void aktualisiere()
{
String wert = model.getDaten();
if (wert == "")
name.setText("hier kommt nichts an.");
else
name.setText(wert);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("actionbutton1")) {
System.out.println("button 1 geklickt!");
String wert = name.getText();
controller.setDaten(wert);
}
if (e.getActionCommand().equals("actionbutton2")) {
System.out.println("button 1 geklickt!");
String wert = controller.getDaten();
name.setText(wert);
}
}//end actionperformend
}