简体   繁体   English

为什么JTextArea.append()不显示任何内容?

[英]why JTextArea.append() doesn't display anything?

I have two classes: 我有两节课:

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

I would like to print "textLine" in "listadoTextArea" but it doesn't display nothing. 我想在“ listadoTextArea”中打印“ textLine”,但不显示任何内容。 If I use System.out.println(textLine) instead of listadoTextArea.append(textLine) , the console displays "textLine" correctly. 如果我使用System.out.println(textLine)而不是listadoTextArea.append(textLine) ,则控制台会正确显示“ textLine”。 So, I don't know where is the error. 所以,我不知道错误在哪里。


I'm sorry, the most part of the code is here. 抱歉,大部分代码在这里。

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

public class MetodosFicheros
{
public MetodosFicheros(){}


private VentanaPrincipal objVentana;
private String dialogo = "No se ha encontrado el fichero";
private String fileName = "FicherosDatos.dat";
private JTextField JTextField;
private JTextArea JTextArea;


/****************************************************************************************
**Metodo que graba en un fichero ".dat" los valores introducidos en la interfaz grafica**
*****************************************************************************************/
public void grabarFichero(String clave, String nombre, int edad, float sueldo)
{
    objVentana = new VentanaPrincipal();
    DataOutputStream fichero = new DataOutputStream(new FileOutputStream(fileName, true));
    fichero.writeUTF(clave);
    fichero.writeUTF(nombre);
    fichero.writeInt(edad);
    fichero.writeFloat(sueldo);
}

public void leerFichero()
{
    String textLine;
    JTextArea = new JTextArea();
    FileReader fichero = null;
    BufferedReader reader;
    objVentana = new VentanaPrincipal();
    fichero = new FileReader(fileName);
    reader = new BufferedReader(fichero);
    while((textLine = reader.readLine()) != null)
    {
       objVentana.ListadoTextArea(textLine);
    }
  }
}

A guess, perhaps your problem is due to changing the state of the wrong reference -- that you may have create a second object, and are changing the state of the 2nd object, not the currently displayed one. 一个猜测,也许您的问题是由于更改了错误引用的状态所致-您可能已经创建了第二个对象,并且正在更改第二个对象的状态,而不是当前显示的对象的状态。

Key here will be the objVentana variable below: 关键是下面的objVentana变量:

public class MetodosFicheros
{
    private VentanaPrincipal objVentana;

    public void leerFichero()
    {
        String textLine;
        objVentana.ListadoTextArea(textLine);
    }
}

How do you assign it a reference? 您如何为其分配参考? Are you 100% sure that it is in fact referring to the displayed VentanaPrincipal, or is there somewhere in the code not shown a call to new VentanaPrincipal() that creates a new reference, and that you're calling methods on that. 您是否100%确定实际上是在指所显示的VentanaPrincipal,还是代码中未显示的地方调用了创建新引用的new VentanaPrincipal() ,并且您正在该方法上进行调用。 I'll bet that this is exactly what you're doing. 我敢打赌,这正是您在做什么。

But more importantly, regardless of whether I'm right or wrong, you should strive to improve your question as it is missing key information that would allow us to help you without having to guess. 但更重要的是,无论我是对还是错,您都应努力改善您的问题,因为它缺少关键信息,这些关键信息将使我们无需猜测即可为您提供帮助。


Edit 编辑
Yep, I'm right. 是的,我是对的。 As I mentioned, all you have to do is search your code for new VentanaPrincipal() , and any time you see that, you know that you're creating a new VentanaPrincipal window object, one that is completely distinct from the one that's being displayed: 正如我提到的,您要做的就是在代码中搜索new 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);
    }
  }
}

The solution is not to create a new VentanaPrincipal, but instead to pass into your MetodosFicheros object a reference to the currently viewed VentanaPrincipal object, and then call methods on it. 解决方案不是创建新的VentanaPrincipal,而是将对当前查看的VentanaPrincipal对象的引用传递到您的MetodosFicheros对象中,然后在其上调用方法。

So change MetodosFicheros to 因此将MetodosFicheros更改为

public class MetodosFicheros {
   private VentanaPrincipal objVentana;

   public MetodosFicheros(VentanaPrincipalobjVentana) {
      this.objVentana = objVentana;
   }

pass in the appropriate VentanaPrincipal when calling the above constructor, 在调用上述构造函数时传递适当的VentanaPrincipal,

private MetodosFicheros objMetodos = new MetodosFicheros(this);

and don't create any new VentanaPrincipal objects. 并且不要创建任何新的VentanaPrincipal对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM