下面列出了怎么用javax.swing.ScrollPaneLayout的API类实例代码及写法,或者点击链接到github查看源代码。
public CustomScroll(JComponent component) {
scr = new JScrollPane(component);
scr.setBorder(null);
scr.setViewportBorder(null);
scr.setBorder(BorderFactory.createEmptyBorder());
scr.getViewport().setOpaque(false);
// scr.setOpaque(false);
// scr.setBackground(new Color(0, 0, 0, 5));
verticalScrollBar = scr.getVerticalScrollBar();
verticalScrollBar.setVisible(false);
verticalScrollBar.setOpaque(false);
verticalScrollBar.setUI(new MyScrollBarUI());
verticalScrollBar.setUnitIncrement(16);
horizontalScrollBar = scr.getHorizontalScrollBar();
horizontalScrollBar.setVisible(false);
horizontalScrollBar.setOpaque(false);
horizontalScrollBar.setUI(new MyScrollBarUI());
JLayeredPane layeredPane = new JLayeredPane();
layeredPane.setLayer(verticalScrollBar, JLayeredPane.PALETTE_LAYER);
layeredPane.setLayer(horizontalScrollBar, JLayeredPane.PALETTE_LAYER);
scr.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scr.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scr.setLayout(new ScrollPaneLayout() {
@Override
public void layoutContainer(Container parent) {
viewport.setBounds(0, 0, getWidth(), getHeight());
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
displayScrollBarsIfNecessary(viewport);
}
});
}
});
layeredPane.add(horizontalScrollBar);
layeredPane.add(verticalScrollBar);
layeredPane.add(scr);
setLayout(new BorderLayout() {
@Override
public void layoutContainer(Container target) {
super.layoutContainer(target);
int width = getWidth();
int height = getHeight();
scr.setBounds(0, 0, width, height);
int scrollBarSize = 10;
int cornerOffset = verticalScrollBar.isVisible() &&
horizontalScrollBar.isVisible() ? scrollBarSize : 0;
if (verticalScrollBar.isVisible()) {
verticalScrollBar.setBounds(width - scrollBarSize, 0,
scrollBarSize, height - cornerOffset);
}
if (horizontalScrollBar.isVisible()) {
horizontalScrollBar.setBounds(0, height - scrollBarSize,
width - cornerOffset, scrollBarSize);
}
}
});
add(layeredPane, BorderLayout.CENTER);
layeredPane.setBackground(Color.BLUE);
}
/**
* Constructs a new PluginHistory.
*/
public MarvinPluginHistory()
{
frameHistory = this;
this.setLayout(new BorderLayout ());
setResizable(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setTitle("Plug-ins history");
listMarvinImage = new LinkedList<MarvinImage>();
listMarvinAttributes = new LinkedList<MarvinAttributes>();
listPluginName = new LinkedList<String>();
panelPlugin = new JPanel();
scrollPanelPlugins = new JScrollPane
(
panelPlugin,
ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED
);
scrollPanelPlugins.setLayout(new ScrollPaneLayout());
add(scrollPanelPlugins);
panelButtonHistory = new JPanel();
buttonExportHistortAsImage = new JButton("Export as Image");
buttonExportHistortAsImage.addActionListener(new ExportButtonHandler());
buttonExportHistortAsImage.setMnemonic('I');
// the action listener is still not created
// so no button appears
//btnExportHistoricText = new JButton ("Export as Text");
//btnExportHistoricText.addActionListener (new ExportTexButtonHandler ());
// ActionListener "ExportTextButtonHandler" need to be created!!!
//btnExportHistoricText.setMnemonic('T');
panelButtonHistory.add(buttonExportHistortAsImage);
//jpBtnHistoric.add (btnExportHistoricText);
this.add(panelButtonHistory, BorderLayout.PAGE_END);
}