当我运行该程序时,JPanel不可见。不过,它可以在没有JScrollPane的情况下工作。这快把我逼疯了!以前,我是用画布和ScrollPane做的。请注意,FlowchartPanel扩展了JPanel。
public class Window extends JFrame{
private FlowchartPanel panel; // contains all the main graphics
private JScrollPane scrollpane; // contains panel
private int canvasWidth, canvasHeight; // the width and height of the canvas object in pixels
private Flowchart flowchart; // the flowchart object
public Window(Flowchart flowchart) {
super();
canvasWidth = 900;
canvasHeight = 700;
this.flowchart = flowchart;
flowchart.setWidth(canvasWidth);
flowchart.setHeight(canvasHeight);
setDefaultCloseOperation(EXIT_ON_CLOSE);
panel = new FlowchartPanel(flowchart);
panel.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
scrollpane = new JScrollPane();
scrollpane.setPreferredSize(new Dimension(canvasWidth, canvasHeight));
scrollpane.add(panel);
add(scrollpane);
//add(panel);
pack();
}
}发布于 2017-05-05 08:04:04
不要直接将组件添加到JScrollPane。
需要将组件添加到JScrollPane的JViewPort中
最简单的方法是使用:
JScrollPane scrollPane = new JScrollPane( panel );另一种方法是替换(添加)视口中的组件,方法是:
scrollPane.setViewportView( panel );新维度(
panel.setPreferredSize(canvasWidth,canvasHeight));
不要设置组件的首选大小。每个Swing组件都负责确定自己的首选大小。相反,重写自定义面板的getPreferredSize()方法以返回大小。这样,当自定义绘画更改时,您可以根据需要动态更改首选大小。
https://stackoverflow.com/questions/43794440
复制相似问题