Java 为什么JTextArea.append()不';我什么也没展示?
我有两门课:Java 为什么JTextArea.append()不';我什么也没展示?,java,swing,jtextarea,Java,Swing,Jtextarea,我有两门课: public class VentanaPrincipal extends javax.swing.JFrame { MetodosFicheros objMetodosFicheros; public void ListadoTextArea(String textLine) { listadoTextArea.append(textLine); } private void datosButtonActi
public class VentanaPrincipal extends javax.swing.JFrame
{
MetodosFicheros objMetodosFicheros;
public void ListadoTextArea(String textLine)
{
listadoTextArea.append(textLine);
}
private void datosButtonActionPerformed(java.awt.event.ActionEvent evt)
{
objMetodos = new MetodosFicheros();
objMetodos.leerFichero();
}
}
public class MetodosFicheros
{
private VentanaPrincipal objVentana;
public void leerFichero()
{
String textLine;
objVentana.ListadoTextArea(textLine);
}
}
我想在“listadoTextArea”中打印“textLine”,但它不会显示任何内容。如果我使用System.out.println(textLine)
而不是listadoTextArea.append(textLine)
,控制台将正确显示“textLine”。所以,我不知道错误在哪里
对不起,大部分代码都在这里
public class VentanaPrincipal extends javax.swing.JFrame
{
private String clave;
private String nombre;
private int edad;
private float sueldo;
private MetodosFicheros objMetodos;
public VentanaPrincipal()
{
initComponents();
}
public void ListadoTextArea(String textLine)
{
listadoTextArea.append(textLine);
}
private void datosButtonActionPerformed(java.awt.event.ActionEvent evt {
objMetodos = new MetodosFicheros();
objMetodos.leerFichero();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
claveText = new javax.swing.JTextField();
edadText = new javax.swing.JTextField();
sueldoText = new javax.swing.JTextField();
nombreText = new javax.swing.JTextField();
grabarButton = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
datosButton = new javax.swing.JButton();
indicesButton = new javax.swing.JButton();
indicesDatosButton = new javax.swing.JButton();
ordenarButton = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
listadoTextArea = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
jLabel6 = new javax.swing.JLabel();
claveBuscarText = new javax.swing.JTextField();
indiceSinOrdenarButton = new javax.swing.JButton();
indiceOrdenadoButton = new javax.swing.JButton();
busquedaDicotomica = new javax.swing.JButton();
jLabel7 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
nombreBuscarText = new javax.swing.JTextField();
edadBuscarText = new javax.swing.JTextField();
sueldoBuscarText = new javax.swing.JTextField();
registrosText = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
indiceText = new javax.swing.JTextField();
errorText = new javax.swing.JTextField();
}
// Variables declaration - do not modify
private javax.swing.JButton busquedaDicotomica;
private javax.swing.JTextField claveBuscarText;
private javax.swing.JTextField claveText;
private javax.swing.JButton datosButton;
private javax.swing.JTextField edadBuscarText;
private javax.swing.JTextField edadText;
private javax.swing.JTextField errorText;
private javax.swing.JButton grabarButton;
private javax.swing.JButton indiceOrdenadoButton;
private javax.swing.JButton indiceSinOrdenarButton;
private javax.swing.JTextField indiceText;
private javax.swing.JButton indicesButton;
private javax.swing.JButton indicesDatosButton;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea listadoTextArea;
private javax.swing.JTextField nombreBuscarText;
private javax.swing.JTextField nombreText;
private javax.swing.JButton ordenarButton;
private javax.swing.JTextField registrosText;
private javax.swing.JTextField sueldoBuscarText;
private javax.swing.JTextField sueldoText;
}
猜测一下,您的问题可能是由于更改了错误引用的状态——您可能创建了第二个对象,并且正在更改第二个对象的状态,而不是当前显示的对象的状态 此处的键是下面的objVentana变量:
public class MetodosFicheros
{
private VentanaPrincipal objVentana;
public void leerFichero()
{
String textLine;
objVentana.ListadoTextArea(textLine);
}
}
您如何为其分配参考?您是否100%确定它实际上引用的是显示的VentanaPrincipal,或者在代码中的某个地方没有显示对new VentanaPrincipal()
的调用,该调用创建了一个新引用,并且您正在调用该引用的方法。我敢打赌这正是你在做的
但更重要的是,无论我是对是错,你都应该努力改进你的问题,因为它缺少关键信息,我们可以帮助你,而无需猜测
编辑
是的,我是对的。正如我所提到的,您所要做的就是在代码中搜索
新的VentanaPrincipal()
,当您看到它时,您就知道您正在创建一个新的VentanaPrincipal窗口对象,它与正在显示的对象完全不同:
public void leerFichero()
{
String textLine;
JTextArea = new JTextArea();
FileReader fichero = null;
BufferedReader reader;
objVentana = new VentanaPrincipal(); // ********* here **********
fichero = new FileReader(fileName);
reader = new BufferedReader(fichero);
while((textLine = reader.readLine()) != null)
{
objVentana.ListadoTextArea(textLine);
}
}
}
解决方案不是创建一个新的VentanaPrincipal,而是将对当前查看的VentanaPrincipal对象的引用传递到MetodosFicheros对象中,然后对其调用方法
因此,将Metodosfichers更改为
public class MetodosFicheros {
private VentanaPrincipal objVentana;
public MetodosFicheros(VentanaPrincipalobjVentana) {
this.objVentana = objVentana;
}
调用上述构造函数时,传入相应的VentanaPrincipal
private MetodosFicheros objMetodos = new MetodosFicheros(this);
不要创建任何新的VentanaPrincipal对象。您的代码片段并不能说明全部情况。通常,此问题是由于更改了错误引用的状态造成的——您可能创建了第二个对象,并且正在更改第二个对象的状态,而不是当前显示的对象的状态。请出示更多,最好是一张。@Amaro88:Ha sido un placer。
private MetodosFicheros objMetodos = new MetodosFicheros(this);