我正在尝试通过 SerialRead 方法设置数据变量,但我得到“无法对非静态字段 Etiqueta2 进行静态引用”,Eclipse 说我应该将 Etiqueta2 设为静态,但它没有在 Jlabel 中显示数据。
public class Final extends JFrame
{
//Crea Elementos de la interfaz
JLabel Etiqueta=new JLabel();
JTextField Texto=new JTextField();
JButton Boton=new JButton();
JLabel Etiqueta1=new JLabel();
JLabel Etiqueta2=new JLabel();
JButton Boton1=new JButton();
public Final()
{
super();
// Crea la interfaz
setVisible(true);
setLayout(null);
setTitle("Lectura y escritura de datos");
setSize(380,200);
//Propiedades de los elementos del Frame
// Etiqueta
Etiqueta.setBounds(20,50, 100, 20);
Etiqueta.setText("Enviar un digito");
add(Etiqueta);
// Caja de texto
Texto.setBounds(120,50,100, 20);
add(Texto);
// Boton
Boton.setBounds(250,50,100, 20);
Boton.setText("Enviar");
add(Boton);
// Etiqueta 1
Etiqueta1.setBounds(20,80, 100, 20);
Etiqueta1.setText("Leer un digito");
add(Etiqueta1);
// Etiqueta2
Etiqueta2.setBounds(120,80,100, 20);
add(Etiqueta2);
// Boton 1
Boton1.setBounds(250,80,100, 20);
Boton1.setText("Leer");
add(Boton1);
// Boton de cierre
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
void connect ( String portName ) throws Exception
{
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier(portName);
if ( portIdentifier.isCurrentlyOwned() )
{
System.out.println("Error: Port is currently in use");
}
else
{
CommPort commPort = portIdentifier.open(this.getClass().getName(),2000);
if ( commPort instanceof SerialPort )
{
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
InputStream in = serialPort.getInputStream();
OutputStream out = serialPort.getOutputStream();
new SerialReader(in, Etiqueta2).execute();
(new Thread(new SerialWriter(out))).start();
}
else
{
System.out.println("Error: Only serial ports are handled by this example.");
}
}
}
/** */
public static class SerialReader extends SwingWorker<Void, String> { //Final frame = new Final();
private InputStream in;
private JLabel label;
public SerialReader(InputStream in, JLabel label) {
this.in = in;
this.label = label;
}
@Override
protected void process(List<String> chunks) {
String data = chunks.get(chunks.size() - 1);
label.setText(data);
}
@Override
protected Void doInBackground() throws Exception {
byte[] buffer = new byte[1024];
int len = -1;
while ((len = this.in.read(buffer)) > -1) {
String data = new String(buffer, 0, len);
System.out.println(data);
publish(data);
}
return null;
}
}
/** */
public static class SerialWriter implements Runnable
{
**** output code ****
}
public static void main ( String[] args )
{
try
{
(new Final()).connect("COM7");
}
catch ( Exception e )
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如何设置 Etiqueta2 标签上的文本以显示数据变量?
谢谢
EDIT3:我用 swingworker 更新了代码,执行和 println 以在控制台中查看数据
最佳答案
SerialReader
是 Final
的静态内部类,因此无法访问 Final
中的任何非静态字段。由于您显然不在 Final
之外使用 SerialReader
,只需将其设为非静态(甚至可以是私有(private)内部类)。这同样适用于 SerialWriter
。
关于java - JFrame,错误无法对非静态字段进行静态引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29761883/