2010-08-19 145 views
0

我目前正在研究java中的示例应用程序。我之前并没有真正使用过Java,所以请耐心等待。我已经设置了我的应用程序,所以它轮询mysql数据库以获取信息。我现在的问题与更新框架中的内容有关。目前,我已经在每次请求时都会打开一个包含内容的新窗口。这是由于我对java的知识有限。我想更新框架内的内容而不创建新窗口。 下面的代码:java swing更新框架中的内容

// 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) {} 
     } 
    } 
} 
+0

您看过您的文章并注意到所有代码都未格式化吗?如果你想让人们看你的代码,那么你应该确保你的问题是可读的。 – camickr 2010-08-20 02:42:00

回答

0

你可以把帧与它的一类组件,并给这个类方法来改变组件。所以有一个参考框架和它的组件的类。在每次更新创建新标签代替

void updateText(String text){ 
thisLabel.setText(text); 
} 

JFrame thisframe 
JLabel thislabel = new JLabel("somedeafaulttext"); 
// etc. 

然后,你可以写这样的方法。据我所知当你在JComponents上调用这些设置方法时,它们会自动更新。

0

从数据库中显示数据的更好方法是使用JTable。阅读有关How to Use Tables的Swing教程的部分。

现在,只要您想要替换数据,您可以简单地创建一个新的DefaultTableModel。对于ResultSet中的每一行,您都使用DefaultTableModel.addRow(..)方法将数据添加到模型中。读完ResultSet后,您只需将模型添加到表格中:

table.setModel(...);