问题在于这个程序正确计算并正确输出信息,当它输出正确的值时,它不会正确显示它们。它们被随机切断或部分遮盖。如果我调整applet窗口的大小,即使我没有移动它,只需单击调整栏,它就会移动输出的sentance并正确显示它。下面是我的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);
}
}
答案 0 :(得分:0)
我建议查看有关Java布局的一些信息。默认情况下,Applet带有FlowLayout,这可能不是您想要的,因为它基本上是并排添加组件。可能使用的最佳布局是GridBagLayout。
此外,您可能希望使用setSize方法为applet设置固定大小。
为了更清楚,如果你想使用GridLayout(比GridBagLayout更容易使用),你可以在代码中添加以下行:
import java.awt.GridLayout;
//...
public void init(){
this.setLayout(new GridLayout(7,2));
//your code for adding components...
this.setSize(550,250);//sets fixed size
}
此GridLayout会将所有组件添加到7x2网格中。