垂直滚动条返回的值与Java小程序中应该返回的值相反

垂直滚动条返回的值与Java小程序中应该返回的值相反,java,scrollbar,awt,Java,Scrollbar,Awt,我在早期阶段有一个java小程序,带有两个垂直滚动条。当其中一个被更改时,另一个也会随之更改,并且旁边的标签会更新为滚动条的值。这是可行的,但是返回的值与应该的值不同。在滚动条的初始化过程中,最大值和最小值是正确的值,并正确放置在语句中。将条向上拖动到最大值时,最小值将显示在标签中,反之亦然。我不知道我是否初始化了错误的东西,或者计算要显示的值背后的数学是否错误 请注意,所使用的所有数字都具有代表性,或者是华氏度、开尔文或兰金 import java.awt.*; import java.app

我在早期阶段有一个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;
    }                       
}

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

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

这只解决了显示文本的问题。

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

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

这只解决了显示的文本问题。

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

因此,在这一行之后:

Rbar.setValue(newvalue);
在s==Rbar块中,您需要:

newvalue = Rbar.getMaximum() + Rbar.getMinimum() - Rbar.getValue();
newvalue = Kbar.getMaximum() + Kbar.getMinimum() - Kbar.getValue(); 
在s==Kbar块中,您需要:

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

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

因此,在这一行之后:

Rbar.setValue(newvalue);
在s==Rbar块中,您需要:

newvalue = Rbar.getMaximum() + Rbar.getMinimum() - Rbar.getValue();
newvalue = Kbar.getMaximum() + Kbar.getMinimum() - Kbar.getValue(); 
在s==Kbar块中,您需要:

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

嗯,这确实适用于标签中显示的值,但滑块气泡的动画是相反的。当气泡向上滑动时,它似乎向下移动,然后在松开鼠标时捕捉到它的位置。只要在“Rbar.setValuenewvalue”行之后重新分配newvalue变量,就不会出现该问题,如前所述,但是我同意@SF有一个更干净的解决方案。嗯,这确实适用于标签中显示的值,但是滑块气泡的动画是相反的。当气泡向上滑动时,它似乎向下移动,然后在放开鼠标时迅速恢复到其位置。只要在“Rbar.setValuenewvalue”行之后重新分配newvalue变量,我就没有遇到过这个问题,如前所述,但我同意@SF有一个更干净的解决方案。成功!显示器是我真正需要的,所以这正是我需要的。奇怪的是,你说主要的问题是最小值是顶部,最大值是底部。有什么办法可以扭转这种局面吗?成功!显示器是我真正需要的,所以这正是我需要的。奇怪的是,你说主要的问题是最小值是顶部,最大值是底部。有没有办法把这个翻过来?