【发布时间】:2014-05-03 13:17:21
【问题描述】:
我想在我的图表框架中添加一个滚动窗格。我尝试了几种方法但没有成功。我是 Java 新手,所以请发布代码和建议。谢谢 代码在这里
public class GraphDraw extends JFrame {
int width;
int height;
JPanel setPanel;
JFrame jf=new JFrame();
ArrayList<Node> nodes;
ArrayList<edge> edges;
public GraphDraw() { //Constructor
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().add(new JScrollPane(new Canvas()));
nodes = new ArrayList<Node>();
edges = new ArrayList<edge>();
width = 30;
height = 30;
}
public GraphDraw(String name) { //Construct with label
this.setTitle(name);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nodes = new ArrayList<Node>();
edges = new ArrayList<edge>();
width = 30;
height = 30;
}
class Node {
public Node(String myName, int myX, int myY) {
}
}
class edge {
public edge(int ii, int jj) {
}
}
public void addNode(String name, int x, int y) {
//add a node at pixel (x,y)
}
public void addEdge(int i, int j) {
//add an edge between nodes i and j
}
public void paint(Graphics g) { // draw the nodes and edges
FontMetrics f = g.getFontMetrics();
}}}
现在这个类在其他一些类中的实例,例如
public class showGraph extends JFrame {
public int x=250;
public int y=50;
public showGraph(ArrayList<Structure> array){
GraphDraw frame = new GraphDraw("My Window");
frame.setBounds(600,10,600,800);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//logics
}
}
__________________________
最新代码:
public class GraphDraw extends JFrame {
int width;
int height;
ArrayList<Node> nodes;
ArrayList<edge> edges;
public GraphDraw() { // Constructor
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nodes = new ArrayList<Node>();
edges = new ArrayList<edge>();
width = 30;
height = 30;
}
public GraphDraw(String name) { // Construct with label
this.setTitle(name);
// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
nodes = new ArrayList<Node>();
edges = new ArrayList<edge>();
width = 30;
height = 30;
}
class Node {
int x, y;
String name;
public Node(String myName, int myX, int myY) {
x = myX;
y = myY;
name = myName;
}
}
class edge {
int i, j;
public edge(int ii, int jj) {
i = ii;
j = jj;
}
}
public void addNode(String name, int x, int y) {
// add a node at pixel (x,y)
nodes.add(new Node(name, x, y));
this.repaint();
}
public void addEdge(int i, int j) {
// add an edge between nodes i and j
edges.add(new edge(i, j));
this.repaint();
}
public void paint(Graphics g) { // draw the nodes and edges
FontMetrics f = g.getFontMetrics();
int nodeHeight = Math.max(height, f.getHeight());
g.setColor(Color.black);
for (edge e : edges) {
g.drawLine(nodes.get(e.i).x, nodes.get(e.i).y, nodes.get(e.j).x,
nodes.get(e.j).y);
}
for (Node n : nodes) {
int nodeWidth = Math.max(width, f.stringWidth(n.name) + width / 2);
g.setColor(Color.white);
g.fillOval(n.x - nodeWidth / 2, n.y - nodeHeight / 2, nodeWidth,
nodeHeight);
g.setColor(Color.black);
g.drawOval(n.x - nodeWidth / 2, n.y - nodeHeight / 2, nodeWidth,
nodeHeight);
g.drawString(n.name, n.x - f.stringWidth(n.name) / 2,
n.y + f.getHeight() / 2);
}
}
}
showGraph.java
public class showGraph {
public int pos_x = 250;
public int pos_y = 50;
public showGraph(ArrayList<Structure> array) {
GraphDraw frame = new GraphDraw("My Graph");
frame.setBounds(600, 10, 600, 800);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.addNode("Node-1", pos_x, pos_y);
frame.addNode("Node-2", pos_x, pos_y + 100);
frame.addEdge(0, 1);
}
}
【问题讨论】:
-
请详细说明——例如,您似乎将 Canvas 对象放入 JScrollPane 中——为什么?你到底想把什么放入 JScrollPane 中?另外,请不要索要代码。您可能会获得代码,但可能不会,而只会获得建议。但是,如果您首当其冲地编写代码,您将学到更多。
-
您也有扩展 JFrame 的类,但似乎正在创建另一个 JFrame,jf,您从未显示过 - 为什么?你的代码背后的逻辑是什么?
-
我想要可滚动的窗口/框架,我将在其中添加一些图表
-
您将需要阅读 Kayaman 链接到的教程以及其他教程。请查看this link 以获得一些不错的 Swing 资源。
标签: java swing jscrollpane