gpt4 book ai didi

java - 垂直滚动条返回的值与它们在 Java 小程序中应返回的值相反

转载 作者:行者123 更新时间:2023-11-29 07:12:02 25 4
gpt4 key购买 nike

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

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

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;
}
}

最佳答案

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

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

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

关于java - 垂直滚动条返回的值与它们在 Java 小程序中应返回的值相反,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13037136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com