我有一个早期阶段的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;
}
}
答案 0 :(得分:5)
主要问题似乎是min是top而max是你的垂直滚动条的底部。解决这个问题的快捷方法是简单地更改标签以反映差异
curK_text.setText(String.valueOf(minK + maxK- curK));
curR_text.setText(String.valueOf(minR + maxR - curR));
这只会修复显示的文字问题。
答案 1 :(得分:1)
在adjustValueChanged()方法中,您需要将输入值“反转”到计算中,以计算最大值和最小值。
所以,在这一行之后:
Rbar.setValue(newvalue);
在“(s == Rbar)”块中,您需要:
newvalue = Rbar.getMaximum() + Rbar.getMinimum() - Rbar.getValue();
在“(s == Kbar)”区块中,您需要:
newvalue = Kbar.getMaximum() + Kbar.getMinimum() - Kbar.getValue();