java中的观察者设计模式

hrysbysz  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(336)

这是一个将celcuis改为fahrenhiat的代码。我应该用观察者设计模式完成代码。问题是它没有改变swing(outputframe)中的值……(它在终端中改变了正确的值)请帮助我。

public class WeatherTemperture implements Subject {
    private float celsius=0;
    private float fahrenheit=0;
    private ArrayList<Observer> observersArrayList = new ArrayList<>();

    public WeatherTemperture() {
    }

    public void convertToFahrenheit(float celsius) {

        this.fahrenheit = (float) ((celsius * 1.8) + 32);
    }

    public void convertToCelsius(float fahrenheit) {
        this.celsius = (float) ((fahrenheit - 32) / 1.8);
    }

    public float getCelsius() {
        return celsius;
    }

    public float getFahrenheit() {
        return fahrenheit;
    }

    @Override
    public void registerObserver(Observer observer) {
        observersArrayList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        int i = observersArrayList.indexOf(observer);
        if (i >= 0) {
            observersArrayList.add(observer);
        }
    }

    @Override
    public void notifyObservers() {

        for (int i = 0; i < observersArrayList.size(); i++) {
            observersArrayList.get(i).update(this);
        }
    }
}

这是第一帧:(输入值)

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class InputFrame extends JFrame {

    private JTextField celsiusField;
    private JTextField fahrenheitField;
    private JButton convertCelsius;
    private JButton convertFahrenheit;
    private ArrayList<Observer> observerArrayList = new ArrayList<>();

    private WeatherTemperture weatherTemperture;

    public InputFrame(WeatherTemperture weatherTemperture) {
        init();
        this.weatherTemperture = weatherTemperture;
    }

    private void init() {
        this.setLayout(new FlowLayout());
        //
        JLabel celsiusLabel = new JLabel("Degrees in Celsius: ");
        this.add(celsiusLabel);
        //
        celsiusField = new JTextField();
        celsiusField.setPreferredSize(new Dimension(50, 22));
        this.add(celsiusField);
        //
        convertCelsius = new JButton("Convert to Fahrenheit > ");
        convertCelsius.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                float Celsius = Float.parseFloat(celsiusField.getText());
                System.out.println(" =="+Celsius);
                weatherTemperture.convertToFahrenheit(Celsius);
                System.out.println(" =="+weatherTemperture.getFahrenheit());
                weatherTemperture.notifyObservers();

            }
        });
        this.add(convertCelsius);
        //
        JLabel dummyLabel1 = new JLabel();
        dummyLabel1.setPreferredSize(new Dimension(50, 22));
        this.add(dummyLabel1);
        //
        JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
        separator.setPreferredSize(new Dimension(250, 5));
        this.add(separator);
        //
        JLabel dummyLabel2 = new JLabel();
        dummyLabel2.setPreferredSize(new Dimension(100, 22));
        this.add(dummyLabel2);
        //
        JLabel fahrenheitLabel = new JLabel("Degrees in Fahrenheit: ");
        this.add(fahrenheitLabel);
        //
        fahrenheitField = new JTextField();
        fahrenheitField.setPreferredSize(new Dimension(50, 22));
        this.add(fahrenheitField);
        //
        convertFahrenheit = new JButton("Convert to Celsius > ");
        convertFahrenheit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                float fahrenheit = Float.parseFloat(fahrenheitField.getText());
                System.out.println(" =="+fahrenheit);
                weatherTemperture.convertToCelsius(fahrenheit);
                System.out.println(" =="+weatherTemperture.getCelsius());
                weatherTemperture.notifyObservers();

            }
        });
        this.add(convertFahrenheit);
    }

}

这是输出帧:(应该显示结果)

import javax.swing.*;
import java.awt.*;

public class OutputFrame extends JFrame implements Observer {
    private JTextField fahrenheitOutput;
    private JTextField celsiusOutput;

    private float celsius;
    private float fahrenheit;

    private WeatherTemperture weatherTemperture;

    public OutputFrame(WeatherTemperture weatherTemperture) {
        init();
        this.weatherTemperture = weatherTemperture;
        weatherTemperture.registerObserver(this);

    }

    private void init() {
        this.setLayout(new FlowLayout());
        //
        JLabel fahrenheitLabel = new JLabel("Equivalent fahrenheit: ");
        this.add(fahrenheitLabel);
        //
        fahrenheitOutput = new JTextField();
        fahrenheitOutput.setPreferredSize(new Dimension(150, 22));
        fahrenheitOutput.setEditable(false);
        //changed
        try {
            fahrenheitOutput.setText(String.valueOf(weatherTemperture.getFahrenheit()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        this.add(fahrenheitOutput);

        //
        JLabel dummyLabel1 = new JLabel();
        dummyLabel1.setPreferredSize(new Dimension(150, 32));
        this.add(dummyLabel1);
        //
        JSeparator separator = new JSeparator(SwingConstants.HORIZONTAL);
        separator.setPreferredSize(new Dimension(250, 5));
        this.add(separator);
        //
        JLabel dummyLabel4 = new JLabel();
        dummyLabel4.setPreferredSize(new Dimension(150, 22));
        this.add(dummyLabel4);
        //
        JLabel celsiusLabel = new JLabel("Equivalent celsius: ");
        this.add(celsiusLabel);
        //
        celsiusOutput = new JTextField();
        celsiusOutput.setPreferredSize(new Dimension(150, 22));
        celsiusOutput.setEditable(false);
        //changed
        try {
            celsiusOutput.setText(String.valueOf(weatherTemperture.getCelsius()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.add(celsiusOutput);
    }

    @Override
    public void update(WeatherTemperture weatherTemperture) {
        this.celsius = weatherTemperture.getCelsius();
        this.fahrenheit = weatherTemperture.getFahrenheit();
    }
}

这是两个帧聚集的地方:

public class Application {
    public static void main(String[] args) {

        WeatherTemperture weatherTemperture = new WeatherTemperture();

        InputFrame inputFrame = new InputFrame(weatherTemperture);
        inputFrame.setBounds(250, 250, 250, 220);
        inputFrame.setResizable(false);
        inputFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        inputFrame.setLocationRelativeTo(null);
        inputFrame.setAlwaysOnTop(true);
        inputFrame.setVisible(true);
        //

        OutputFrame outputFrame = new OutputFrame(weatherTemperture);
        outputFrame.setBounds(501, 250, 250, 220);
        outputFrame.setResizable(false);
        outputFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        outputFrame.setLocation(inputFrame.getX() + inputFrame.getWidth() + 1, inputFrame.getY());
        outputFrame.setAlwaysOnTop(true);
        outputFrame.setVisible(true);

    }
}
eivnm1vs

eivnm1vs1#

根据我看到的,output框架上的update()方法从不更新文本字段以显示新值。假设您的观察者按预期工作,这就可以了。

@Override
public void update(WeatherTemperture weatherTemperture) {
    this.celsius = weatherTemperture.getCelsius();
    this.fahrenheit = weatherTemperture.getFahrenheit();

    celsiusOutput.setText(String.valueOf(this.celsius));
    fahrenheitOutput.setText(String.valueOf(this.fahrenheit));
}

相关问题