gpt4 book ai didi

java - 为什么我的滚动 Pane 不起作用

转载 作者:行者123 更新时间:2023-12-01 12:56:58 25 4
gpt4 key购买 nike

我正在使用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);
}


}


}

你能帮我吗?

最佳答案

您没有为滚动 Pane 提供大小调整提示来确定组件应该有多大,从而确定是否需要滚动。

首先重写 MyPannelgetPreferredSize 方法,并返回一个最能代表您希望组件大小的值。

如果可能的话最好提前计算

关于java - 为什么我的滚动 Pane 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23798778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com