为什么我的滚动窗格不起作用

时间:2014-05-22 05:53:09

标签: java swing jpanel jscrollpane preferredsize

我使用Java图形界面显示分支和剪切树,但树的大小很大,我无法完全看到它,我决定添加滚动条以便能够轻松遍历,但它无法正常工作。 我的代码是:

package minemodel;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

public class BAndBGUI {
    private int _distance;
    private JFrame _frame;
    private MyPannel _mainPannel;
    private ArrayList<NodeForTree> _node;
    public BAndBGUI(int convNumber) {
    _node=new ArrayList<NodeForTree>();
    _frame=new JFrame();
    _distance=700/convNumber;
    _mainPannel=new MyPannel();
    _frame.setVisible(true);
    _frame.setSize(1350, 700);
    _frame.setLocation(0, 0);
    _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JScrollPane scrollPane = new JScrollPane( _mainPannel );

    _frame.add(scrollPane);

}

public MyPannel getPanel(){
    return _mainPannel;
}

public void setNode(NodeForTree node){
    _node.add(node);
    _mainPannel.repaint();
}

public class MyPannel extends JPanel{

    /**
     * 
     * @param level the level of the current node
     * @param number number of nodes currently on that level,
     * @param Parent's locaton to draw the line.
     * @return
     */


    public void paintComponent(Graphics g){
        //  super.paintComponent(g);
        Iterator<NodeForTree> itrNode=_node.iterator();
        while (itrNode.hasNext()){
            NodeForTree node=itrNode.next();

            g.drawOval(node.getLocation()[0], node.getLocation()[1], 20, 20);
            g.setFont(new Font("Arial", Font.BOLD, 14));
            g.setColor(Color.BLACK);
            if (node.getParent()!=null){
                g.drawString(node.toString(),node.getLocation()[0], node.getLocation()[1]+15);
                NodeForTree parrent=node.getParent();
                drawArrow(g,parrent.getLocation()[0], parrent.getLocation()[1], node.getLocation()[0], node.getLocation()[1]);
                System.out.println("parent"+parrent.getLocation()[0]+" "+parrent.getLocation()[1]+" and "+node.getLocation()[0]+" "+node.getLocation()[1]);

                //  g.drawLine(node.getLocation()[0], node.getLocation()[1],parrent.getLocation()[0], parrent.getLocation()[1]);

            }else{
                g.drawString("start",500, 15);
            }
            g.drawString(node.getObjective(),node.getLocation()[0], node.getLocation()[1]);
        }
    }

    public void drawArrow (Graphics g1, int x1, int y1, int x2, int y2) {
        Graphics2D g = (Graphics2D) g1.create();

        double dx = x2 - x1, dy = y2 - y1;
        double angle = Math.atan2(dy, dx);
        int len = (int) Math.sqrt(dx*dx + dy*dy);
        AffineTransform at = AffineTransform.getTranslateInstance(x1, y1);
        at.concatenate(AffineTransform.getRotateInstance(angle));
        g.transform(at);

        // Draw horizontal arrow starting in (0, 0)
        g.setColor(Color.BLACK);
        g.drawLine(0, 0, len, 0);
        //  g.setColor(Color.BLUE);
        g.fillPolygon(new int[] {len, len-5, len-5, len}, new int[] {0, -5, 5, 0}, 4);
    }


}


}

你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您没有为滚动窗格提供任何大小调整提示,以确定组件应该有多大,从而确定是否需要滚动它。

首先覆盖MyPannel的{​​{1}}方法,然后返回一个最能代表您希望该组件大小的值。

如果可能,最好提前计算