2012-04-11 45 views
1

有人可以幫助我,我今晚有作業,但有一個絆腳石,因爲我似乎沒有正確更新JTextArea。該循環自行運行,併產生6行的CONSOL但不會做同樣的GUI?任何建議歡迎。Java JTextArea未正確填充。請幫助

import java.awt.EventQueue; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import javax.swing.JTextArea; 

@SuppressWarnings("serial") 
public class GUI extends JFrame { 

private JPanel contentPane; 
private JTextArea textField; 

/** 
* Launch the application. 
*/ 
public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       GUI frame = new GUI(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

/** 
* Create the frame. 
*/ 
public GUI() { 
    setTitle("Home Improvement"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 666, 300); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    contentPane.setLayout(null); 
    textField = new JTextArea(); 
    textField.setEditable(false); 
    textField.setBounds(5, 5, 655, 235); 
    contentPane.add(textField); 

    JButton btnClear = new JButton("Clear"); 
    btnClear.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent arg0) 
     { 
      textField.setText(""); 
     } 
    }); 
    btnClear.setBounds(543, 243, 117, 29); 
    contentPane.add(btnClear); 

    JButton btnProcess = new JButton("Process"); 
    btnProcess.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) 
     { 
      populate(); 

     } 
    }); 
    btnProcess.setBounds(414, 244, 117, 29); 
    contentPane.add(btnProcess); 
} 


public void populate() 
{ 
    String msg = "Error"; 
    HomeImprovement[] homeImprove = new HomeImprovement[6]; 
    double price [] = {500.99,33,90,1599,33,900}; 
    int isWhat [] = {1,0,0,0,1,1}; 

    homeImprove[0] = new TileInstaller("The Tile Guy\t"); 
    homeImprove[1] = new Electrician("Electrons R Us\t"); 
    homeImprove[2] = new HandyMan("Mr. Handy\t"); 
    homeImprove[3] = new GeneralContractor("N.B.J. Inc.\t"); 
    homeImprove[4] = new HandyMan("Mr. Handy(Tile)"); 
    homeImprove[5] = new GeneralContractor("N.B.J. Inc.(Tile)"); 

    for(int i=0; i<homeImprove.length; i++) 
     { 
      if ((isWhat[i] != 1)) 
      {msg=homeImprove[i].doTheElectric();} 
      else 
      {msg=homeImprove[i].tileIt();} 

     String tmp = "Company: "+homeImprove[i].getCompanyName() + "\t" + 
         "Final Price: "+ homeImprove[i].computeBid(price[i]) + "\t" + 
         "msg: " + msg + "\n"; 

     textField.setText(tmp); 
     } 

} 
} 

回答

3

你總是覆蓋字符串。順便說一句

String tmp = ""; 
for(int i=0; i<homeImprove.length; i++) 
{ 
    if (isWhat[i] != 1) 
    { 
     msg = homeImprove[i].doTheElectric(); 
    } 
    else 
    { 
     msg = homeImprove[i].tileIt(); 
    } 

    tmp += "Company: "+homeImprove[i].getCompanyName() + "\t" + 
     "Final Price: "+ homeImprove[i].computeBid(price[i]) + "\t" + 
     "msg: " + msg + "\n"; 

} 
textField.setText(tmp); 

試試這個,我建議改變你的編程風格(我猜你還在學習:-)),換行符和縮進錯誤搜索時不需要支付任何費用,並幫助了很多。

+1

excelent catch ---> append() – mKorbel 2012-04-11 20:47:40

+0

最好的方法是使用集合而不是字符串函數,但我不知道在java中是否有.net的StringBuilder對應。 – Philipp 2012-04-11 20:55:11

+0

我喜歡Snicolas SwingWorker,因爲它看起來很有用,所以我會做更多的閱讀,但是爲了快速和骯髒地打開家庭作業。我不得不感謝Philipp Mehrwald的簡單決議。 – David 2012-04-11 20:58:15

3

嘗試使用揮杆工。這個runnable將在ui線程上執行並更新你的UI。

SwingWorker worker = new SwingWorker<ImageIcon[], Void>() { 
private String populated = null; 
    @Override 
    public ImageIcon[] doInBackground() { 
     String populated = populate(); //but remove last line and return the result 
    } 

    @Override 
    public void done() { 
     textField.append(populated); 
    } 
};