提供一个setter ...
public void setPreferredScrollableViewportSize(Dimension size) {
this.preferredScrollableViewportSize = size;
}
现在,很明显,你getPreferredScrollableViewportSize
需要返回这个值...
public class CustomScroll extends JComponent implements Scrollable {
private Dimension preferredScrollableViewportSize = new Dimension(266, 300);
public CustomScroll() {
}
public void setPreferredScrollableViewportSize(Dimension size) {
this.preferredScrollableViewportSize = size;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return preferredScrollableViewportSize;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 64;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 64;
}
}
还应该设置这个在您的JScrollPane
包之前,它会更容易管理,否则,您需要invalidate
其中JScrollPane
所在的容器
Yo ü可能想通过Code Conventions for the Java TM Programming Language有一读,它将使人们更容易阅读您的代码,供您阅读别人
但是,这仍然只是得到最佳尺寸
没有它不会,它将使用preferredScrollableViewportSize
(假设你内JScrollPane
我的滚动窗格中调整到什么是它,所以它只是成长的按钮添加
在包装0
然后你做错事
JScrollPane scrollPane = new JScrollPane(leftTop);
customScroll t1 = new customScroll();
// This is wrong...
scrollPane.add(t1);
left.add(scrollPane);
你不应该添加任何东西到JScrollPane
,这不是他们的工作,相反,你需要在它
leftTop t1 = new CustomScroll();
JScrollPane scrollPane = new JScrollPane(leftTop);
left.add(scrollPane);
包裹组件
或其他一些...
我希望它保持一个固定的大小,所以我需要明确地设置它可以cann从
OT偏离是的,这就是preferredScrollableViewportSize
做
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class JavaApplication57 {
public static void main(String[] args) {
new JavaApplication57();
}
public JavaApplication57() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridBagLayout());
CustomScroll comp = new CustomScroll();
comp.setPreferredScrollableViewportSize(new Dimension(100, 100));
comp.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.weightx = 1;
gbc.insets = new Insets(5, 10, 10, 5);
for (int index = 0; index < 100; index++) {
JButton btn = new JButton("Hello world " + index);
comp.add(btn, gbc);
}
add(new JScrollPane(comp));
}
}
public class CustomScroll extends JComponent implements Scrollable {
private Dimension preferredScrollableViewportSize = new Dimension(266, 300);
public CustomScroll() {
}
public void setPreferredScrollableViewportSize(Dimension size) {
this.preferredScrollableViewportSize = size;
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return preferredScrollableViewportSize;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 64;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect,
int orientation, int direction) {
return 64;
}
}
}
请注意Java的命名约定 - http://www.oracle.com/technetwork/java/codeconventions -135099.html - 尤其是类名;另外,请提一下你面临的确切问题? –