您好,我已经编写了这段代码,我在这段代码中尝试的是获取方法的返回值到java swing标签
这是我的代码:
public static int search(java.util.Date date)
{
Connection conn = null;
ResultSet rs = null;
Statement st = null;
int b=0;
try
{
conn=DBMgr.openConnection();
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String dateStr=formatter.format(date);
System.out.println("date"+dateStr);
String sqlQuery = "select sum(time_spend) as Time_Billed_Per_Day,datetime from time_entry where datetime like '"+dateStr+"%' ";
st = conn.createStatement();
rs = st.executeQuery(sqlQuery);
while(rs.next())
{
b = rs.getInt(1);
System.out.println("BILL of the date u specified is:"+b);
}
}
catch(SQLException ex)
{
System.out.println(ex.toString());
}
finally
{
try
{
if(rs!=null)
rs.close();
if(conn!=null)
DBMgr.closeConnection(conn);
}
catch(Exception ex)
{
}
}
return b;
}
Run Code Online (Sandbox Code Playgroud)
这是秋千代码:
JLabel lblTimeBilledDayText = new JLabel( "00:45:20" , JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);
Run Code Online (Sandbox Code Playgroud)
我想得到方法的返回值代替"00:45:20"
这该怎么做?
试试这个
JLabel lblTimeBilledDayText = new JLabel(String.valueOf(search(date)) , JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);
Run Code Online (Sandbox Code Playgroud)
如果b是返回int值则
JLabel lblTimeBilledDayText = new JLabel(new String(Integer.toString(b)), JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);
Run Code Online (Sandbox Code Playgroud)
int searchResult = search( date);
// convert to String. You might want to opt for a method
// which offers more formatting options
String searchResultAsString = Integer.toString( searchResult );
JLabel lblTimeBilledDayText = new JLabel( searchResultAsString , JLabel.RIGHT);
pnlOuter.add(lblTimeBilledDayText);
Run Code Online (Sandbox Code Playgroud)
话虽这么说,你不应该在同一个线程上混合数据库查询和Swing对象修改/创建.Swing对象必须在事件指派线程(EDT)和长时间运行的操作(例如像数据库查询)不应该在美国东部时间执行的UI将被阻塞/反应迟钝而操作发生处理.
有关详细信息,请查看Swurrency中的Concurrency教程.典型的解决方案是使用a在工作线程上执行查询SwingWorker.查询完成后,SwingWorker有一个简单的机制来在正确的线程上更新Swing组件.
| 归档时间: |
|
| 查看次数: |
1538 次 |
| 最近记录: |