【问题标题】:Java JTextArea not populating correctly. Help pleaseJava JTextArea 未正确填充。请帮忙
【发布时间】:2012-04-24 05:05:46
【问题描述】:

谁能帮助我,我今晚有作业要交,但有一个绊脚石,因为我似乎没有正确更新 JTextArea。循环自行运行并生成 6 行到控制台,但不会对 GUI 做同样的事情?欢迎任何建议。

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);
      }

}
}

【问题讨论】:

    标签: java swing user-interface awt


    【解决方案1】:

    你一直在覆盖字符串。

    试试这个:

    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);
    

    顺便说一句:我建议改变你的编程风格(我猜你还在学习:-)),换行和缩进不需要任何成本,并且在搜索错误时有很大帮助。

    【讨论】:

    • 最好的方法是使用集合而不是字符串函数,但我不知道java中是否有.net的StringBuilder的对应物。
    • 我喜欢 Snicolas SwingWorker,并且会做更多的阅读,因为这似乎很有用,但为了快速和肮脏地交作业。我必须感谢 Philipp Mehrwald 的简单解决方案。
    • 是的,没错,我为此给了他+1。您不应该在不同的线程中访问 GUI。
    【解决方案2】:

    尝试为此使用摇摆工人。此可运行对象将在 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 );
       }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-22
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多