我在我的项目中在Mac上的netbeans上创建了一个jform,我通过拖放自动创建了他,`我放了三个按钮和一个jtextarea。 在项目的主体中,我创建一个与此jform类型的新对象。 jform出现,但jtextarea中的文本不出现。
netbeans会像这样自动创建initcomponent:
private void initComponents() {
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jButton4 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jButton1.setText("reply");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jButton2.setText("reply-all");
jButton3.setText("delete");
jButton4.setText("forward");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 363, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jButton3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton4)))
.addContainerGap(23, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2)
.addComponent(jButton3)
.addComponent(jButton4))
.addGap(18, 18, 18)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 232, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(15, Short.MAX_VALUE))
);
pack();
}
我这样创建构造函数:
public MailRead(Email a){
java.awt.EventQueue.invokeLater(() -> {
new MailRead().setVisible(true);
});
Date d=new Date();
this.a=a;
a=new Email("ffd", "edgvxsfz", "h6v4gtrwd","ujyhrstegfw", d,21334231);
initComponents();
//System.out.println("-----Email from : "+a.getMittente()+"-----"+"\n"+"-----Object : "+a.getObj()+"-----\n"+a.getEmail());
String text= "-----Email from : "+a.getMittente()+"-----"+"\n"+"-----Object : "+a.getObj()+"-----\n"+a.getEmail();
jTextArea1=new JTextArea(text);
}
电子邮件代码是这样的:
import java.io.Serializable;
import java.util.Date;
public class Email implements Serializable{
String mittente;
String destinatario;
String email;
Date dataInvio;
int id;
String obj;
boolean isnew;
public Email(String mittente, String destinatario,String obj, String email, Date dataInvio,int id) {
this.mittente = mittente;
this.destinatario = destinatario;
this.email = email;
this.dataInvio = dataInvio;
this.id=id;
this.obj=obj;
isnew=true;
}
public String getMittente() {
return mittente;
}
public void setMittente(String mittente) {
this.mittente = mittente;
}
public String getDestinatario() {
return destinatario;
}
public void setDestinatario(String destinatario) {
this.destinatario = destinatario;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getObj() {
return obj;
}
public void setObj(String obj) {
this.obj = obj;
}
public Date getDataInvio() {
return dataInvio;
}
public void setDataInvio(Date dataInvio) {
this.dataInvio = dataInvio;
}
public int getId() {
return id;
}
public boolean isIsnew() {
return isnew;
}
public void setIsnew(boolean isnew) {
this.isnew = isnew;
}
@Override
public String toString() {
return "Email{" + "mittente=" + mittente + ", destinatario=" + destinatario + ", email=" + email + ", dataInvio=" + dataInvio + ", id=" + id + '}';
}
} 在承包商中,我现在在实际代码中分配了值,我使用MailRead的参数 如何解决在jtextarea中阅读电子邮件?
答案 0 :(得分:0)
jTextArea1=new JTextArea(text);
不要创建新的JTextArea。
您要向已添加到框架的现有文本区域添加文本。因此,您将使用:
jTextArea1.append(text);
这意味着在创建MailRead
类时,需要将文本区域作为参数传递给MailRead class as well as the Email parameter so your
MailRead`类,以便将文本追加到文本区域。