java实现的动态代码图 java 动态代码

java动态时间代码

你应该是想把时间显示在jl01的控件上。但是你这样只能显示初始化时的时间。

目前成都创新互联已为近千家的企业提供了网站建设、域名、虚拟空间、网站托管、服务器托管、企业网站设计、鹤山网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

每隔1s更新时间,简单一点可以用循环,然后每次循环设置1000ms的睡眠时间,这个同时需要用多线程实现。也可以用timer做定时任务。推荐后者

//import java.util.Timer

//import java.util.TimerTask 

Timer timer = new Timer();

TimerTask task=new TimerTask() {

@Override

public void run() {

Date date = new Date();

String string = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);

jl01.setText(string);

}

};

//立即开始任务,任务间隔1000ms。schedule和scheduleAtFixedRate的区别自行搜索

timer.scheduleAtFixedRate(task,0,1000);

哪位大侠可以帮我写一段java代码!是关于根据数据库里的数据来形成一个动态树形图

这是代码,你可以自己调试一下。

数据结构如下:

CREATE TABLE dtree (

id int,

pid int,

name varchar(200),

url varchar(200),

title varchar(200),

target varchar(200),

icon varchar(200),

iconopen varchar(200),

opened bit);

为了实现获取数据库变量功能,需要建立一个DTree类,并编译生成CLASS文件,放入\WEB-INF\classes文件夹下。

DTree类代码如下:

package work3;

public class DTree {

private int id;

private int pid;

private String name;

private String url;

private String title;

private String target;

private String icon;

private String iconOpen;

private int opened;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public int getPid() {

return pid;

}

public void setPid(int pid) {

this.pid = pid;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getUrl() {

return url;

}

public void setUrl(String url) {

this.url = url;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

public String getTarget() {

return target;

}

public void setTarget(String target) {

this.target = target;

}

public String getIcon() {

return icon;

}

public void setIcon(String icon) {

this.icon = icon;

}

public String getIconOpen() {

return iconOpen;

}

public void setIconOpen(String iconOpen) {

this.iconOpen = iconOpen;

}

public int getOpened() {

return opened;

}

public void setOpened(int opened) {

this.opened = opened;

}

}

work3.jsp代码如下:

%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%

%@ page import="java.sql.*"%

jsp:useBean id='settree' scope="application" class="work3.DTree" /

!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

""

html

head

meta http-equiv="Content-Type" content="text/html; charset=GB18030"

link rel="StyleSheet" href="dtree.css" type="text/css" /

script type="text/javascript" src="dtree.js"/script

titledTree in MySQL/title

/head

body

h2

Example

/h2

div class="dtree"

p

a href="javascript: d.openAll();"open all/a |

a href="javascript: d.closeAll();"close all/a

/p

script type="text/javascript"

!--

d = new dTree('d');

%

//驱动程序名

String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";

//数据库用户名

String userName = "sa";

//密码

String userPwd = "1";

//数据库名

String dbName = "master";

//表名

String tableName = "dtree";

//连接字符串

String url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName="+dbName;

//加载驱动

Class.forName(driverName).newInstance();

//连接数据库

java.sql.Connection conn = DriverManager.getConnection(url,userName,userPwd);

//得到Statement实例

java.sql.Statement statement = conn.createStatement();

//查询数据

String sql = "select * from " + tableName;

//返回结果

java.sql.ResultSet rs = statement.executeQuery(sql);

//获取变量

while (rs.next()) {

settree.setId(rs.getInt(1));

settree.setPid(rs.getInt(2));

settree.setName(rs.getString(3));

settree.setUrl(rs.getString(4));

settree.setTitle(rs.getString(5));

settree.setTarget(rs.getString(6));

settree.setIcon(rs.getString(7));

settree.setIconOpen(rs.getString(8));

settree.setOpened(rs.getInt(9));

if(settree.getPid()==0)

settree.setOpened(1);

%

d.add(%=settree.getId()%,%=settree.getPid()%,'%=settree.getName()%','%=settree.getUrl()%','%=settree.getTitle()%','%=settree.getTarget()%','','',%=settree.getOpened()%);

%

}

%

document.write(d);

//--

/script

/div

/body

/html

用java如何用代码实现动态生成JLabel

String[] str={"名字1","名字2","名字3"};

for(int i=0;iv.length;i++){

JLabel lbl = new JLabel(str[i]);

int x=i%4*80+40; //4个一排 间隔80 第一个x从40开始

int y=i/4*40+40;

lbl[i].setBounds(x, y, 80, 20);

panel.add(chb[i]);

}


当前文章:java实现的动态代码图 java 动态代码
链接URL:http://pwwzsj.com/article/hiicgd.html