簡體   English   中英

無法將文本從單獨的類追加到JTextArea-Java

[英]Unable to append text from a separate class to a JTextArea - Java

我目前遇到麻煩(如標題所述),將文本從單獨的類(或線程)追加到JTextArea。

這是我的代碼,因為我不知道是什么引起了問題。 (請注意,我正在使用Netbeans為我的GUI生成的代碼):

聊天框GUI:

public class ChatBoxGUI extends javax.swing.JFrame
{
/**
 * Creates new form ChatBoxGUI
 * @return 
 */
public javax.swing.JTextArea getTextArea() 
{
    return inputOutputTextArea; 
}

public void setLabels(java.net.SocketAddress address, int port) {
    this.addressLabel.setText("IP Address: " +address);
    this.portLabel.setText("Port: " +Integer.toString(port)); 
}

public ChatBoxGUI() {
    initComponents();
}

/**
 * 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() {

    jPanel1 = new javax.swing.JPanel();
    startServerButton = new javax.swing.JButton();
    connectServerButton = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    addressLabel = new javax.swing.JLabel();
    portLabel = new javax.swing.JLabel();
    outputTextField = new javax.swing.JTextField();
    stopButton = new javax.swing.JButton();
    jScrollPane2 = new javax.swing.JScrollPane();
    inputOutputTextArea = new javax.swing.JTextArea();

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 100, Short.MAX_VALUE)
    );
    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 100, Short.MAX_VALUE)
    );

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    startServerButton.setText("Start Server");
    startServerButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            startServerButtonActionPerformed(evt);
        }
    });

    connectServerButton.setText("Connect to Server");
    connectServerButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            connectServerButtonActionPerformed(evt);
        }
    });

    jLabel1.setFont(new java.awt.Font("Lucida Grande", 1, 14)); // NOI18N
    jLabel1.setText("Isaac's Chat Box");

    addressLabel.setText("IP Address:");

    portLabel.setText("Port: ");

    stopButton.setText("Stop");
    stopButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            stopButtonActionPerformed(evt);
        }
    });

    inputOutputTextArea.setEditable(false);
    inputOutputTextArea.setColumns(20);
    inputOutputTextArea.setRows(5);
    jScrollPane2.setViewportView(inputOutputTextArea);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jScrollPane2)
                .addComponent(outputTextField, javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(startServerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(connectServerButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(addressLabel))
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(stopButton)
                        .addGroup(layout.createSequentialGroup()
                            .addGap(6, 6, 6)
                            .addComponent(portLabel)))
                    .addGap(0, 18, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(startServerButton)
                .addComponent(jLabel1)
                .addComponent(stopButton))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(connectServerButton)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addressLabel)
                    .addComponent(portLabel)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 168, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(outputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );

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

private void startServerButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                  
    CreateServerDialog createServerDialog = new CreateServerDialog(this, true);
    createServerDialog.setVisible(true);

    try {
        Runnable serverClassRunnable = new ServerClass(createServerDialog.getPort()); 
        Thread serverClassThread = new Thread(serverClassRunnable);
        serverClassThread.start();
    } catch (java.io.IOException e) {
        e.printStackTrace(); 
    }
}                                                 

private void connectServerButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                    

}                                                   

private void stopButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

}                                          

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(ChatBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(ChatBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(ChatBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(ChatBoxGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new ChatBoxGUI().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel addressLabel;
private javax.swing.JButton connectServerButton;
private javax.swing.JTextArea inputOutputTextArea;
private javax.swing.JLabel jLabel1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane2;
private javax.swing.JTextField outputTextField;
private javax.swing.JLabel portLabel;
private javax.swing.JButton startServerButton;
private javax.swing.JButton stopButton;
// End of variables declaration                   
}

創建服務器對話框:

public class CreateServerDialog extends javax.swing.JDialog {

private int port; private boolean isValid = false; private java.awt.Frame parent; 

public int getPort() {
    return port; 
}
/**
 * Creates new form CreateServerDialog
 */
public CreateServerDialog(java.awt.Frame par, boolean modal) {
    super(par, modal);
    parent = par; 
    initComponents();
}

/**
 * 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() {

    jLabel1 = new javax.swing.JLabel();
    portInput = new javax.swing.JTextField();
    okButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    jLabel1.setText("Enter Port:");

    okButton.setText("Ok");
    okButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            okButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addComponent(okButton)
            .addGap(0, 0, Short.MAX_VALUE))
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(portInput)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addGap(0, 0, Short.MAX_VALUE)))
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jLabel1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(portInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(okButton))
    );

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

private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    ErrorDialog errorDialog = new ErrorDialog(parent, true);

    try {
        port = Integer.parseInt(this.portInput.getText()); 
        isValid = true; 
    } catch (NumberFormatException n) {
        errorDialog.errorOccured(0); 
        isValid = false; 
    }

    if (port <= 1024 || port > 65535) {
        errorDialog.errorOccured(0);
        isValid = false; 
    }

    if (isValid == true) {
       this.dispose();
    }
}                                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */
    try {
        for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                javax.swing.UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (ClassNotFoundException ex) {
        java.util.logging.Logger.getLogger(CreateServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
        java.util.logging.Logger.getLogger(CreateServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
        java.util.logging.Logger.getLogger(CreateServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        java.util.logging.Logger.getLogger(CreateServerDialog.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    //</editor-fold>

    /* Create and display the dialog */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            CreateServerDialog dialog = new CreateServerDialog(new javax.swing.JFrame(), true);
            dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                @Override
                public void windowClosing(java.awt.event.WindowEvent e) {
                    System.exit(0);
                }
            });
            dialog.setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JLabel jLabel1;
private javax.swing.JButton okButton;
private javax.swing.JTextField portInput;
// End of variables declaration                   
}

服務器類別:

import java.io.*;
import java.net.*;

public class ServerClass implements Runnable
{
private final ServerSocket serverSocket; 

public ServerClass(int port) throws IOException
{
    serverSocket = new ServerSocket(port); 
}

public void run() 
{
    ChatBoxGUI chatBoxGUI = new ChatBoxGUI();  
    try {
        chatBoxGUI.getTextArea().append("\nSearching for client connection. \nPort: " +serverSocket.getLocalPort()); //this is the code that isn't working

        Socket server = serverSocket.accept(); 

        System.out.println();

        chatBoxGUI.setLabels(server.getLocalSocketAddress(), serverSocket.getLocalPort()); //this also isn't working - but that's not the focus of this question

        DataOutputStream out = new DataOutputStream(server.getOutputStream());
        DataInputStream in = new DataInputStream(server.getInputStream());

        chatBoxGUI.getTextArea().append("\nClient found! Connected with: " +server.getRemoteSocketAddress()); 

        //note: this part isn't finished - but it doesn't need to be for that code to work
    } catch (IOException e) {
        e.printStackTrace(); 
    }
}

}

我已經嘗試對這個問題進行研究,發現的發現告訴我,在運行代碼時必須使用invokeLater或invokeAndWait才能使其在EDT中運行。 而且我已經嘗試實現了這一點,但它似乎仍然不起作用:

javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                chatBoxGUI.getTextArea().append("\nSearching for client connection. \nPort: " +serverSocket.getLocalPort());
            }
});

與終端的類似輸出完全可以正常工作,因此添加文本絕對是一個問題。

任何幫助將不勝感激,並在此先感謝。

您具有ChatBoxGUI類的多個實例,因此您不會更新在桌面上可見的文本區域。

因此,首先創建ChatBoxGUI類。 然后,當您單擊按鈕時,將創建ServerClass 在ServerClass中,創建ChatBoxGUI的第二個實例。

不要創建ChatBoxGUI類的第二個實例!!!

相反,如果要訪問ChatBoxGUI類中的變量,則需要:

  1. 將ChatBoxGUI類的引用傳遞給ServerClass
  2. 將此引用另存為ServerClass中的實例變量
  3. 要將文本添加到文本區域時,請訪問此變量。

如果沒有更完整的示例,我所能做的就是將您鏈接到這個問題該問題可以為類似的問題提供答案。

或者,您可以嘗試從以下方法更改方法:

public javax.swing.JTextArea getTextArea() 
{
     return inputOutputTextArea; 
}

至:

public void setTextArea(String textToAppend) 
{
    inputOutputTextArea.append(textToAppend); 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM