2014-10-09 46 views
0

我想从NetBeans中的全局字符串中填充文本的jLabel,但每次我尝试打印除字符串字面以外的任何内容时,它都不会打印任何内容。我试过直接打印字符串,而我最近的尝试使用get()方法。jLabel不会打印字符串

下面的代码,到目前为止,我填充线73标签:

public class ChatWindow extends javax.swing.JFrame { 

    private Integer userID; 
    private String otherPerson = ""; 

    /** 
    * Creates new form ChatWindow 
    * @param id 
    */ 
    public ChatWindow(int id) { 
     initComponents(); 
     userID = id; 
     otherPerson = userID.toString(); 
     this.setVisible(true); 
    } 

    /** 
    * This method is called from within the constructor to initialize the form. 
    * WARNING: Do NOT modify this code. The content of this method is always 
    * regenerated by the Form Editor. 
    */ 
    @SuppressWarnings("unchecked") 
    // <editor-fold defaultstate="collapsed" desc="Generated Code">       
    private void initComponents() { 

     jScrollPane1 = new javax.swing.JScrollPane(); 
     jTextArea1 = new javax.swing.JTextArea(); 
     jButton1 = new javax.swing.JButton(); 
     jButton2 = new javax.swing.JButton(); 
     jTextField1 = new javax.swing.JTextField(); 
     otherPersonLabel = new javax.swing.JLabel(); 
     menuBar = new javax.swing.JMenuBar(); 
     fileMenu = new javax.swing.JMenu(); 
     openMenuItem = new javax.swing.JMenuItem(); 
     saveMenuItem = new javax.swing.JMenuItem(); 
     saveAsMenuItem = new javax.swing.JMenuItem(); 
     exitMenuItem = new javax.swing.JMenuItem(); 
     editMenu = new javax.swing.JMenu(); 
     cutMenuItem = new javax.swing.JMenuItem(); 
     copyMenuItem = new javax.swing.JMenuItem(); 
     pasteMenuItem = new javax.swing.JMenuItem(); 
     deleteMenuItem = new javax.swing.JMenuItem(); 
     helpMenu = new javax.swing.JMenu(); 
     contentsMenuItem = new javax.swing.JMenuItem(); 
     aboutMenuItem = new javax.swing.JMenuItem(); 

     setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE); 

     jTextArea1.setBackground(new java.awt.Color(248, 248, 248)); 
     jTextArea1.setColumns(20); 
     jTextArea1.setRows(5); 
     jTextArea1.setText("ContactName: FloopyNoops\nYou: Yes, floopynoops"); 
     jScrollPane1.setViewportView(jTextArea1); 

     jButton1.setText("Send"); 

     jButton2.setText("Save"); 

     jTextField1.setText("jTextField1"); 

     otherPersonLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N 
     otherPersonLabel.setText(getLabelText()); 

     fileMenu.setMnemonic('f'); 
     fileMenu.setText("File"); 

     openMenuItem.setMnemonic('o'); 
     openMenuItem.setText("Open"); 
     fileMenu.add(openMenuItem); 

     saveMenuItem.setMnemonic('s'); 
     saveMenuItem.setText("Save"); 
     fileMenu.add(saveMenuItem); 

     saveAsMenuItem.setMnemonic('a'); 
     saveAsMenuItem.setText("Save As ..."); 
     saveAsMenuItem.setDisplayedMnemonicIndex(5); 
     fileMenu.add(saveAsMenuItem); 

     exitMenuItem.setMnemonic('x'); 
     exitMenuItem.setText("Exit"); 
     exitMenuItem.addActionListener(new java.awt.event.ActionListener() { 
      public void actionPerformed(java.awt.event.ActionEvent evt) { 
       exitMenuItemActionPerformed(evt); 
      } 
     }); 
     fileMenu.add(exitMenuItem); 

     menuBar.add(fileMenu); 

     editMenu.setMnemonic('e'); 
     editMenu.setText("Edit"); 

     cutMenuItem.setMnemonic('t'); 
     cutMenuItem.setText("Cut"); 
     editMenu.add(cutMenuItem); 

     copyMenuItem.setMnemonic('y'); 
     copyMenuItem.setText("Copy"); 
     editMenu.add(copyMenuItem); 

     pasteMenuItem.setMnemonic('p'); 
     pasteMenuItem.setText("Paste"); 
     editMenu.add(pasteMenuItem); 

     deleteMenuItem.setMnemonic('d'); 
     deleteMenuItem.setText("Delete"); 
     editMenu.add(deleteMenuItem); 

     menuBar.add(editMenu); 

     helpMenu.setMnemonic('h'); 
     helpMenu.setText("Help"); 

     contentsMenuItem.setMnemonic('c'); 
     contentsMenuItem.setText("Contents"); 
     helpMenu.add(contentsMenuItem); 

     aboutMenuItem.setMnemonic('a'); 
     aboutMenuItem.setText("About"); 
     helpMenu.add(aboutMenuItem); 

     menuBar.add(helpMenu); 

     setJMenuBar(menuBar); 

     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.LEADING) 
        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE) 
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() 
         .addComponent(jTextField1) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
          .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
          .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(otherPersonLabel) 
         .addGap(0, 0, Short.MAX_VALUE))) 
       .addContainerGap()) 
     ); 
     layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 
      .addGroup(layout.createSequentialGroup() 
       .addGap(5, 5, 5) 
       .addComponent(otherPersonLabel) 
       .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 
       .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 202, javax.swing.GroupLayout.PREFERRED_SIZE) 
       .addGap(6, 6, 6) 
       .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) 
        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE) 
        .addGroup(layout.createSequentialGroup() 
         .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE) 
         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
         .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 
     ); 

     pack(); 
    }// </editor-fold>       

    private void exitMenuItemActionPerformed(java.awt.event.ActionEvent evt) {            
     this.dispose(); 
    }            

    private String getLabelText(){ 
     return otherPerson; 
    } 

    // Variables declaration - do not modify      
    private javax.swing.JMenuItem aboutMenuItem; 
    private javax.swing.JMenuItem contentsMenuItem; 
    private javax.swing.JMenuItem copyMenuItem; 
    private javax.swing.JMenuItem cutMenuItem; 
    private javax.swing.JMenuItem deleteMenuItem; 
    private javax.swing.JMenu editMenu; 
    private javax.swing.JMenuItem exitMenuItem; 
    private javax.swing.JMenu fileMenu; 
    private javax.swing.JMenu helpMenu; 
    private javax.swing.JButton jButton1; 
    private javax.swing.JButton jButton2; 
    private javax.swing.JScrollPane jScrollPane1; 
    private javax.swing.JTextArea jTextArea1; 
    private javax.swing.JTextField jTextField1; 
    private javax.swing.JMenuBar menuBar; 
    private javax.swing.JMenuItem openMenuItem; 
    private javax.swing.JLabel otherPersonLabel; 
    private javax.swing.JMenuItem pasteMenuItem; 
    private javax.swing.JMenuItem saveAsMenuItem; 
    private javax.swing.JMenuItem saveMenuItem; 
    // End of variables declaration     

} 
+0

这是很多不相关的代码,你期望志愿者通过。为了更快地获得更好的帮助,请发布证明问题的[最小,完整,可验证示例](http://stackoverflow.com/help/mcve)。 – splungebob 2014-10-09 19:48:03

回答

0

我相信构造是你的问题。

public ChatWindow(int id) { 
    initComponents(); //otherPerson = ""; 
    userID = id; 
    otherPerson = userID.toString(); //otherPerson = id; 
    this.setVisible(true); 
} 

您在otherPerson设置之前初始化组件。设置otherPerson后移动initComponents,它应该再次工作。

+0

他实际上从来没有实例化ChatWindow。如果您从不创建otherPerson设置的对象,顺序无关紧要。 – MarsAtomic 2014-10-09 19:48:55

+0

谢谢,就是这样!肯定会飞过我。 – 2014-10-09 19:49:20

+0

@MarsAtomic他可以从一个HomeWindow或从另一个类的主要方法实例化这个。 – Compass 2014-10-09 19:51:27

0

您首先调用“initcomponent”,然后初始化“otherPerson”类成员。这样做:

public ChatWindow(int id) 
{ 
    userID = id; 
    otherPerson = userID.toString(); 

    initComponents(); 
    this.setVisible(true); 
}