0

问题在于这个程序计算正确并正确输出信息,虽然它输出正确的值,但它没有正确显示它们。它们被随机切断或部分覆盖。如果我调整小程序窗口的大小,即使我不移动它而只是单击调整栏,它也会移动输出语句并正确显示它。下面是我的 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); 
    }
}
4

1 回答 1

0

我建议查看一些有关 Java 布局的信息。默认情况下,Applet 带有FlowLayout,这可能不是您想要的,因为它基本上是并排添加组件的。最好使用的布局可能是GridBagLayout

此外,您可能希望使用 setSize 方法为您的小程序设置固定大小。

为了更清楚地说明这一点,如果您想使用 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 网格中。

于 2013-03-19T17:46:12.500 回答