我想在我的图框中添加一个滚动窗格。我尝试了几种方法但没有成功。我是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);
}
}
答案 0 :(得分:3)
您的代码完全是随机的。您正在扩展JFrames
,创建JFrames
,您无条件地将Canvas
放入JScrollPane
。你在不考虑自己在做什么的情况下编写随机的东西。这不是一个让事情变得更好的好方法。
JScrollPane Tutorial就在这里,但我建议你也阅读其他教程。
答案 1 :(得分:1)
您的代码存在几个大问题,但最重要的是:
public class GraphDraw extends JFrame {
// ...
JFrame jf=new JFrame();
// ...
public GraphDraw() { //Constructor
// ...
jf.getContentPane().add(new JScrollPane(new Canvas()));
// ...
}
您的GraphDraw类扩展了JFrame,您的代码看起来就像是打算将它用作JFrame,但令人费解的是,您创建了一个完全 new 和不同的JFrame,这里称为jf,给它一个JScrollPane给出了一些不包含任何用途的Canvas对象。它看起来像随机代码抛在墙上。
你不想这样做。相反,创建你的JScrollPane并将你想要滚动的东西传递给它的构造函数,例如一个带有图形的JPanel,然后将它添加到this
作为类的JFrame。例如:
public class GraphDraw extends JFrame {
// JFrame jf=new JFrame(); // get rid of this guy
public GraphDraw() { //Constructor
// ...
getContentPane().add(new JScrollPane(myJPanelWithGraphics));
// ...
}
其中myJPanelWithGraphics是一个包含实际图形的JPanel。
最重要的 - 阅读您已获得链接的教程。正如我从自己的个人经验中学到的,猜测这些东西永远不会奏效。以下是一些不错的Swing资源:
修改强>
有关您最新代码的建议:
paintComponent(Graphics g)
覆盖方法。super.paintComponent(g);
作为上述覆盖的第一个方法调用。这将告诉Swing在组件上进行管家绘制,包括去除脏像素。