mirror of
https://github.com/2martens/uni.git
synced 2026-05-07 03:46:25 +02:00
ID-5: Aufgabe 2 bearbeitet
This commit is contained in:
157
id/blatt5/code/mvc/UserInterface.java
Normal file
157
id/blatt5/code/mvc/UserInterface.java
Normal file
@ -0,0 +1,157 @@
|
||||
import java.awt.GridLayout;
|
||||
import java.awt.BorderLayout;
|
||||
import javax.swing.BoxLayout;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.WindowConstants;
|
||||
import java.util.Observer;
|
||||
import java.util.Observable;
|
||||
|
||||
class UserInterface implements Observer {
|
||||
public static final String NAME = "Temperature converter";
|
||||
|
||||
private Controller _controller;
|
||||
|
||||
private JFrame __frame;
|
||||
private JPanel __panel;
|
||||
private JTextField __celsius;
|
||||
private JTextField __fahrenheit;
|
||||
private JSlider __celsiusSlider;
|
||||
private JSlider __fahrenheitSlider;
|
||||
private JButton __convertCelsiusButton;
|
||||
private JButton __convertFahrenheitButton;
|
||||
|
||||
private GridLayout __layout;
|
||||
|
||||
private static final int CELSIUS_MIN = -18;
|
||||
private static final int CELSIUS_MAX = 100;
|
||||
private static final int CELSIUS_INIT = 0;
|
||||
|
||||
private static final int FAHRENHEIT_MIN = 0;
|
||||
private static final int FAHRENHEIT_MAX = 212;
|
||||
private static final int FAHRENHEIT_INIT = 32;
|
||||
|
||||
public UserInterface(Controller controller) {
|
||||
_controller = controller;
|
||||
__frame = new JFrame();
|
||||
__frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
__frame.setTitle(NAME);
|
||||
}
|
||||
|
||||
public void update(Observable o, Object arg) {
|
||||
if (o instanceof Temperature) {
|
||||
Temperature temperature = (Temperature) o;
|
||||
int celsius = temperature.getCelsiusTemperature();
|
||||
int fahrenheit = temperature.getFahrenheitTemperature();
|
||||
__celsius.setText(Integer.toString(celsius));
|
||||
__celsiusSlider.setValue(celsius);
|
||||
__fahrenheit.setText(Integer.toString(fahrenheit));
|
||||
__fahrenheitSlider.setValue(fahrenheit);
|
||||
}
|
||||
}
|
||||
|
||||
public void initGUI() {
|
||||
__panel = new JPanel();
|
||||
__layout = new GridLayout(1, 3);
|
||||
__panel.setLayout(__layout);
|
||||
|
||||
JPanel celsiusPanel = new JPanel();
|
||||
BoxLayout boxLayoutCelsius = new BoxLayout(celsiusPanel, BoxLayout.Y_AXIS);
|
||||
celsiusPanel.setLayout(boxLayoutCelsius);
|
||||
|
||||
__celsius = new JTextField();
|
||||
__celsius.setText(Integer.toString(CELSIUS_INIT));
|
||||
__celsiusSlider = new JSlider(JSlider.VERTICAL, CELSIUS_MIN, CELSIUS_MAX, CELSIUS_INIT);
|
||||
celsiusPanel.add(__celsius);
|
||||
celsiusPanel.add(__celsiusSlider);
|
||||
celsiusPanel.add(new JLabel("Celsius"));
|
||||
|
||||
JPanel fahrenheitPanel = new JPanel();
|
||||
BoxLayout boxLayoutFahrenheit = new BoxLayout(fahrenheitPanel, BoxLayout.Y_AXIS);
|
||||
fahrenheitPanel.setLayout(boxLayoutFahrenheit);
|
||||
|
||||
__fahrenheit = new JTextField();
|
||||
__fahrenheit.setText(Integer.toString(FAHRENHEIT_INIT));
|
||||
__fahrenheitSlider = new JSlider(JSlider.VERTICAL, FAHRENHEIT_MIN, FAHRENHEIT_MAX, FAHRENHEIT_INIT);
|
||||
fahrenheitPanel.add(__fahrenheit);
|
||||
fahrenheitPanel.add(__fahrenheitSlider);
|
||||
fahrenheitPanel.add(new JLabel("Fahrenheit"));
|
||||
|
||||
JPanel middlePanel = new JPanel();
|
||||
middlePanel.setLayout(new BoxLayout(middlePanel, BoxLayout.Y_AXIS));
|
||||
__convertCelsiusButton = new JButton("Convert to Fahrenheit");
|
||||
__convertFahrenheitButton = new JButton("Convert to Celsius");
|
||||
middlePanel.add(__convertCelsiusButton);
|
||||
middlePanel.add(__convertFahrenheitButton);
|
||||
|
||||
__panel.add(celsiusPanel);
|
||||
__panel.add(middlePanel);
|
||||
__panel.add(fahrenheitPanel);
|
||||
|
||||
__frame.getContentPane().add(__panel, BorderLayout.CENTER);
|
||||
__frame.pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gibt das JFrame der UI zurück.
|
||||
*
|
||||
* @return Das JFrame der UI.
|
||||
*/
|
||||
public JFrame getUIFrame()
|
||||
{
|
||||
return __frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zeigt das Hauptfenster an.
|
||||
*/
|
||||
public void zeigeFenster()
|
||||
{
|
||||
__frame.setLocationRelativeTo(null);
|
||||
__frame.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Schließt das Fenster.
|
||||
*/
|
||||
public void schliesseFenster()
|
||||
{
|
||||
__frame.dispose();
|
||||
}
|
||||
|
||||
public JTextField getCelsius() {
|
||||
return __celsius;
|
||||
}
|
||||
|
||||
public JTextField getFahrenheit() {
|
||||
return __fahrenheit;
|
||||
}
|
||||
|
||||
public JSlider getCelsiusSlider() {
|
||||
return __celsiusSlider;
|
||||
}
|
||||
|
||||
public JSlider getFahrenheitSlider() {
|
||||
return __fahrenheitSlider;
|
||||
}
|
||||
|
||||
public JButton getCelsiusButton() {
|
||||
return __convertCelsiusButton;
|
||||
}
|
||||
|
||||
public JButton getFahrenheitButton() {
|
||||
return __convertFahrenheitButton;
|
||||
}
|
||||
|
||||
/**
|
||||
* Aktualisiert das Layout.
|
||||
*/
|
||||
private void aktualisiereLayout()
|
||||
{
|
||||
__frame.validate();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user