0
在回顧了這個問題的現有答案之後,我仍然處於黑暗中。我試圖在標籤框架內實現可滾動面板。我正在使用JScrollPane
和JTabbedPane
。但是,滾動不起作用。使用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(); }
}
我會感激你可以提供任何幫助。
爲了更好地幫助越早,張貼[MCVE]或[簡要,獨立的,正確的示例](http://www.sscce.org /)。 –
*「但是,滾動不起作用。」* - 我猜是因爲您實際上從未實際將任何組件添加到'Panel'實例?繪製到它不會計算它的首選大小(你應該重寫'paintComponent(Graphics)'而不是'paint(Graphics'))。爲此,您可以*重寫* getPreferredSize()方法。但是'scrollPane.setPreferredSize(new Dimension(500,500));'在這裏沒有任何作用,它仍然會延伸到所有可用空間。但如前所述,如果這不能解決您的問題,請發佈一個MCVE。 –