Java 在JScrollingPanel中滚动JPanel
我正在尝试为我的platformer游戏制作一个关卡编辑器,我希望我的关卡是100乘100格 到目前为止,编辑器可以工作,但我无法滚动浏览JPanel。我一直在玩,我已经做了一个小的测试类拨弄,我将张贴Java 在JScrollingPanel中滚动JPanel,java,swing,jpanel,jscrollpane,Java,Swing,Jpanel,Jscrollpane,我正在尝试为我的platformer游戏制作一个关卡编辑器,我希望我的关卡是100乘100格 到目前为止,编辑器可以工作,但我无法滚动浏览JPanel。我一直在玩,我已经做了一个小的测试类拨弄,我将张贴如果您运行它,它所做的一切都会显示网格。但是,如果我调出两个变量(我会在哪里注释),它可以显示一个图像并根据该图像的大小滚动 我只想要JPanel的滚动功能,这样我就可以滚动我的100x100平方米 导入java.awt.BorderLayout; 公共类ScrollPaneJ扩展了JFrame{
如果您运行它,它所做的一切都会显示网格。但是,如果我调出两个变量(我会在哪里注释),它可以显示一个图像并根据该图像的大小滚动 我只想要JPanel的滚动功能,这样我就可以滚动我的100x100平方米
导入java.awt.BorderLayout;
公共类ScrollPaneJ扩展了JFrame{
//设置面板
私有JPanel内容窗格;
私有JScrollPane滚动窗格;
//网格的尺寸/变量
int size=16;
int-startX=112;
int startY=48;
整数宽度=30;
整数高度=30;
//这是网格
字符串[][]网格=新字符串[宽度][高度];
//这是来自完整的编辑器类
String currentImage=新字符串(“平台”);
ImageIcon currentBackIcon=新的ImageIcon(“Resources/backdirttile.jpg”);
/**
*启动应用程序。
*/
公共静态void main(字符串[]args){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
//添加滚动窗格
ScrollPaneJ frame=新的ScrollPaneJ();
frame.setVisible(true);
}捕获(例外e){
e、 printStackTrace();
}
}
});
}
/**
*创建框架。
*/
公共卷轴panej(){
setTitle(“滚动窗格应用程序”);
设置尺寸(新尺寸(300200));
挫折背景(颜色:灰色);
setDefaultCloseOperation(关闭时退出);
//定义顶部和底部面板,底部是我认为我是什么
//在上面画图,上面是滚动面板的位置,我复制了这个代码
//我不太清楚它是如何工作的
JPanel-topPanel=新的JPanel();
JPanel bottomPanel=newjpanel(newgridlayout());
bottomPanel.setLayout(新的BorderLayout());
getContentPane().add(底部面板);
setLayout(新的BorderLayout());
getContentPane().add(topPanel);
//这就是我说的标签
Icon image=new-ImageIcon(“src/MenuDesign.jpg”);
JLabel标签=新的JLabel(图像);
//创建选项卡式窗格
//如果将其设置为“标签”而不是“底部面板”,则可以滚动
//通过标签的大小
scrollPane=新的JScrollPane(底部面板);
滚动窗格.设置边界(40,40,100,100);
//在这里也设置它的标签。
scrollPane.getViewport().add(底部面板);
//我希望这会迫使滚动条,但它没有做什么
滚动窗格
.setHorizontalScrollBarPolicy(JScrollPane.HorizontalScrollBar,根据需要);
滚动窗格
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL\u SCROLLBAR\u根据需要);
滚动窗格.立根(50,30,300,50);
JPanel contentPane=新的JPanel(null);
setPreferredSize(新维度(500400));
添加(滚动窗格);
添加(滚动窗格,BorderLayout.CENTER);
init();
}
公共void init(){
//这会将网格设置为空
对于(int x=0;x
scrollPane.getViewport().add(底部面板)代码>应该更像scrollPane.getViewportView(底部面板)代码>
JScrollPane
,JViewport
视图的自定义组件中完成JScrollPane
需要两件事,首先是组件想要的大小(“code>首选大小”)和视口视图的大小。如果组件没有实现可滚动的接口,那么组件的首选大小也将用于确定。这就是为什么JLabel
可以工作的原因
JScrollPane
有一个JViewport
作为主要子组件。J
import java.awt.BorderLayout;
public class ScrollPaneJ extends JFrame {
// setting the panels
private JPanel contentPane;
private JScrollPane scrollPane;
// dimensions/ variables of the grid
int size = 16;
int startX = 112;
int startY = 48;
int width = 30;
int height = 30;
// this is the grid
String[][] grid = new String[width][height];
// this is from the full editor class
String currentImage = new String("platform");
ImageIcon currentBackIcon = new ImageIcon("Resources/backdirttile.jpg");
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
// adding the scrollpane
ScrollPaneJ frame = new ScrollPaneJ();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ScrollPaneJ() {
setTitle("Scrolling Pane Application");
setSize(new Dimension(300, 200));
setBackground(Color.gray);
setDefaultCloseOperation(EXIT_ON_CLOSE);
// defining the top and bottom panels, bottom is what I think I'm
// drawing on, top is where the scrollpanel goes, I copied this code
// from the internet and I'm not too sure how it works
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel(new GridLayout());
bottomPanel.setLayout(new BorderLayout());
getContentPane().add(bottomPanel);
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
// this is the label I was talking about
Icon image = new ImageIcon("src/MenuDesign.jpg");
JLabel label = new JLabel(image);
// Create a tabbed pane
// if you set it to say label instead of bottomPanel, you can scroll
// through the size of the label
scrollPane = new JScrollPane(bottomPanel);
scrollPane.setBounds(40, 40, 100, 100);
// set it label here as well.
scrollPane.getViewport().add(bottomPanel);
// I was hoping this would force the scrollbar in but it does nothing
scrollPane
.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane
.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(50, 30, 300, 50);
JPanel contentPane = new JPanel(null);
contentPane.setPreferredSize(new Dimension(500, 400));
contentPane.add(scrollPane);
topPanel.add(scrollPane, BorderLayout.CENTER);
init();
}
public void init() {
// this sets the grid to empty
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
grid[x][y] = "";
}
}
}
@Override
public void paint(Graphics g) {
// this paints the grid
super.paint(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.black);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
g2d.drawRect(x * size + startX, y * size + startY, size, size);
if (grid[x][y].equals("")) {
g2d.drawImage(currentBackIcon.getImage(),
x * size + startX, y * size + startY, null);
}
g2d.setColor(Color.black);
g2d.drawRect((x * size) + 1 + startX, (y * size) + 1 + startY,
size, size);
}
}
}
public void drawTile() {
// this isn't enabled which is why you can't paint the grid, however it
// would change the tile of the square you're mouse is on, to the
// current tile, it works and isn't really important for what i need
// help with
PointerInfo a = MouseInfo.getPointerInfo();
Point b = a.getLocation();
int mouseX = (int) b.getX();
int mouseY = (int) b.getY();
int gMX = ((mouseX - 48) / 16) - 4;
int gMY = ((mouseY - 48) / 16) - 3;
grid[gMX][gMY] = currentImage;
repaint();
}
}
JPanel p = new JPanel();
add(new JScrollPane(p));