Java 从另一个类追加jtextarea
我正在制作一个java应用程序,它总共使用了4个类,Java 从另一个类追加jtextarea,java,swing,append,jtextarea,Java,Swing,Append,Jtextarea,我正在制作一个java应用程序,它总共使用了4个类,Main,Form1,Form2和ZBC。我试图通过ZBC类中的一些代码来附加JTextArea,它位于Form2上 第一个问题是,当尝试附加JTextArea时,我会收到一个错误,因为JTextArea不是static——如果我将JTextArea更改为static,我也会收到一个错误 我试图使用Form2.jtaConsole.append(“TEST”)附加文本框因为我以前就是这样做的 有没有办法从另一个类中附加这个文本区域 我将在下面发
Main
,Form1
,Form2
和ZBC
。我试图通过ZBC
类中的一些代码来附加JTextArea
,它位于Form2
上
第一个问题是,当尝试附加JTextArea
时,我会收到一个错误,因为JTextArea
不是static
——如果我将JTextArea
更改为static
,我也会收到一个错误
我试图使用Form2.jtaConsole.append(“TEST”)附加文本框代码>因为我以前就是这样做的
有没有办法从另一个类中附加这个文本区域
我将在下面发布每个类的代码
欢迎任何帮助
Main.java
package com.company;
public class Main
{
public static void main(String[] args)
{
Form1 f1 = new Form1();
f1.open();
}
}
package com.company;
import javax.swing.*;
public class Form1
{
//Declare GUI items
private JComboBox comboBox1;
private JButton goButton;
private JPanel jp_one;
public Form1()
{
comboBox1.addActionListener(e ->
{
Object selected **= comboBox1.getSelectedItem().toString();
});
goButton.addActionListener(e ->
{
Form2 f2 = new Form2(comboBox1.getSelectedItem().toString());
});
}
public static void open()
{
JFrame frame = new JFrame("Zebra Print");
frame.setContentPane(new Form1().jp_one);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.pack();
frame.setLocationRelativeTo(null);
}
}
package com.company;
import javax.swing.*;
class Form2
{
//Declare GUI items
public JTextArea jtaConsole;
public JPanel Jframer;
public JButton stopButton;
public JButton startButton;
String IP1 = "172.20.9.61";
int PORT1 = 9100;
ZBC ZBC = new ZBC();
Thread t1 = new Thread(ZBC);
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
t1.start();
});
stopButton.addActionListener(e ->
jtaConsole.append("- Program is now paused..\n\n"));
}
}
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;
public class ZBC implements Runnable
{
@Override
public void run()
{
while (true)
{
// Connect & Select SQL
String connectionString = "jdbc:sqlserver://:;database=;user=;password=;";
String SQL = "SELECT [PK_PrintQueueID],[FK_PrinterID],[FK_BarcodeTypeID],[Barcode],[Quantity],[QueueDate],[ProcessedDate] FROM [X].[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();
}
}
}
Form1.java
package com.company;
public class Main
{
public static void main(String[] args)
{
Form1 f1 = new Form1();
f1.open();
}
}
package com.company;
import javax.swing.*;
public class Form1
{
//Declare GUI items
private JComboBox comboBox1;
private JButton goButton;
private JPanel jp_one;
public Form1()
{
comboBox1.addActionListener(e ->
{
Object selected **= comboBox1.getSelectedItem().toString();
});
goButton.addActionListener(e ->
{
Form2 f2 = new Form2(comboBox1.getSelectedItem().toString());
});
}
public static void open()
{
JFrame frame = new JFrame("Zebra Print");
frame.setContentPane(new Form1().jp_one);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.pack();
frame.setLocationRelativeTo(null);
}
}
package com.company;
import javax.swing.*;
class Form2
{
//Declare GUI items
public JTextArea jtaConsole;
public JPanel Jframer;
public JButton stopButton;
public JButton startButton;
String IP1 = "172.20.9.61";
int PORT1 = 9100;
ZBC ZBC = new ZBC();
Thread t1 = new Thread(ZBC);
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
t1.start();
});
stopButton.addActionListener(e ->
jtaConsole.append("- Program is now paused..\n\n"));
}
}
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;
public class ZBC implements Runnable
{
@Override
public void run()
{
while (true)
{
// Connect & Select SQL
String connectionString = "jdbc:sqlserver://:;database=;user=;password=;";
String SQL = "SELECT [PK_PrintQueueID],[FK_PrinterID],[FK_BarcodeTypeID],[Barcode],[Quantity],[QueueDate],[ProcessedDate] FROM [X].[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();
}
}
}
Form2.java
package com.company;
public class Main
{
public static void main(String[] args)
{
Form1 f1 = new Form1();
f1.open();
}
}
package com.company;
import javax.swing.*;
public class Form1
{
//Declare GUI items
private JComboBox comboBox1;
private JButton goButton;
private JPanel jp_one;
public Form1()
{
comboBox1.addActionListener(e ->
{
Object selected **= comboBox1.getSelectedItem().toString();
});
goButton.addActionListener(e ->
{
Form2 f2 = new Form2(comboBox1.getSelectedItem().toString());
});
}
public static void open()
{
JFrame frame = new JFrame("Zebra Print");
frame.setContentPane(new Form1().jp_one);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.pack();
frame.setLocationRelativeTo(null);
}
}
package com.company;
import javax.swing.*;
class Form2
{
//Declare GUI items
public JTextArea jtaConsole;
public JPanel Jframer;
public JButton stopButton;
public JButton startButton;
String IP1 = "172.20.9.61";
int PORT1 = 9100;
ZBC ZBC = new ZBC();
Thread t1 = new Thread(ZBC);
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
t1.start();
});
stopButton.addActionListener(e ->
jtaConsole.append("- Program is now paused..\n\n"));
}
}
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;
public class ZBC implements Runnable
{
@Override
public void run()
{
while (true)
{
// Connect & Select SQL
String connectionString = "jdbc:sqlserver://:;database=;user=;password=;";
String SQL = "SELECT [PK_PrintQueueID],[FK_PrinterID],[FK_BarcodeTypeID],[Barcode],[Quantity],[QueueDate],[ProcessedDate] FROM [X].[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();
}
}
}
ZBC.java
package com.company;
public class Main
{
public static void main(String[] args)
{
Form1 f1 = new Form1();
f1.open();
}
}
package com.company;
import javax.swing.*;
public class Form1
{
//Declare GUI items
private JComboBox comboBox1;
private JButton goButton;
private JPanel jp_one;
public Form1()
{
comboBox1.addActionListener(e ->
{
Object selected **= comboBox1.getSelectedItem().toString();
});
goButton.addActionListener(e ->
{
Form2 f2 = new Form2(comboBox1.getSelectedItem().toString());
});
}
public static void open()
{
JFrame frame = new JFrame("Zebra Print");
frame.setContentPane(new Form1().jp_one);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
frame.setResizable(true);
frame.pack();
frame.setLocationRelativeTo(null);
}
}
package com.company;
import javax.swing.*;
class Form2
{
//Declare GUI items
public JTextArea jtaConsole;
public JPanel Jframer;
public JButton stopButton;
public JButton startButton;
String IP1 = "172.20.9.61";
int PORT1 = 9100;
ZBC ZBC = new ZBC();
Thread t1 = new Thread(ZBC);
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
t1.start();
});
stopButton.addActionListener(e ->
jtaConsole.append("- Program is now paused..\n\n"));
}
}
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;
public class ZBC implements Runnable
{
@Override
public void run()
{
while (true)
{
// Connect & Select SQL
String connectionString = "jdbc:sqlserver://:;database=;user=;password=;";
String SQL = "SELECT [PK_PrintQueueID],[FK_PrinterID],[FK_BarcodeTypeID],[Barcode],[Quantity],[QueueDate],[ProcessedDate] FROM [X].[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();
}
}
}
首先,您需要在ZBC中使用Form2对象。你可以使用
t1.setForm2(this);
从2年级开始。
并在ZBC创建一个setter作为setForm2
like-
private Form2 form2;
public void setForm2(final Form2 obj){
this.form2=obj;
}
现在你有了这个物体。
现在你知道了
form2.jtaConsole.append("TEST");
form2.jtaConsole.repaint();
form2.jtaConsole.revalidate();
请让我知道这是否有帮助。首先,您需要在ZBC中使用Form2对象。你可以使用
t1.setForm2(this);
从2年级开始。
并在ZBC创建一个setter作为setForm2
like-
private Form2 form2;
public void setForm2(final Form2 obj){
this.form2=obj;
}
现在你有了这个物体。
现在你知道了
form2.jtaConsole.append("TEST");
form2.jtaConsole.repaint();
form2.jtaConsole.revalidate();
如果这有帮助,请告诉我。@Sursebdu嗨,谢谢你的帮助-我应该在哪里使用t1.setForm2(this);在ZBC.java中?您可以在t1.start()之后的Form2中完成;我试过了,但没有成功。。说方法不能在form2scrap中解析,这是有效的。现在我可以在我的程序中实现。非常感谢,如果我想在我的代码中从ZBC中使用的ifstatement中附加它,那么这种方法会起作用吗?@Sursebdu Hi,谢谢你的帮助-我会在哪里使用t1.setForm2(这个);在ZBC.java中?您可以在t1.start()之后的Form2中完成;我试过了,但没有成功。。说方法不能在form2scrap中解析,这是有效的。现在我可以在我的程序中实现。非常感谢,如果我想在我的代码中从ZBC中使用的ifstatement中附加它,那么同样的方法会起作用吗?请参阅@andrewhompson啊,好的-谢谢你的帮助请参阅@andrewhompson啊,好的-谢谢你的帮助