在回顾了这个问题的现有答案之后,我仍然处于黑暗中。我试图在选项卡框架内实现可滚动面板。我正在使用JScrollPane
和JTabbedPane
。但是,滚动不起作用。
这是我的代码:
public TabbedFrame(List<Panel> panels) {
super(new GridLayout(1, 1));
JTabbedPane tabbedPane = new JTabbedPane();
int layerNum = 0;
for (Panel p: panels){
JScrollPane scrollPane = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrollPane.setViewportView(p);
scrollPane.setPreferredSize(new Dimension(500, 500));
tabbedPane.addTab("Layer " + layerNum, null, scrollPane, "Layer " + layerNum);
layerNum++;
}
//Add the tabbed pane to this panel.
add(tabbedPane);
//The following line enables to use scrolling tabs.
tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
}
public class Panel extends JPanel{
private static final long serialVersionUID = 4109034958236244752L;
private Design design;
public Panel(Design design){
this.design = design;
this.setSize(design.getCanvasWidth(), design.getCanvasHeight());
}
public void paint(Graphics G) {
G.setColor(design.getColor());
design.draw(G);
}
public int getWidth() { return design.getCanvasWidth(); }
public int getHeight() { return design.getCanvasHeight(); }
}
我将非常感谢您提供的任何帮助。
答案 0 :(得分:0)
发现问题。 我也应该设置面板的可取尺寸:
for (Panel p: panels){
JScrollPane scrollPane = new JScrollPane(
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
p.setPreferredSize(new Dimension(1000, 1000));
scrollPane.setViewportView(p);
scrollPane.setPreferredSize(new Dimension(500, 500));
tabbedPane.addTab("Layer " + layerNum, null, scrollPane, "Layer " + layerNum);
layerNum++;
}