2012-04-11 3 views
1

JTextArea를 올바르게 업데이트하지 않는 것 때문에 도움이 될만한 사람이 있습니까? 오늘 밤 인해 숙제가 필요하지만 한 가지 걸림돌이 있습니다. 루프는 자체적으로 실행되며 콘솔에 6 줄을 생성하지만 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); 

을 Btw는 : 난 (난 당신이 아직 :-)를 배우고있는 것 같아요) 오류를 검색 할 때, LINEBREAK 들여 쓰기 아무것도 비용과 많은 도움이되지 않는 프로그래밍 스타일을 변경하는 것이 좋습니다 .

+1

excelent catch ---> append() – mKorbel

+0

가장 좋은 방법은 문자열 함수 대신 컬렉션을 사용하는 것이지만 Java의 .net StringBuilder에 해당하는 것이 있는지는 잘 모릅니다. – Philipp

+0

나는 Snicolas SwingWorker를 좋아하고, 알아두면 유용 할 것으로 보이지만 집에서 일하는 것이 더 빠르고 더러운 것을 위해 더 많은 독서를 할 것입니다. 간단한 해결책을 위해 필립 Mehrwald에게 감사해야합니다. – David

3

스윙 작업자를 사용해보십시오. 이 실행 파일은 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); 
    } 
}; 
관련 문제