Java 为什么我有一个可以工作的滚动面板和另一个不能';T
我在左边有一个很大的面板,上面有一个卷轴面板,效果很好;但在右上角,我有一个面板,也应该有一个滚动面板,但我似乎无法得到它,以便滚动条可以实际移动。它们只是保持饱满和不可移动。有人能帮我弄清楚这是为什么吗 此滚动面板是导致以下问题的面板:Java 为什么我有一个可以工作的滚动面板和另一个不能';T,java,swing,user-interface,Java,Swing,User Interface,我在左边有一个很大的面板,上面有一个卷轴面板,效果很好;但在右上角,我有一个面板,也应该有一个滚动面板,但我似乎无法得到它,以便滚动条可以实际移动。它们只是保持饱满和不可移动。有人能帮我弄清楚这是为什么吗 此滚动面板是导致以下问题的面板: public MapBuilderSidePanel(Dimension size){ tileSetPanel = new MapBuilderTileSetPanel(new Dimension(tileSetPanelWidth,tileSetP
public MapBuilderSidePanel(Dimension size){
tileSetPanel = new MapBuilderTileSetPanel(new Dimension(tileSetPanelWidth,tileSetPanelHeight),6,17, tileSetPanelHeight, tileSetPanelWidth, this);
scrollPane = new JScrollPane(tileSetPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//add(tileSet, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
setPreferredSize(size);
setBorder(new BevelBorder(BevelBorder.LOWERED));
setVisible(true);
}
这是我的小应用程序的代码,我已经尽可能多地删除了它。它是复制粘贴编译并可运行的
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.BevelBorder;
public class MapBuilderFrame extends JFrame {
/**
* @param args
*/
private String frameTitle = "Yoonsio Map Editor";
private int frameWidth = 1000;
private int frameHeight = 1000;
private MapBuilderPanel mainPanel;
private MapBuilderSidePanel myLeftSidePanel;
private MapBuilderSidePanel myRightSidePanel;
private JScrollPane scrollPane;
public MapBuilderFrame() {
myRightSidePanel = new MapBuilderSidePanel(new Dimension(200,2000));
mainPanel = new MapBuilderPanel(myRightSidePanel);
setTitle(frameTitle);
setSize(frameWidth,frameHeight);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setPreferredSize(new Dimension(450, 110));
setResizable(false);
scrollPane = new JScrollPane(mainPanel,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scrollPane, BorderLayout.CENTER);
add(myRightSidePanel, BorderLayout.EAST);
}
public static void main(String[] args) {
new MapBuilderFrame();
}
}
/***************************************************************************/
class MapBuilderPanel extends JPanel {
/**
* @param args
*/
private final int NUMERIC_TO_ASCII_OFFSET = 96;
private MapBuilderSidePanel mySidePanel;
private Rectangle hoverRect;
public MapBuilderPanel(MapBuilderSidePanel mySidePanel){
this.mySidePanel = mySidePanel;
setPreferredSize(new Dimension(2000,2000));
setBackground(new Color(0x00006595));
}
public void refresh(){
this.repaint();
}
public void paint(Graphics g){
g.setColor(new Color(0x00006595));
g.fillRect(0, 0, 2000, 2000);
}
}
/***************************************************************************/
class MapBuilderSidePanel extends JPanel {
/* Ypos = the Y position counter used to draw the grid*/
private MapBuilderTileSetPanel tileSetPanel;
/* Width and Heights pertaining to the tileset grid */
private int tileSetPanelHeight = 400;
private int tileSetPanelWidth = 175;
private JScrollPane scrollPane;
/*only constructor needed*/
public MapBuilderSidePanel(Dimension size){
tileSetPanel = new MapBuilderTileSetPanel(new Dimension(tileSetPanelWidth,tileSetPanelHeight),6,17, tileSetPanelHeight, tileSetPanelWidth, this);
scrollPane = new JScrollPane(tileSetPanel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//add(tileSet, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
setPreferredSize(size);
setBorder(new BevelBorder(BevelBorder.LOWERED));
setVisible(true);
}
// Remember override paintComponent(); NOT paint().
public void paintComponent(Graphics g){
}
public void loadTileSet(String filename, int r, int c){
//tileSet.loadTileSet(filename, r, c);
}
public void refresh(){
tileSetPanel.repaint();
}
}
/***************************************************************************/
class MapBuilderTileSetPanel extends JPanel{
private MapBuilderSidePanel myParent;
MapBuilderTileSetPanel(Dimension size, int columns, int rows, int parentPanelHeight, int parentPanelWidth, MapBuilderSidePanel mp){
this.myParent = mp;
setBackground(Color.BLACK);
setPreferredSize(size);
setSize(1000,1000);
setBorder(new BevelBorder(BevelBorder.LOWERED));
}
public void loadTileSet(String filename, int c, int r){
}
public void paintComponent(Graphics g){
g.setColor(new Color(0x00550055));
g.drawRect(25, 25, 25, 25);
g.setColor(new Color(0x0000FF00));
g.drawRect(30, 30, 200, 500);
}
public MapBuilderTileSetPanel returnThis() {
return this;
}
}
您的滚动窗格没有首选大小,因此它正在缩放到内部组件 加:
正如mKorbel所指出的,当您验证它时,调用上层类方法。您的滚动窗格没有首选的大小,因此它正在扩展到内部组件 加:
正如mKorbel所指出的,当您验证它时,调用上层类方法。您可能正在寻找
JScrollNavigator
,请检查
关于SSCCE的注意事项:
- Swing GUI对象应仅在上构造和操作
- “Swing程序应该重写
,而不是重写paintComponent()
”paint()
- 如前所述,当您真正想要覆盖
时,不要使用getPreferredSize()
setPreferredSize()
- 如前所述,每当滚动窗格小于所附组件时,滚动条将自动显示
JScrollNavigator
,已检查
关于SSCCE的注意事项:
- Swing GUI对象应仅在上构造和操作
- “Swing程序应该重写
,而不是重写paintComponent()
”paint()
- 如前所述,当您真正想要覆盖
时,不要使用getPreferredSize()
setPreferredSize()
- 如前所述,每当滚动窗格小于所附组件时,滚动条将自动显示
- +1 for.+1 for.谢谢你。我一定会研究你提供的笔记,我真的很感谢这些有用的链接。我有点害怕线程,因此我避免使用事件调度线程,但我会注意学习并正确使用它。我倾向于详细讨论理论原因,但这里是实际原因。谢谢你。我一定会研究你提供的笔记,我真的很感谢这些有用的链接。我有点害怕线程,因此我避免使用事件调度线程,但我会注意学习并正确使用它。我倾向于详细讨论理论原因,但这里是实际的原因。
scrollPane.setPreferredSize( new Dimension(200, 200));