JFrame滚动条和Canvas
JFrame Scroll bar and Canvas
我在尝试向具有 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
是徒劳的,因为后面的滚动窗格会忽略它。
当您的图像大于 JScrollPane
时,只需调用 canvas.setPreferredSize(imageWidth, imageHeight);
我在尝试向具有 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
是徒劳的,因为后面的滚动窗格会忽略它。
当您的图像大于 JScrollPane
时,只需调用 canvas.setPreferredSize(imageWidth, imageHeight);