-3

我有几个类和一个主要方法。该程序用于连接到 Access 数据库并检索信息。

JTextArea 我有一个类在一个框中仅处理 GUI(以显示结果) 。另一个类运行一个 while 循环并从数据库中提取数据并将其分配给一个字符串,如下所示:

line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");

基本上我的问题是我如何String line从 GUI 类访问,以便我可以txtArea.setText用来显示line记住 GUI 没有 Main 方法?

编辑:

为了解决这个问题,我创建了一个LineObject作为line参数的。然后我调用getLinefrom ,void addComponents但它给出了 nullPointerException?

这是 searchProps 类:

import java.awt.Container;
import java.sql.*;
import java.util.*;
import javax.swing.*;


public class searchProps 
{
    protected String price, area, query, suburb, date, SaleOrRent, strQuery, out, line="";
    protected int agentID, upid, StreetNum, numBeds, numBaths, numGarages, ownerID, size;
    protected boolean spool;
    PropertyObject PropertyArray[] = new PropertyObject[3];
    LineObject obj;
    JFrame jf;  
    JTextArea txtArea = new JTextArea();
     {
        initialFrame();
        addComponents();
    }

public searchProps(int propID) //search using UPID only
   {
       try 
               {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection("jdbc:odbc:PropertyOracleDatabase");
            Statement s = conn.createStatement();



            query = ("SELECT * FROM Properties WHERE UPID = "+propID);

            // Fetch table
            s.execute(query);
            ResultSet rs = s.getResultSet();
            while((rs!=null) && (rs.next()))
            {
                upid=rs.getInt(1);
                StreetNum=rs.getInt(2);
                suburb=rs.getString(3);
                area=rs.getString(4);
                price=rs.getString(5);
                agentID= rs.getInt(6);
                numBeds=rs.getInt(7);
                numBaths=rs.getInt(8);
                spool=rs.getBoolean(9);
                numGarages=rs.getInt(10);
                date=rs.getString(11);
                ownerID=rs.getInt(12);
                SaleOrRent=rs.getString(13);
                size++;


              line = (line+upid+"\t"+StreetNum+"\t"+suburb+"\t"+area+"\t"+price+"\t"+agentID+"\t"+numBeds+"\t"+numBaths+"\t"+spool+"\t"+numGarages+"\t"+date+"\t"+ownerID+"\t"+SaleOrRent+"\n");
              obj= new LineObject(line);
              System.out.println(line);
              String out = obj.getLine();
              System.out.println(out);
            }



            // close and cleanup
            s.close();
            conn.close();
               }

        catch(Exception ex)
        {
            ex.printStackTrace();
        }



   }
    void initialFrame()
    {
        jf=new JFrame();
        jf.setSize (1300,700);
        jf.setTitle("Property Oracle | Results Page");
        jf.setVisible(true);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

     void addComponents()
     {

         Container con = jf.getContentPane();
         con.setLayout(null);
         txtArea.setBounds(20,30,1200,600);
         con.add(txtArea);
         txtArea.setText("UPID\tStreetNum\tSuburb\tArea\tPrice\tAgentID\tBedrooms\tBathrooms\tSwimming Pool\tGarages\tDate\tOwner\tSale/Rent\n");
         out = obj.getLine();
         System.out.println(out);



     }

}

这是 LineObject 类: public class LineObject

{
    protected String line;


    public LineObject(String a)
    {
        line = a;
    }


    public String getLine()
    {
        return line;
    }

}
4

3 回答 3

1

我将假设您的数据库访问代码在单独的线程中运行,否则典型的延迟会阻塞事件调度线程(EDT)。将对您的引用JTextArea作为参数传递给您的数据库代码。使用参考JTextArea在 EDT 上更新:

final String line = …
EventQueue.invokeLater(new Runnable() {
    @Override
    public void run() {
        ta.append(line);
    }
});

在这里可以看到一个相关的例子。

于 2013-09-18T21:27:52.523 回答
0

Take a look at the MVC pattern: it's always good practice to decouple the business logic (putting data in a database and building the string "line") from the frontend (GUI).

By the way, since you're building the string by appending more data to it, you should consider using a StringBuilder instead:

StringBuilder lineBuilder = new StringBuilder();

// append data:
lineBuilder.append(someString);

// create a string only when you need it:
String line = lineBuilder.toString();

In this way you are not continuosly creating new strings (which can be expensive in the long run especially if the string keeps growing), but using the buffer provided by the StringBuilder and then creating an actual string only when you need it, e.g., when you need to update your JTextArea.

于 2013-09-18T19:14:01.820 回答
0

line为您的班级创建一个私有班级字段(运行 while 循环的班级)。

public LoopingClass {
  private String line;

  public void loopMethod() {
    line = //...
  }
}

然后为这个变量创建一个公共 getter。

public LoopingClass {
  private String line;

  public void loopMethod() {
    line = //...
  }

  public String getLine() {
    return line;
  }
}

然后从您的 GUI 中调用对象实例的 getter。

// somewhere in your GUI class
loopingClassInstance.getLine();
于 2013-09-18T19:08:12.713 回答