【问题标题】:How to add a scrollpane in java SE?如何在 java SE 中添加滚动窗格?
【发布时间】: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


【解决方案1】:

您的代码完全是随机的。您正在扩展JFrames,创建JFrames,您无缘无故地将Canvas 放入JScrollPane。你在写随机的东西而不考虑你在做什么。这不是做任何明智的事情的好方法。

JScrollPane Tutorial 在这里,但我建议您也阅读其他教程。

【讨论】:

    【解决方案2】:

    您的代码有几个大问题,但最大的问题在这里:

    public class GraphDraw extends JFrame {
       // ...
       JFrame jf=new JFrame();
       // ...
    
       public GraphDraw() { //Constructor
          // ...
          jf.getContentPane().add(new JScrollPane(new Canvas()));
          // ...
       }
    

    您的 GraphDraw 类扩展了 JFrame,并且您的代码看起来像您打算将其用作 JFrame,但令人费解的是,您创建了一个完全和不同的 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 资源:


    编辑
    关于您最新代码的建议:

    • 不要直接在 JFrame 中绘制
    • 改为在 JPanel 的 paintComponent(Graphics g) 覆盖方法中绘制。
    • 确保调用super.paintComponent(g); 作为上述覆盖的第一个方法调用。这将告诉 Swing 对组件进行内务绘制,包括删除脏像素。
    • 然后将此绘图 JPanel 添加到 JScrollPane 的视口中,方法是将其传递给 JScrollPane 的构造函数。
    • 然后将 JScrollPane 添加到 JFrame 的 contentPane 中。

    【讨论】:

    • @user3531482:如果您需要更详细的帮助,那么您将需要改进您的问题。根据您迄今为止发布的内容,任何人都无法猜测可能出了什么问题。如果您尝试对代码进行更改但仍然无法正常工作,那么您需要edit your question,将您的最新代码显示为问题底部的补充,并提供更详细的说明不工作。
    • ^是的,我询问任务的方式是错误的...我再次发布,请访问这里stackoverflow.com/questions/23445973/…
    猜你喜欢
    • 1970-01-01
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-08-29
    • 2013-07-09
    相关资源
    最近更新 更多