gpt4 book ai didi

java - 每次变量更改时更新 JTextFields 文本

转载 作者:行者123 更新时间:2023-11-29 08:13:24 24 4
gpt4 key购买 nike

你好,我是 Java 的新手,我正在尝试编写一个非常简单的练习来练习,我创建了一个 Jclass 来从数据库读取参数并将其保存在名为“estado”的变量和一个 JFrame 中显示该变量的值在数据库中随机变化。

该程序读取 MySql DB 并将所需的数据存储在 Java 变量中,如下所示:

package Paquete_domotica;

import java.io.*;
import java.sql.*;

public class domotica {

public static int estado;
boolean loop = true;

public domotica() throws IOException
{
while(loop)
{
try
{
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
Connection conexion = DriverManager.getConnection (
"jdbc:mysql://localhost/XXX","XXXX", "XXXX");
Statement s = conexion.createStatement();
ResultSet rs = s.executeQuery ("select id, nombre, valor from data");
while (rs.next())
{
if (rs.getInt ("id") == 20)
{
estado = rs.getInt ("valor");
}
}
rs.close();
conexion.close();
}
catch (SQLException e)
{
e.printStackTrace();
}

}
}
}

存储的变量称为“estado”,这些变量是 1 或 0,我尝试这些变量的每次更改都会更改以下 Jframe 中 jTextField1 的值:

package Paquete_domotica;

import java.awt.event.ActionListener;
import java.io.IOException;

public class JFramedomotica extends javax.swing.JFrame {

int numeroRecibido;

public JFramedomotica() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jTextField1 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
jTextField1.setText("SIN DATOS");
jTextField1.setCursor(new java.awt.Cursor(java.awt.Cursor.TEXT_CURSOR));
jTextField1.setEnabled(false);
jTextField1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
jTextField1MouseClicked(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()
.addGap(110, 110, 110)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(154, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(138, 138, 138)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(142, Short.MAX_VALUE))
);

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

private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {
jTextField1.setText(String.valueOf(domotica.estado));
}

public static void main(String args[]) throws IOException {
/* 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(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(JFramedomotica.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 JFramedomotica().setVisible(true);
}
});
new domotica();

}

// Variables declaration - do not modify
private javax.swing.JTextField jTextField1;
// End of variables declaration


}

正如您现在所看到的,我可以使用

更新 jTextField1
private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {                                         
jTextField1.setText(String.valueOf(domotica.estado));
}

但是使用这段代码我必须单击鼠标来刷新 jTextField1,我不知道如何在每次更改“estado”时更新 jTextField1。

最佳答案

您的代码有很多错误,如此之多以至于更容易,因此给您一个清晰的示例来说明如何完成此操作。

解决这个问题的关键是将轮询和UI分离,然后引入事件机制来通知UI轮询器检测到的变化。

您的第一个概念是正确的,但是您确实不希望调用线程永远循环,就像您的 domotica 类的情况一样。您希望该类对您隐藏轮询并在数据更改时触发事件,如下所示:

public class Poller
{
private boolean running;
private DataSource dataSource;
private String sql;
private long pollInterval;
private List<DataChangeListener> dataChangeListeners;

public Poller(DataSource dataSource, String sql, long pollInterval)
{
this.dataSource = dataSource;
this.sql = sql;
this.pollInterval = pollInterval;
this.running = false;
this.dataChangeListeners = new CopyOnWriteArrayList<DataChangeListener>();
}

public void addDataChangeListener(DataChangeListener dataChangeListener)
{
this.dataChangeListeners.add(dataChangeListener);
}

public void removeDataChangeListener(DataChangeListener dataChangeListener)
{
this.dataChangeListeners.remove(dataChangeListener);
}

public boolean isRunning()
{
return running;
}

public synchronized void start()
{
if (!isRunning())
{
this.running = true;
new PollingThread().start();
}
}

public synchronized void stop()
{
this.running = false;
}

private void fireListeners(Object previousValue, Object newValue)
{
for (DataChangeListener dataChangeListener : dataChangeListeners)
{
dataChangeListener.dataChanged(previousValue, newValue);
}
}

private class PollingThread extends Thread
{
@Override
public void run()
{
Connection connection = null;
PreparedStatement statement = null;

try
{
connection = dataSource.getConnection();
statement = connection.prepareStatement(sql);

Object previousValue = null;

while (isRunning())
{
ResultSet resultSet = statement.executeQuery();

while (resultSet.next())
{
Object newValue = resultSet.getObject(1);

if (!newValue.equals(previousValue))
{
fireListeners(previousValue, newValue);
previousValue = newValue;
}
}

Thread.sleep(pollInterval);
}
}
finally
{
if (statement != null) statement.close();
if (connection != null) connection.close();
}
}
}
}

注意PollerThread 以及它如何处理 JDBC 资源。首先它有一个 sleep 以避免占用CPU。其次,像您的示例中那样重复打开和关闭连接可能会让您的 DBA 感到不安。最好使用 javax.sql.DataSource 并获取循环外部的资源。

然后可以实例化 Poller 类并将其传递到应用程序主框架,如下所示,使用窗口关闭来停止轮询:

SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
DataSource dataSource = ...
String sql = ...

final Poller poller = new Poller(dataSource, sql, 1000);

MainFrame mainFrame = new MainFrame(poller);
mainFrame.addWindowListener(
new WindowAdapter()
{
public void windowClosed(WindowEvent event)
{
poller.stop():
}
}
);
}
}
);

差不多就这样了。

最后一部分是让 MainFrame 监听来自 Poller 的事件并更新 UI。这里重要的是更新应该在 Event Dispatch Thread 上执行。像这样:

public class MainFrame extends JFrame
{
private JTextField textField;

public MainFrame(Poller poller)
{
//Create controls first.

poller.add(new TestFieldUpdateListener());
poller.start();
}

private class TestFieldUpdateListener implements DataChangeListener
{
public void dataChanged(final Object previousValue, final Object newValue)
{
SwingUtilities.invokeLater(
new Runnable()
{
public void run()
{
textField.setText(newValue.toString());
}
}
);
}
}
}

就是这样。这个示例应该为您提供要点,它缺少必要的异常处理、null 检查等,但这应该很容易添加。

关于java - 每次变量更改时更新 JTextFields 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21095225/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com