Hello II'是Java中的新手,我正在尝试编写一个非常简单的练习练习,我创建了一个Jclass来从DB读取参数并将其保存在名为“estado”的变量和JFrame中显示在DB中随机更改的此变量的值。
该程序读取MySql数据库并存储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
}
正如您现在所看到的,我可以使用
更新jTextField1private void jTextField1MouseClicked(java.awt.event.MouseEvent evt) {
jTextField1.setText(String.valueOf(domotica.estado));
}
但是使用这段代码我必须单击鼠标刷新jTextField1,我不知道如何在每次更改“estado”时更新jTextField1。
答案 0 :(得分:1)
你有很多痛苦的代码,很难从哪里开始,你使用的是public static estado
变量,这在OOP中是一个非常糟糕的设计。 Swing是一个基于事件的系统。所以让我们看一些提示
while(true)
阻挡了你的gui使它无法反应。如果您想重复,可以使用Swing Timer 。请注意,Swing计时器的任务是在事件派发线程中执行的。这意味着任务可以安全地操作组件,但这也意味着任务应该快速执行。如果任务可能需要一段时间才能执行,那么请考虑使用SwingWorker而不是计时器。Observer Pattern
,例如使用PropertyChangeListener
和PropertyChangeSupport
来更新您的JTextField。使用SwingWorker
。
public class Domotica extends SwingWorker<Void, Void> {
private Integer estado;
private boolean loop = true;
@Override
protected Void doInBackground() throws Exception {
while (loop) {
try {
DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
Connection conexion = DriverManager.getConnection(
"jdbc:mysql://localhost/XXX", "XXXX", "XXXX");
java.sql.Statement s = conexion.createStatement();
ResultSet rs = s.executeQuery("select id, nombre, valor from data");
while (rs.next()) {
if (rs.getInt("id") == 20) {
setEstado(rs.getInt("valor"));
}
}
//think in some connection pool to not open and close everytime
rs.close();
conexion.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
Thread.sleep(100);
}
return null;
}
public void setEstado(Integer estado) {
int oldEstado = this.estado;
this.estado = estado;
firePropertyChange("estado", oldEstado, this.estado);
}
}
然后在JFrame
你可以做到这一点。
public JFramedomotica() {
initComponents();
SwingWorker<Void,Void> worker = new Domotica();
worker.addPropertyChangeListener(new PropertyChangeListener(){
@Override
public void propertyChange(PropertyChangeEvent evt){
if(evt.getPropertyName().equals("estado")){
jTextfield1.setText(evt.getNewValue().toString());
}
}
});
worker.execute();
}
答案 1 :(得分:1)
你的代码出了很多问题,以至于它更容易,所以给你一个干净的例子来说明如何做到这一点。
解决此问题的关键是分离轮询和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资源。首先,它是一个睡眠,以避免占用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
检查等,但这应该很容易添加。