1

我在早期阶段有一个带有 2 个垂直滚动条的 Java 小程序。当一个被改变时,另一个也随之改变,并且它们旁边的标签被更新为滚动条的值。这行得通,但是返回的值从它们应该是翻转的。在滚动条的初始化中,最大值和最小值是正确的值,并正确放置在语句中。当条形图向上拖动到最大值时,最小值将显示在标签中,反之亦然。我不知道我是否初始化了错误,或者计算要显示的值背后的数学是否错误。

请注意,所有使用的数字都是有代表性的,或者是华氏度、开尔文度或兰金度。

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class Exam2 extends Applet implements AdjustmentListener
{
    //variables from the HTML file
    String unit = "K";
    double temp = 0;
    Dimension APPSIZE = this.getSize();

    //variables for temperature
    double curK, curR;
    double fmin = -50.0, fmax = 250.0;
    double minK = (fmin + 459.67) * (5.0/9.0);
    double maxK = (fmax + 459.67) * (5.0/9.0);
    double minR = fmin + 459.67;
    double maxR = fmax + 459.67;
    DecimalFormat df = new DecimalFormat("#0.00");

    //graphics objects
    Scrollbar Kbar = new Scrollbar(Scrollbar.VERTICAL, (int)minK, 10, (int)fmin, (int)fmax);
    Scrollbar Rbar = new Scrollbar(Scrollbar.VERTICAL, (int)minR, 10, (int)fmin, (int)fmax);


    Graphics ColorBar;
    Label KelvinText = new Label("Kelvin");
    Label RankinText = new Label("Rankine");
    Label curK_text = new Label();
    Label curR_text = new Label();
    Label maxK_text = new Label(String.valueOf(df.format(maxK)));
    Label maxR_text = new Label(String.valueOf(df.format(maxR)));
    Label minK_text = new Label(String.valueOf(df.format(minK)));
    Label minR_text = new Label(String.valueOf(df.format(minR)));

    //runtime variables
    boolean running = true;

    public void init()
    {
        Dimension appsize = (APPSIZE);
        double colweight[] = {1,1,1,1,1};//3
        double rowweight[] = {1,1,1,1,1,1,1,1,1,1};//8
        int colwidth[] = {1,1,1,1,1};//3
        int rowheight[] = {1,1,1,1,1,1,1,1,1,1};//8
        GridBagConstraints c = new GridBagConstraints();
        GridBagLayout gbl = new GridBagLayout();
        gbl.rowHeights = rowheight;
        gbl.rowWeights = rowweight;
        gbl.columnWeights = colweight;
        gbl.columnWidths = colwidth;
        c.anchor = GridBagConstraints.CENTER;
        setBounds(0,0,480,640);
        setLayout(new GridLayout(1,5));
        Panel k = new Panel(gbl);
        Panel r = new Panel(gbl);
        Panel drawingpanel = new Panel();

        //add objects
        //Kelvin label
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 1;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.KelvinText,c);

        //maxK_text
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 2;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.maxK_text,c);

        //Kbar
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 1;
        c.gridheight = 5;
        c.gridx = 2;
        c.gridy = 3;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.Kbar,c);

        //minK_text
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 8;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.minK_text,c);


        //Rankin label
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 1;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.RankinText,c);

        //maxR_text
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 2;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.maxR_text,c);

        //Rbar
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 1;
        c.gridheight = 5;
        c.gridx = 2;
        c.gridy = 3;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.Rbar,c);

        //minR_text
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 3;
        c.gridheight = 1;
        c.gridx = 1;
        c.gridy = 8;
        c.fill= GridBagConstraints.VERTICAL;
        gbl.setConstraints(this.minR_text,c);

        //curK_text
        curK_text.setAlignment(Label.RIGHT);
        curK_text.setVisible(true);
        curK_text.setText("0");

        //curR_text
        curR_text.setAlignment(Label.LEFT);
        curR_text.setVisible(true);
        curR_text.setText("0");

        //curR_text
        c.weightx = 1;
        c.weighty = 1;
        c.gridwidth = 2;
        c.gridheight = 1;
        c.gridx = 9;
        c.gridy = 5;
        c.fill= GridBagConstraints.HORIZONTAL;
        gbl.setConstraints(this.curR_text,c);

        //add items
        k.add(this.KelvinText);
        k.add(this.maxK_text);
        k.add(this.Kbar);
        k.add(this.minK_text);
        r.add(this.RankinText);
        r.add(this.maxR_text);
        r.add(this.Rbar);
        r.add(this.minR_text);

        //add listeneers
        Kbar.addAdjustmentListener(this);
        Rbar.addAdjustmentListener(this);

        //draw bar
        ColorBar = drawingpanel.getGraphics();

        //add to screen
        add(curK_text);
        add(k);
        add(drawingpanel);
        add(r);
        add(curR_text);
    }

    public void run()
    {       
        //get the unit
        try{unit = getParameter("UNIT", "K");}
        catch(Exception e){e.printStackTrace();}
        //get the temperature 
        try{temp = getParameter("TEMP", 0);}
        catch(Exception e){e.printStackTrace();}

        //make sure temp is initialized
        if (temp == 0)
        {
            if (unit.equalsIgnoreCase("k"))
            {
                temp = minK;
            }
            else
            {
                temp = minR;
            }
        }

        //initial conversion to initialize scrollbars
        if (unit.equalsIgnoreCase("K"))
        {
            curK = temp;
            curR = curK * (9/5);
        }
        else if(unit.equalsIgnoreCase("R"))
        {
            curR = temp;
            curK = curR * (5/9);
        }
        else
        {
            stop();
            System.err.println("An invalid unit was given for UNIT. The applet is now terminated.");
        }

        //set text labels
        curK_text.setText(String.format(df.format(curK)));
        curR_text.setText(String.format(df.format(curR)));

        //set initial values of scrollbars

        Kbar.setValue((int)curK);
        Rbar.setValue((int)curR);
        while (running)
        {

        }
    }   

    public void adjustmentValueChanged(AdjustmentEvent e)
    {
        Object s = e.getSource();
        int newvalue;
        if (s == Rbar)
        {
            newvalue = Rbar.getValue();
            Kbar.setValue(newvalue);
            Rbar.setValue(newvalue);

            curK = (newvalue + 459.67)*(5.0/9.0);
            curR = newvalue + 459.67;   
            curK_text.setText(String.valueOf(curK));
            curR_text.setText(String.valueOf(curR));    
        }
        if (s == Kbar)
        {
            newvalue = Kbar.getValue(); 
            Kbar.setValue(newvalue);
            Rbar.setValue(newvalue);

            curK = (newvalue + 459.67)*(5.0/9.0);
            curR = newvalue + 459.67;   
            curK_text.setText(String.valueOf(df.format(curK)));
            curR_text.setText(String.valueOf(df.format(curR)));
        }
    }   

    public void stop()
    {
        Kbar.removeAdjustmentListener(this);
        Rbar.removeAdjustmentListener(this);
    }   

    //overridden getParamater methods
    private String getParameter(String key, String def)
    {
        String t;
        return (t = getParameter(key))!=null ? t : def;
    }   

    private int getParameter(String key, int def)
    {
        Integer t;
        return (t = Integer.valueOf(getParameter(key)))!= null ? t.intValue() : def;
    }                       
}
4

2 回答 2

5

主要问题似乎是最小值是顶部,最大值是垂直滚动条的底部。解决此问题的快速方法是简单地更改标签以反映差异

curK_text.setText(String.valueOf(minK + maxK- curK));
curR_text.setText(String.valueOf(minR + maxR - curR)); 

这仅修复了显示的文本问题。

于 2012-10-23T21:00:11.873 回答
1

在您的adjustmentValueChanged() 方法中,您需要将输入值“反转”到计算中以说明您的最大值和最小值。

所以,在这一行之后:

Rbar.setValue(newvalue);

在“(s == Rbar)”块中,您需要:

newvalue = Rbar.getMaximum() + Rbar.getMinimum() - Rbar.getValue();

在“(s == Kbar)”块中,您需要:

newvalue = Kbar.getMaximum() + Kbar.getMinimum() - Kbar.getValue(); 
于 2012-10-23T21:02:39.027 回答