gpt4 book ai didi

java - JFrame 滚动条和 Canvas

转载 作者:行者123 更新时间:2023-12-01 11:36:46 25 4
gpt4 key购买 nike

我在尝试向具有 Canvas 的 JFrame 添加水平滚动条时遇到问题。我在 Canvas 上绘制图像,有时这些图像会超出屏幕,因此我需要一个滚动条以使用户能够看到它们。

这是代码中的关键部分,如果需要任何其他代码,请告诉我,我会编辑。

有人能指出我做错了什么吗?

非常感谢J

public class TheFrame extends JFrame {

private static ThePanel canvas;
private String deckImagesToUse;

/**
* The constructor creates a Frame ready to display the cards
*/
public TheFrame(String cardImgFile) {


// Calls the constructor in the JFrame superclass passing up the name to
// display in the title
super("Window title");

//Using users choice of cards
deckImagesToUse = cardImgFile;

// When you click on the close window button the window will be closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// This has North, East, South, West and center positions for components
setLayout(new BorderLayout());

// This is what we will draw on (see the inner class below)
canvas = new ThePanel();
setSize(700, 300);
this.add(canvas, BorderLayout.CENTER);

//Scroll Bar
JScrollPane scrollPane = new JScrollPane(canvas);
//scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
this.getContentPane().add(scrollPane);
int desiredWidth = (Pack.cardsInPlay.size() * 70);
Dimension d = new Dimension(desiredWidth,300);
canvas.setPreferredSize(d);

setVisible(true); // Display the window


}

最佳答案

canvas = new ThePanel();
setSize(700, 300);
this.add(canvas, BorderLayout.CENTER);

设置大小并添加到 this 是徒劳的,因为稍后的滚动 Pane 会忽略它。

当您的图像大于 JScrollPane 时,只需调用 canvas.setPreferredSize(imageWidth, imageHeight);

关于java - JFrame 滚动条和 Canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29895421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com