Je travaille actuellement sur un exemple d'application en Java. Je n'ai pas vraiment utilisé Java pour beaucoup plus que bonjour monde alors s'il vous plaît ours avec moi. J'ai paramétré ma demande de manière à interroger une base de données mysql pour plus d'informations. Le problème que j'ai maintenant à faire avec la mise à jour du contenu dans le cadre. Actuellement, je l'ai ouvert une nouvelle fenêtre avec le contenu à chaque demande. Cela est dû à ma connaissance limitée de Java. Je voudrais mettre à jour le contenu dans le cadre sans créer une nouvelle fenêtre. est ici le code:java swing mise à jour du contenu dans le cadre
// Import the swing and AWT classes needed
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.BoxLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.awt.Component;
import java.awt.Container;
import java.util.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
/**
* Basic Swing example.
*/
public class client extends JPanel {
public static void main(String[] args) {
// Make sure all Swing/AWT instantiations and accesses are done on the
// Create a JFrame, which is a Window with "decorations", i.e.
// title, border and close-button
JFrame f = new JFrame("Densebrain Test Client");
// Set a simple Layout Manager that arranges the contained
// Components
f.setLayout(new FlowLayout());
// Add some Components
f.add(new JLabel("Hello, world!"));
f.add(new JButton("Press me!"));
// "Pack" the window, making it "just big enough".
f.pack();
// Set the default close operation for the window, or else the
// program won't exit when clicking close button
// (The default is HIDE_ON_CLOSE, which just makes the window
// invisible, and thus doesn't exit the app)
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Set the visibility as true, thereby displaying it
f.setVisible(true);
int delay = 0; // delay for 5 sec.
int period = 5000; // repeat every sec.
Timer timer = new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
public void run() {
runn();
}
}, delay, period);
//runn();
}
public static void runn() {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/densebrain",
"root", "");
if(!con.isClosed())
{
System.out.println("Successfully connected to " +
"MySQL server using TCP/IP...");
stmt = con.createStatement();
try {
rs = stmt.executeQuery("SELECT * FROM posts");
try {
JFrame f = new JFrame("POSTS");
f.setLayout(new GridLayout(40,1));
//pane = f.getContentPane();
//getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
while (rs.next()) {
int numColumns = rs.getMetaData().getColumnCount();
for (int i = 1 ; i <= numColumns ; i++) {
// Column numbers start at 1.
// Also there are many methods on the result set to return
// the column as a particular type. Refer to the Sun documentation
// for the list of valid conversions.
System.out.println("COLUMN " + i + " = " + rs.getObject(i));
switch(i)
{
case 1:
break;
case 2: f.add(new JLabel("NAME = " + rs.getObject(i)));
break;
case 3: f.add(new JLabel("CONTENT = " + rs.getObject(i)));
break;
case 4: f.add(new JLabel("CREATED = " + rs.getObject(i)));
break;
}
}
f.add(new JLabel("*********"));
}
f.pack();
// Set the default close operation for the window, or else the
// program won't exit when clicking close button
// (The default is HIDE_ON_CLOSE, which just makes the window
// invisible, and thus doesn't exit the app)
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
// Set the visibility as true, thereby displaying it
f.setVisible(true);
} finally {
try { rs.close(); } catch (SQLException ignore) { /* Propagate the original exception instead of this one that you may want just logged */ }
}
} finally {
try { stmt.close(); } catch (SQLException ignore) { /* Propagate the original exception instead of this one that you may want just logged */ }
}
}
} catch(Exception e) {
System.err.println("Exception: " + e.getMessage());
} finally {
try {
if(con != null)
con.close();
} catch(SQLException e) {}
}
}
}
Avez-vous regardé votre message et remarqué que tout le code n'est PAS formaté? Si vous voulez que les gens regardent votre code, vous devez vous assurer que votre question est lisible. – camickr