软件设计模式与软件体系-编程作业1

课下设计 - 编程作业

在例6.19的基础上添加两个观察者类:RankineGUI和ReumurGUI,分别代表两种新的温度表示度量。

//RankineGUI
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.util.Observable;
import java.util.Observer;

public class RankineGUI extends JFrame implements Observer{
    private JSplitPane  bigSplitPane;
    private JPanel showInfoPane;
    private JPanel tempColorPane;
    private JLabel lblTempType;
    private JLabel lblColorTemp;
    private JTextArea tempTextArea;
    private JTextArea colorTempTxt;
    private static Dimension size = new Dimension(400, 200);

    public RankineGUI() {
        super("Rankine - Observer 4");
        setUpScrollPanes();
    }

    private void setUpScrollPanes() {
        Border raisedbevel = BorderFactory.createRaisedBevelBorder();

        lblTempType= new JLabel("Rankine Temperature");
        lblTempType.setFont(new Font("Helvetica", Font.BOLD, 13));

        tempTextArea = new JTextArea(20, 20);
        tempTextArea.setFont(new Font("Helvetica", Font.BOLD, 20));
        tempTextArea.setLineWrap(true);
        tempTextArea.setBackground(Color.pink);

        showInfoPane = new JPanel();
        showInfoPane.setLayout(new BorderLayout());
        showInfoPane.add(lblTempType, "North");
        showInfoPane.add(tempTextArea, "Center");

        lblColorTemp = new JLabel("Temperature Color");
        lblColorTemp.setFont(new Font("Helvetica", Font.BOLD, 13));
        colorTempTxt = new JTextArea(20, 20);

        tempColorPane = new JPanel();
        tempColorPane.setLayout(new BorderLayout());
        tempColorPane.add(lblColorTemp, "North");
        tempColorPane.add(colorTempTxt, "Center");

        bigSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, showInfoPane,tempColorPane);
        bigSplitPane.setDividerLocation(200);

        getContentPane().add(bigSplitPane);
        setSize(size);
        setVisible(true);
    }

    public void update(Observable subject, Object arg) {
        String t = (String) arg;


        TemperatureGUI tg = (TemperatureGUI)subject;
        String option = tg.getSelectedTemExpression();

        TemperatureConvertor tc = new TemperatureConvertor();
        float kTem = tc.getKelvinTemperature(option,t);
        tempTextArea.setText(""+ kTem);

        Color color=tc.getColor(kTem);
        colorTempTxt.setBackground(color);
    }
}
//ReumurGUI
import java.awt.*;
import javax.swing.*;
import javax.swing.border.Border;
import java.util.Observable;
import java.util.Observer;

public class ReaumurGUI extends JFrame implements Observer{
    private JSplitPane  bigSplitPane;
    private JPanel showInfoPane;
    private JPanel tempColorPane;
    private JLabel lblTempType;
    private JLabel lblColorTemp;
    private JTextArea tempTextArea;
    private JTextArea colorTempTxt;
    private static Dimension size = new Dimension(400, 200);

    public ReaumurGUI() {
        super("Reaumur - Observer 5");
        setUpScrollPanes();
    }

    private void setUpScrollPanes() {
        Border raisedbevel = BorderFactory.createRaisedBevelBorder();

        lblTempType= new JLabel("Reaumur Temperature");
        lblTempType.setFont(new Font("Helvetica", Font.BOLD, 13));

        tempTextArea = new JTextArea(20, 20);
        tempTextArea.setFont(new Font("Helvetica", Font.BOLD, 20));
        tempTextArea.setLineWrap(true);
        tempTextArea.setBackground(Color.pink);

        showInfoPane = new JPanel();
        showInfoPane.setLayout(new BorderLayout());
        showInfoPane.add(lblTempType, "North");
        showInfoPane.add(tempTextArea, "Center");

        lblColorTemp = new JLabel("Temperature Color");
        lblColorTemp.setFont(new Font("Helvetica", Font.BOLD, 13));
        colorTempTxt = new JTextArea(20, 20);

        tempColorPane = new JPanel();
        tempColorPane.setLayout(new BorderLayout());
        tempColorPane.add(lblColorTemp, "North");
        tempColorPane.add(colorTempTxt, "Center");

        bigSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, showInfoPane,tempColorPane);
        bigSplitPane.setDividerLocation(200);

        getContentPane().add(bigSplitPane);
        setSize(size);
        setVisible(true);
    }

    public void update(Observable subject, Object arg) {
        String t = (String) arg;


        TemperatureGUI tg = (TemperatureGUI)subject;
        String option = tg.getSelectedTemExpression();

        TemperatureConvertor tc = new TemperatureConvertor();
        float kTem = tc.getKelvinTemperature(option,t);
        tempTextArea.setText(""+ kTem);

        Color color=tc.getColor(kTem);
        colorTempTxt.setBackground(color);
    }
}

演示视频:

猜你喜欢

转载自blog.csdn.net/tangzongpi6936/article/details/82727181
今日推荐