mirror of https://github.com/2martens/uni.git
ID-5: Aufgabe 2 bearbeitet
This commit is contained in:
parent
64a4762a1f
commit
4d8ed895e2
|
@ -0,0 +1,64 @@
|
|||
import javax.swing.JFrame;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
|
||||
public class Controller {
|
||||
private Temperature _temperature;
|
||||
private UserInterface _ui;
|
||||
private JFrame _frame;
|
||||
|
||||
public Controller() {
|
||||
_temperature = new Temperature();
|
||||
_ui = new UserInterface(this);
|
||||
_frame = _ui.getUIFrame();
|
||||
_temperature.addObserver(_ui);
|
||||
_ui.initGUI();
|
||||
registriereUIAktionen();
|
||||
_ui.zeigeFenster();
|
||||
}
|
||||
|
||||
private void registriereUIAktionen() {
|
||||
_ui.getCelsius().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
_ui.getCelsiusSlider().setValue(Integer.valueOf(_ui.getCelsius().getText()));
|
||||
}
|
||||
});
|
||||
|
||||
_ui.getCelsiusSlider().addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent changeEvent) {
|
||||
int celsius = _ui.getCelsiusSlider().getValue();
|
||||
_ui.getCelsius().setText(Integer.toString(celsius));
|
||||
}
|
||||
});
|
||||
|
||||
_ui.getFahrenheit().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
_ui.getFahrenheitSlider().setValue(Integer.valueOf(_ui.getFahrenheit().getText()));
|
||||
}
|
||||
});
|
||||
|
||||
_ui.getFahrenheitSlider().addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent changeEvent) {
|
||||
int fahrenheit = _ui.getFahrenheitSlider().getValue();
|
||||
_ui.getFahrenheit().setText(Integer.toString(fahrenheit));
|
||||
}
|
||||
});
|
||||
|
||||
_ui.getCelsiusButton().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int celsius = _ui.getCelsiusSlider().getValue();
|
||||
_temperature.setCelsiusTemperature(celsius);
|
||||
}
|
||||
});
|
||||
|
||||
_ui.getFahrenheitButton().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int fahrenheit = _ui.getFahrenheitSlider().getValue();
|
||||
_temperature.setFahrenheitTemperature(fahrenheit);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Main-Class: Main
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] argv) {
|
||||
SwingUtilities.invokeLater(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
UIManager.setLookAndFeel(UIManager
|
||||
.getSystemLookAndFeelClassName());
|
||||
}
|
||||
catch (ClassNotFoundException | InstantiationException
|
||||
| IllegalAccessException
|
||||
| UnsupportedLookAndFeelException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
new Controller();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
import java.util.Observable;
|
||||
|
||||
public class Temperature extends Observable {
|
||||
private int _celsius;
|
||||
private int _fahrenheit;
|
||||
|
||||
public Temperature() {
|
||||
_celsius = 0;
|
||||
_fahrenheit = 32;
|
||||
}
|
||||
|
||||
public int getCelsiusTemperature() {
|
||||
return _celsius;
|
||||
}
|
||||
|
||||
public int getFahrenheitTemperature() {
|
||||
return _fahrenheit;
|
||||
}
|
||||
|
||||
public void setCelsiusTemperature(int celsius) {
|
||||
_celsius = celsius;
|
||||
_fahrenheit = convertToFahrenheit(_celsius);
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
}
|
||||
|
||||
public void setFahrenheitTemperature(int fahrenheit) {
|
||||
_fahrenheit = fahrenheit;
|
||||
_celsius = convertToCelsius(fahrenheit);
|
||||
setChanged();
|
||||
notifyObservers();
|
||||
}
|
||||
|
||||
private int convertToFahrenheit(int celsius) {
|
||||
return Math.round(1.8f * celsius + 32);
|
||||
}
|
||||
|
||||
private int convertToCelsius(int fahrenheit) {
|
||||
return Math.round(5.0f / 9.0f * (fahrenheit - 32));
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
import javax.swing.JFrame;
|
||||
import javax.swing.JSlider;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.event.ChangeListener;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.awt.event.ActionEvent;
|
||||
|
||||
public class HauptWerkzeug {
|
||||
private Umrechner __rechner;
|
||||
private HauptWerkzeugUI __ui;
|
||||
|
||||
private JFrame __frame;
|
||||
|
||||
public HauptWerkzeug(Umrechner rechner) {
|
||||
__rechner = rechner;
|
||||
__ui = new HauptWerkzeugUI();
|
||||
__frame = __ui.getUIFrame();
|
||||
__ui.initGUI();
|
||||
registriereUIAktionen();
|
||||
__ui.zeigeFenster();
|
||||
}
|
||||
|
||||
private void registriereUIAktionen() {
|
||||
__ui.getCelsius().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
__ui.getCelsiusSlider().setValue(Integer.valueOf(__ui.getCelsius().getText()));
|
||||
}
|
||||
});
|
||||
|
||||
__ui.getCelsiusSlider().addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent changeEvent) {
|
||||
int celsius = __ui.getCelsiusSlider().getValue();
|
||||
__ui.getCelsius().setText(Integer.toString(celsius));
|
||||
}
|
||||
});
|
||||
|
||||
__ui.getFahrenheit().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent actionEvent) {
|
||||
__ui.getFahrenheitSlider().setValue(Integer.valueOf(__ui.getFahrenheit().getText()));
|
||||
}
|
||||
});
|
||||
|
||||
__ui.getFahrenheitSlider().addChangeListener(new ChangeListener() {
|
||||
public void stateChanged(ChangeEvent changeEvent) {
|
||||
int fahrenheit = __ui.getFahrenheitSlider().getValue();
|
||||
__ui.getFahrenheit().setText(Integer.toString(fahrenheit));
|
||||
}
|
||||
});
|
||||
|
||||
__ui.getCelsiusButton().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int celsius = __ui.getCelsiusSlider().getValue();
|
||||
int fahrenheit = __rechner.convertToFahrenheit(celsius);
|
||||
__ui.getFahrenheitSlider().setValue(fahrenheit);
|
||||
__ui.getFahrenheit().setText(Integer.toString(fahrenheit));
|
||||
}
|
||||
});
|
||||
|
||||
__ui.getFahrenheitButton().addActionListener(new ActionListener() {
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
int fahrenheit = __ui.getFahrenheitSlider().getValue();
|
||||
int celsius = __rechner.convertToCelsius(fahrenheit);
|
||||
__ui.getCelsiusSlider().setValue(celsius);
|
||||
__ui.getCelsius().setText(Integer.toString(celsius));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,140 @@
|
|||
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;
|
||||
|
||||
class HauptWerkzeugUI {
|
||||
public static final String NAME = "Temperature converter";
|
||||
|
||||
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 HauptWerkzeugUI() {
|
||||
__frame = new JFrame();
|
||||
__frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
__frame.setTitle(NAME);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
Manifest-Version: 1.0
|
||||
Main-Class: Main
|
||||
|
|
@ -0,0 +1,28 @@
|
|||
import javax.swing.SwingUtilities;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.UnsupportedLookAndFeelException;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] argv) {
|
||||
final Umrechner rechner = new Umrechner();
|
||||
SwingUtilities.invokeLater(new Runnable()
|
||||
{
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
try
|
||||
{
|
||||
UIManager.setLookAndFeel(UIManager
|
||||
.getSystemLookAndFeelClassName());
|
||||
}
|
||||
catch (ClassNotFoundException | InstantiationException
|
||||
| IllegalAccessException
|
||||
| UnsupportedLookAndFeelException e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
new HauptWerkzeug(rechner);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
|
||||
public class Umrechner {
|
||||
public int convertToCelsius(int fahrenheit) {
|
||||
return Math.round(5.0f / 9.0f * (fahrenheit - 32));
|
||||
}
|
||||
|
||||
public int convertToFahrenheit(int celsius) {
|
||||
return Math.round(1.8f * celsius + 32);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue