2011-12-10 33 views
2

所以我正在編寫一個程序來解決一個二次方程,並且當涉及到使2個JLabel(先前爲空)顯示答案時(當用戶單擊一個JButton )JLabel.setText()方法

這是整個程序,因爲我不知道錯誤在哪裏。

import java.awt.event.*; 

import javax.swing.*; 


public class Third implements ActionListener { 

    //-------------- 
    //Data Members 
    //-------------- 
/** 
* Top level window 
*/ 
JFrame top; 
/** 
* Changed into a string by ConvertToDouble(string str); 
*/ 
double a, b, c; 
double answer1,answer2; 
JTextField inputA, inputB, inputC; 
JLabel describeA, describeB, describeC, print1, print2; 
JButton submit; 
String aa, bb, cc; 
String result1, result2; 
String strA, strB, strC; 




    public Third(){ 

    top = new JFrame("Ned's quadratic equation solver"); 
    top.setVisible(true); 
    top.setLayout(null); 
    top.setBounds(50,50,250,250); 


    inputA = new JTextField(12);  
    inputA.setBounds(100,30,200,25); 
    inputB = new JTextField(12);  
    inputB.setBounds(100,105,200,25); 
    inputC = new JTextField(12);  
    inputC.setBounds(100,185,200,25); 

    describeA = new JLabel("Enter A here:"); 
    describeA.setBounds(10,30,200,25); 
    describeB = new JLabel("Enter B here:"); 
    describeB.setBounds(10,105,200,25); 
    describeC = new JLabel("Enter C here:"); 
    describeC.setBounds(10,185,200,25); 

    print1 = new JLabel(); 
    print1.setBounds(15,290,1000,10); 
    print2 = new JLabel(); 
    print2.setBounds(15,310,1000,10); 

    submit = new JButton ("WHAT DOES X = ???"); 
    submit.setBounds(50,230,150,25); 
    submit.addActionListener(this); 



    top.add(inputA); 
    top.add(inputB); 
    top.add(inputC); 

    top.add(describeA); 
    top.add(describeB); 
    top.add(describeC); 

    top.add(submit); 
    top.doLayout(); 
    } 


    public void actionPerformed(ActionEvent event) { 

     aa = inputA.getText(); 
     bb = inputB.getText(); 
     cc = inputC.getText(); 

     a = convertToDouble(aa); 
     b = convertToDouble(bb); 
     c = convertToDouble(cc); 

     makeAns(a,b,c); 

    /* 
     * DEBUG CODE 
     * 
     * System.out.println(a); 
     * System.out.println(b); 
     * System.out.println(c); 
     * System.out.println(answer1); 
     * System.out.println(answer2); 
     */ 
     result1 = "x = " + answer1; 
     result2 = "x = " + answer2; 

      print1.setText(result1); 
      print2.setText(result2); 

      //System.out.println(result1); 

      top.doLayout(); 


    } 



private void makeAns(double x,double y,double z){ 

     answer1 =(-y + Math.sqrt (y*y-4*x*z))/(2*x); 
     answer2 =(-y - Math.sqrt (y*y-4*x*z))/(2*x); 

    } 

private double convertToDouble (String str) { 

    Double dubb = new Double(str); 
    return dubb.doubleValue(); 
} 


} 
+0

請包括一些關於錯誤說明或預期與實際行爲的描述。 – Thomas

回答

8

您必須先在GUI中添加組件,然後才能顯示任何內容。你在哪裏添加你的print1和print2 JLabel到GUI或任何容器?

此外,您將需要使用佈局管理器而不是空佈局和絕對定位,以使您的GUI更容易編碼。

此外,您還可以在之後添加所有組件,然後在JFrame 上調用setVisible(true)

+0

非常感謝! –

+0

@ user1091236:不客氣! –

+3

@ user1091236:您可以點擊[空複選標記](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)來接受此答案。左邊。 – trashgod