Java 从另一个类访问对象
我有几个类和一个主要方法。该程序用于连接Access数据库并检索信息 我有一个类,它只在一个Java 从另一个类访问对象,java,string,swing,class,jtextarea,Java,String,Swing,Class,Jtextarea,我有几个类和一个主要方法。该程序用于连接Access数据库并检索信息 我有一个类,它只在一个JTextArea框中处理GUI(显示结果)。另一个类运行while循环,从数据库中提取数据并将其分配给字符串,如下所示: line=(line+upid+“\t”+StreetNum+“\t”+郊区+“\t”+面积+“\t”+price+“\t”+agentID+“\t”+numberds+“\t”+numBaths+“\t”+spool+“\t”+numGarages+“\t”+date+“\t”+o
JTextArea
框中处理GUI(显示结果)。另一个类运行while
循环,从数据库中提取数据并将其分配给字符串,如下所示:
line=(line+upid+“\t”+StreetNum+“\t”+郊区+“\t”+面积+“\t”+price+“\t”+agentID+“\t”+numberds+“\t”+numBaths+“\t”+spool+“\t”+numGarages+“\t”+date+“\t”+ownerID+“\t”+salerent+“\n”);
基本上,我的问题是如何从GUI类访问字符串行
,以便使用txtArea.setText
显示行
,记住GUI没有主方法
编辑:
为了解决这个问题,我创建了一个LineObject
,它将line
作为参数。然后我从void addComponents
调用getLine
,但它给出了一个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类:
公共类LineObject
{
protected String line;
public LineObject(String a)
{
line = a;
}
public String getLine()
{
return line;
}
}
使
line
成为类的私有类字段(运行while循环的字段)
然后创建此变量的公共getter
public LoopingClass {
private String line;
public void loopMethod() {
line = //...
}
public String getLine() {
return line;
}
}
然后从GUI调用对象实例的getter
// somewhere in your GUI class
loopingClassInstance.getLine();
请看一下:将业务逻辑(将数据放入数据库并构建字符串“行”)与前端(GUI)分离始终是一种良好的做法
顺便说一下,由于您通过添加更多的数据来构建字符串,所以应该考虑使用<代码> StringBuilder <代码>代替:
StringBuilder lineBuilder = new StringBuilder();
// append data:
lineBuilder.append(someString);
// create a string only when you need it:
String line = lineBuilder.toString();
通过这种方式,您不会继续创建新字符串(从长远来看,这可能会很昂贵,尤其是当字符串不断增长时),而是使用
StringBuilder
提供的缓冲区,然后仅在需要时创建实际字符串,例如。,当您需要更新JTextArea
时,我将假设您的数据库访问代码在单独的线程中运行,否则典型的延迟将阻塞(EDT)。将对JTextArea
的引用作为参数传递给数据库代码。使用参考更新EDT上的JTextArea
:
final String line = …
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ta.append(line);
}
});
可以看到一个相关示例。将其设置为
公共
或添加一个访问器(getter)方法。它是静态的,还是属于GUI类的实例?通常的方法是为其类中的line
创建getter()
方法。如果另一个类位于同一个包中,则它可以在实例化定义行
的类的对象后直接访问行
。但是,尝试了此操作后,调用它时会出现空指针异常。问题是,我跟踪行,它肯定有数据,直到它在void addComponents方法中被调用为止。要使整个过程正常工作,您必须确保使用的类已正确实例化(new LoopingClass
某处),然后您可以从调用类(GUI类)实际访问它,GUI类中应该有类似于loop=new LoopingClass()
的内容
final String line = …
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ta.append(line);
}
});