文本框不会从单独的表单中追加

时间:2016-07-26 13:44:48

标签: java swing append

我正在为我已经制作的应用程序构建GUI /控制台。我试图让一个类附加一个单独的表单类的JTextArea。在这里搜索过 - 尝试制作JTextArea' public static'并使用FormName.JtextareaName.append("");哪个不行。我将在下面发布我的代码以供参考;

ZBC.java

subtA

FORM2.java

package com.company;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.sql.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
* Created by bboothman on 26/07/2016.
 */

public class ZBC implements Runnable
{
@Override
public void run()
{
    while (true)
    {
        // Connect & Select SQL
        String connectionString = "jdbc:sqlserver://:1433;database=;user=;password=!;";
        String SQL = "SELECT [PK_PrintQueueID],[FK_PrinterID],[FK_BarcodeTypeID],[Barcode],[Quantity],[QueueDate],[ProcessedDate] FROM [Brad].[dbo].[PrintQueue] WHERE ProcessedDate IS NULL";
        Connection connection = null;
        // Time & Date
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        try
        {
            connection = DriverManager.getConnection(connectionString);
            Statement stmt = connection.createStatement();
            Statement stmt2;
            ResultSet rs = stmt.executeQuery(SQL);
            while (rs.next())
            {
                String FK_BarcodeTypeID = rs.getString("FK_BarcodeTypeID");
                String barcode = rs.getString("Barcode");
                String[] parts = barcode.split("-");
                String part1 = parts[0];
                String SQL2 = "UPDATE PrintQueue SET ProcessedDate = '" + dateFormat.format(date) + "' WHERE PK_PrintQueueID = '" + rs.getString("PK_PrintQueueID") + "'";
                stmt2 = connection.createStatement();
                stmt2.executeUpdate(SQL2);

                if (FK_BarcodeTypeID.equals("1"))
                {
                    // Type 128 barcode.
                    String zpl = "^XA^BY2,3,140^FT80,200^BCN,Y,N,N^FD>:" + rs.getString("Barcode") + "^FS^FT200,250^A0N,42,40^FH^FD" + part1 + "^FS^XZ";
                    //printlabel(zpl);
                    System.out.println("New serialized barcode added.\nPrinting: " + (rs.getString("Barcode")));
                    System.out.println("Process date: " + dateFormat.format(date) + ".\n");
                    Form2.jtaConsole.append("TEST");

                } else
                {
                    // Type 39 barcode.
                    String zpl = "CT~~CD,~CC^~CT~ ^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR4,4~SD15^JUS^LRN^CI0^XZ^XA^MMT^PW674^LL0376 ^LS0 ^BY2,3,151^FT84,249^BCN,,Y,N^FD>:" + rs.getString("Barcode") + "^FS ^PQ1,0,1,Y^XZ";
                    //printlabel(zpl);
                    System.out.println("New un-serialized barcode added.\nPrinting: " + (rs.getString("Barcode")));
                    System.out.println("Process date: " + dateFormat.format(date) + ".\n");
                }
            }
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        try
        {
            Thread.sleep(3000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        System.out.println("Checking for new barcode..");
    }
}
public void printlabel(String zpl)
{
    try
    {
        Socket clientSocket;
        clientSocket = new Socket(IP,PORT);
        DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
        outToServer.writeBytes(zpl);
        clientSocket.close();
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
}

}

当我尝试上面这样做时会抛出错误;

 package com.company;
 import javax.swing.*;

class Form2
{
//Declare GUI items
public JTextArea jtaConsole;
public JPanel Jframer;
public JButton stopButton;
public JButton startButton;

public Form2(String message)
{
    JFrame frame = new JFrame("Zebra Print");
    frame.setContentPane(this.Jframer);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setResizable(true);
    frame.setVisible(true);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);

    jtaConsole.append("//////////// CONSOLE ////////////\n\n");
    jtaConsole.append("- User selected: " + message + "\n");
    jtaConsole.append("- IP: "+IP1+"\n");
    jtaConsole.append("- PORT: "+PORT1+"\n\n");

    startButton.addActionListener(e ->
    {
        jtaConsole.append("- Program is now running..\n\n");
        //Starts the ZBC class on a new thread
        ZBC ZBC = new ZBC();
        Thread t1 = new Thread(ZBC);
        t1.start();
    });
    stopButton.addActionListener(e ->
    {
        //Needs to pause or stop execution/ thread
    });
}

这是我尝试从另一个类附加textarea的正确方法吗?

这甚至可能吗?

由于

编辑;已添加完整源代码。

0 个答案:

没有答案