动态显示JFRAME中ContentPane中的内容--后续问题

bristolcc 2010-07-16 07:01:20
现已按照上面兄弟所讲的方法把需要刷新的内容加入到Jpanel, jpanel.add(frame.getContentPane().add(vv));然后在下面调用jpanel.revalidate(),过程如下:

// 该方法用作显示内容和内容刷新
public void newInfo(){
jpanel.add(frame.getContentPane().add(vv));
jpanel.revalidate();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

我的理想状态是,程序每CALL一次方法newInfo(),content区域就会将变化显示出来。

但现在的问题在于显示不出任何信息了,请问还需要添加些什么呢??
...全文
197 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
bristolcc 2010-07-26
  • 打赏
  • 举报
回复
谢谢,这个问题我已经解决了~/
Jclick 2010-07-17
  • 打赏
  • 举报
回复

public class VisualNetwork{
private NetworkElement net;
private Layout<String, Integer>layout;
private BasicVisualizationServer<String,Integer> vv;
private JFrame frame;
private JPanel panel=new JPanel();
private JButton b=new JButton("点击我刷新Panel");

public VisualNetwork(){
frame = new JFrame("Local community Finder");
frame.setSize(650,650);
frame.setBackground(Color.black);

net = new NetworkElement();
layout = new FRLayout(net.graph);

layout.setSize(new Dimension(650,650)); // sets the initial size of the space
// The BasicVisualizationServer<V,E> is parameterized by the edge types
vv = new BasicVisualizationServer<String,Integer>(layout);
vv.setPreferredSize(new Dimension(630,630)); //Sets the viewing area size
panel.add(vv);
frame.getContentPane().add(b,BorderLayout.NORTH);
frame.getContentPane().add(panel,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
new Thread(new Runner(panel)).start();
}
});

}

/*public void ViewNetwork(){
// frame.getContentPane().add(vv);
JPanel jpanel = frame.getContentPane().add(vv);
jpanel.add(frame.getContentPane().add(vv));
jpanel.revalidate();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}*/
class Runner implements Runnable{
private JPanel panel;
public Runner(JPanel panel){
this.panel=panel;
}
public void run(){
vv = new BasicVisualizationServer<String,Integer>(layout);
vv.setPreferredSize(new Dimension(630,630)); //Sets the viewing area size
panel.add(vv);
panel.updateUI();
}
}
public void addVertex(Node vertex){
net.addVertex(vertex);
}

public void addEdge(Edge info,Node startVertex,Node endVertex){
net.addEdge(info, startVertex, endVertex);
}
}


上边的代码里,我给你新建了个线程来添加vv。然后需要刷新页面时就启用线程。
我给你新建了个button按钮,点击button,就可以刷新页面,不知道可以达到你想要的效果不。
bristolcc 2010-07-17
  • 打赏
  • 举报
回复
只刷新部分的内容。变化之处就在于这几句代码。

vv = new BasicVisualizationServer<String,Integer>(layout);
这里初始化一个可视的对象。

当frame.getContentPane().add(vv)),每次调用add(vv)时,就需要刷新。
Jclick 2010-07-16
  • 打赏
  • 举报
回复
你说那我也听不清楚,我想问下,你刷新页面时,是刷新全局页面呢还是只刷新部分的?还有你是想通过什么来判断刷新页面的。刷新页面时不要调用frame.getContentPane();这样调用是刷新不出来任何东西的。刷新JPanel的时候,把需要刷新的东西添加到JPanel里来。
bristolcc 2010-07-16
  • 打赏
  • 举报
回复
恩,这个项目比较复杂,进行社区发现和动态生成动态网络图。还用到了第三方的类库。我贴出关于“显示”部分的源代码

import edu.uci.ics.jung.algorithms.layout.FRLayout;
import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.algorithms.layout.SpringLayout;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.BasicVisualizationServer;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class VisualNetwork{
private NetworkElement net;
private Layout<String, Integer>layout;
private BasicVisualizationServer<String,Integer> vv;
private JFrame frame;

public VisualNetwork(){
frame = new JFrame("Local community Finder");
frame.setSize(650,650);
frame.setBackground(Color.black);

net = new NetworkElement();
layout = new FRLayout(net.graph);

layout.setSize(new Dimension(650,650)); // sets the initial size of the space
// The BasicVisualizationServer<V,E> is parameterized by the edge types
vv = new BasicVisualizationServer<String,Integer>(layout);
vv.setPreferredSize(new Dimension(630,630)); //Sets the viewing area size
}

public void ViewNetwork(){
// frame.getContentPane().add(vv);
JPanel jpanel = frame.getContentPane().add(vv);
jpanel.add(frame.getContentPane().add(vv));
jpanel.revalidate();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

public void addVertex(Node vertex){
net.addVertex(vertex);
}

public void addEdge(Edge info,Node startVertex,Node endVertex){
net.addEdge(info, startVertex, endVertex);
}
}

当另一个类CALL 方法addVertex和addEdge之后,相当于就建立起了点与线的链接,然后我就需要调用ViewNetwork()把更新的状态刷新出来。流程是这样得:初始化:原始点,然后通过一定算法不停的加入新的点。我现在要做的就是能够动态的显示点加入这个过程。
Jclick 2010-07-16
  • 打赏
  • 举报
回复
如果想动态的现实内容,你用方法调用是不行的。因为,方法调用只能在调用方法的时候才能刷新。应该添加事件触发器,监听变化的内容来实现动态显示。
Jclick 2010-07-16
  • 打赏
  • 举报
回复
把你想要的设计效果说下。我给你代码

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧