为什么我有一个可以工作的scrollPanel而另一个没有?

时间:2013-10-08 13:33:20

标签: java swing user-interface

我左边有一个大面板,上面有一个scrollPanel,工作正常;但是在右上角我有一个面板也应该有一个scrollPanel,但我似乎无法得到它,以便滚动条实际上​​可以移动。他们只是保持充实和不可动摇。有人可以帮我弄清楚这是为什么吗?

此scrollPanel导致问题:

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;
}


}

2 个答案:

答案 0 :(得分:2)

您的scrollPane没有优先大小,因此它会缩放到内部组件。

添加:

scrollPane.setPreferredSize( new Dimension(200, 200));

同样正如mKorbel所指出的那样,当你离开它时调用上层方法。

答案 1 :(得分:2)

您可能正在寻找JScrollNavigator,已审核here

image

关于您的SSCCE的说明:

  • 应在event dispatch thread构建和操作Swing GUI对象。

  • “Swing程序应该覆盖paintComponent(),而不是覆盖paint()。” - Painting in AWT and Swing: The Paint Methods

  • 当您真正想要覆盖setPreferredSize()时,请不要使用getPreferredSize(),如上所述here

  • 滚动条会在滚动窗格小于所附组件时自动显示,如here所示。