你好II'ma在Java和我正尝试写一个非常简单的锻炼; Tibial练练新手,我创建了一个就是jclass从数据库读取参数,并将其保存在一个名为“国家体制”变量和一个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();
}
}
}
}
存储变量被称为“国家体制”,这些变量是1或0,我正尝试这些变量的每一个变化使jTextField1在下面的JFrame值的变化:
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,我不知道如何在“国家体制”的每一个变化更新jTextField1。