2

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

此滚动面板是导致问题的原因:

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


}
4

2 回答 2

2

您的 scrollPane 没有首选大小,因此它正在缩放到内部组件。

添加:

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

同样正如 mKorbel 指出的那样,在您覆盖它时调用上层类方法。

于 2013-10-08T13:52:29.070 回答
2

您可能正在寻找JScrollNavigator,检查这里

图片

关于您的 SSCCE 的注意事项:

  • Swing GUI 对象应该事件分派线程上构建和操作。

  • “Swing 程序应该覆盖paintComponent()而不是覆盖paint()。”—<a href="http://www.oracle.com/technetwork/java/painting-140037.html#callbacks" rel="nofollow noreferrer">在 AWT 和 Swing 中绘画: 绘画方法

  • setPreferredSize()当你真的想要覆盖时不要使用,正如这里getPreferredSize()所讨论的。

  • 只要滚动窗格小于封闭的组件,滚动条就会自动出现,如此所述。

于 2013-10-08T17:07:50.943 回答