0
問題是這個程序能夠正確計算並正確輸出信息,它是在輸出正確的值時,不能正確顯示它們。它們被隨機切斷或部分遮蓋。如果我調整applet窗口的大小,即使我不移動它,但只需單擊調整欄,它將移動輸出轉換並正確顯示它。以下是我的JAva代碼。Java小應用程序顯示問題
/*
*Programmer
*Project: Ohm's Law AKA the program of difficulties
*/
import java.awt. *;
import java.applet.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import java.text.DecimalFormat;
public class ohmslaw extends Applet implements ActionListener
{
Color seablue = new Color(70,191,243);
Color white = new Color (250,250,250);
Color black = new Color (0,0,0);
Color NavyBlue = new Color (0,0,153);
Color VegasGold = new Color (197,179,88);
Font fontOne = new Font("Century Schoolbook",Font.PLAIN, 16);
Font fontTwo = new Font("Comic Sans", Font.BOLD, 16);
Font fontThree = new Font ("Times New Roman", Font.PLAIN, 16);
Label titleLabel = new Label ("The Ohm's Law calculator");
Label noteLabel = new Label ("For all unknown values enter 0");
Label voltagelabel = new Label ("Please enter the voltage");
TextField voltageField = new TextField(10);
Label currentlabel = new Label ("Please enter the current");
TextField currentField = new TextField(10);
Label resistancelabel = new Label ("Please enter the resistance");
TextField resistanceField = new TextField(10);
Button calcButton = new Button("Calculate");
Label calcLabel = new Label("Click calculate to output the other variable");
Button clearButton = new Button("Clear");
Label clearLabel = new Label("Click the Clear Buttton to add new data.");
Label newresistanceLabel = new Label (" ");
Label newcurrentLabel = new Label (" ");
Label newvoltageLabel = new Label (" ");
Label blankarea = new Label (" ");
public void init()
{
setBackground(seablue);
setForeground(white);
add(titleLabel);
add(noteLabel);
add(voltagelabel);
setForeground(black);
add(voltageField);
setForeground(white);
add(currentlabel);
setForeground(black);
add(currentField);
setForeground(white);
add(resistancelabel);
setForeground(black);
add(resistanceField);
add(calcButton);
calcButton.addActionListener(this);
setForeground(white);
setForeground(black);
add(clearButton);
clearButton.addActionListener(this);
setForeground(white);
add(clearLabel);
add(newresistanceLabel);
add(newcurrentLabel);
add(newvoltageLabel);
}
public void actionPerformed(ActionEvent e)
{
double resistance;
double current;
double voltage;
if(e.getActionCommand() == "Calculate")
{
resistance = Double.parseDouble(resistanceField.getText());
current = Double.parseDouble(currentField.getText());
voltage = Double.parseDouble(voltageField.getText());
if(resistance == 0)
{
doResistance(current, voltage);
}
if(current == 0)
{
doCurrent(resistance, voltage);
}
if(voltage == 0)
{
doVoltage(resistance, current);
}
}
if(e.getActionCommand() == "Clear")
{
voltageField.setText("");
currentField.setText("");
resistanceField.setText("");
newresistanceLabel.setText (" ");
newcurrentLabel.setText (" ");
newvoltageLabel.setText(" ");
calcLabel.setText("Click the Calculate Button to output your other value.");
voltageField.requestFocus();
}
}
public double doResistance(double current, double voltage)
{
double Rfinal;
Rfinal = ((voltage)/(current));
ResistanceOutput(current, voltage, Rfinal);
return(0);
}
public double doCurrent(double resistance, double voltage)
{
double Ifinal;
Ifinal = ((voltage)/(resistance));
CurrentOutput(resistance, voltage, Ifinal);
return (0);
}
public double doVoltage(double resistance, double current)
{
double Vfinal;
Vfinal = ((resistance)*(current));
VoltageOutput(resistance, current, Vfinal);
return(0);
}
public double ResistanceOutput(double current, double voltage, double Rfinal)
{
DecimalFormat two = new DecimalFormat(".0");
newresistanceLabel.setForeground(white);
newresistanceLabel.setText("Your Resistance is "
+ two.format(Rfinal) + ".");
return(0);
}
public double CurrentOutput(double resistance, double voltage, double Ifinal)
{
DecimalFormat two = new DecimalFormat(".0");
newcurrentLabel.setForeground(white);
newcurrentLabel.setText("Your current is "
+ two.format(Ifinal) + ".");
return(0);
}
public double VoltageOutput(double resistance, double current, double Vfinal)
{
DecimalFormat two = new DecimalFormat(".0");
newvoltageLabel.setForeground(white);
newvoltageLabel.setText("Your voltage is "
+ two.format(Vfinal) + ".");
return(0);
}
public void paint(Graphics g)
{
Image picture;
picture = getImage(getDocumentBase(), "ohm.jpg");
g.drawImage(picture, 55,350, this);
}
}
現在是2013年,從AWT轉到Swing的時候了! – 2013-03-20 03:01:09