我正在为我的项目制作一个关卡编辑器,所以我在JScrollPane中添加了一个JPanel,以便查看正在处理的关卡。这个想法是,如果级别太大,滚动窗格将处理它。问题是当级别变小时,当我尝试将面板的大小调整为小于滚动窗格的大小时,它会被拉伸以填充整个滚动窗格而不是我设置的大小。我似乎无法找到问题的解决方案。
编辑:我添加了一些示例代码,当窗口放大时,面板会拉伸以适应滚动窗格,而不是停留在示例中的500×500设置。设置JScrollPane:
public class Example {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
LevelView levelView = new LevelView();
JScrollPane pane = new JScrollPane(levelView);
frame.add(pane, BorderLayout.CENTER);
frame.setVisible(true);
}
}
的JPanel:
public class LevelView extends JPanel {
private static final long serialVersionUID = 1L;
public LevelView() {
Dimension dim = new Dimension(500, 500);
setPreferredSize(dim);
setMaximumSize(dim);
setMinimumSize(dim);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
System.out.println(getWidth() + " " + getHeight());
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
// Draw grid
g.setColor(Color.WHITE);
for (int x = 0; x < getWidth(); x += 50) {
g.drawLine(x, 0, x, getHeight());
}
for (int y = 0; y < getHeight(); y += 50) {
g.drawLine(0, y, getWidth(), y);
}
}
}
提前致谢!
答案 0 :(得分:2)
首先,请记住,组件的preferredSize
同时提供了视口的大小和JScrollPane
的大小提示
现在,在您从房间尖叫之前,Swing提供了Scrollable
界面,允许您向JScrollPane
和JViewport
提供更多信息,包括首选视图端口大小(允许JScrollPane
小于在布局时显示的视图的首选大小,并且,这是您感兴趣的,当视图小于视图时应如何调整视图的大小区域
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.Scrollable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
TestPane tp = new TestPane();
JSlider slider = new JSlider(50, 1024);
slider.setValue(200);
slider.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
int value = slider.getValue();
tp.apply(new Dimension(value, value));
tp.revalidate();
}
});
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(slider, BorderLayout.NORTH);
frame.add(new JScrollPane(tp));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel implements Scrollable {
private Dimension preferredSize = new Dimension(200, 200);
public TestPane() {
setBackground(Color.RED);
}
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
public void apply(Dimension size) {
preferredSize = size;
revalidate();
}
@Override
public Dimension getPreferredScrollableViewportSize() {
return new Dimension(200, 200);
}
@Override
public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
@Override
public int getScrollableBlockIncrement(Rectangle visibleRect, int orientation, int direction) {
return 128;
}
@Override
public boolean getScrollableTracksViewportWidth() {
return false;
}
@Override
public boolean getScrollableTracksViewportHeight() {
return false;
}
}
}
答案 1 :(得分:1)
尝试绘制您正在绘制的不同对象,并使用布局管理器将该对象添加到JPanel
。退房:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
有关布局管理器的更多信息。