2 つの垂直スクロールバーを備えた初期段階の Java アプレットがあります。1 つが変更されると、もう 1 つがそれに追従し、それらの横にあるラベルがスクロールバーの値に更新されます。これは機能しますが、返される値は本来あるべき値から反転しています。スクロールバーの初期化では、最大値と最小値が正しい値であり、ステートメントに正しく配置されます。バーを最大値までドラッグすると、最小値がラベルに表示され、その逆も同様です。何か間違ったことを初期化したのか、それとも表示される値を計算するための計算が間違っているのかはわかりません。
注として、使用されているすべての数値は代表的なものであるか、華氏、ケルビン、またはランキンのいずれかです。
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;
}
}