2013-03-19 106 views
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); 
    } 
} 
+0

現在是2013年,從AWT轉到Swing的時候了! – 2013-03-20 03:01:09

回答

0

我建議看一些關於Java佈局的信息。 Applet默認隨附FlowLayout,這可能並不是你想要的,因爲它基本上是一起添加組件。可能最好使用的佈局是GridBagLayout

此外,您可能希望使用setSize方法爲applet設置固定大小。

爲了使這更清楚,如果你想使用網格佈局(這是很容易,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 
} 

這個網格佈局將增加你的所有組件到7x2網格。