我在左边有一个大面板,周围有一个滚动面板,它工作正常;但是在右上角我有一个面板,它也应该有一个滚动面板,但我似乎无法得到它,以便滚动条可以实际移动。他们只是保持饱满和不动。有人可以帮我弄清楚这是为什么吗?
此滚动面板是导致问题的原因:
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;
}
}