📄 scrollpanedemo.java
字号:
// ScrollPaneDemo.java
import java.awt.*;
class ScrollPaneDemo extends Frame
{
ScrollPaneDemo (String title)
{
super (title);
// Create a MediaTracker object to track image loading.
MediaTracker mt = new MediaTracker (this);
// Applications require a Toolkit for image loading.
Toolkit tk = Toolkit.getDefaultToolkit ();
// Get the next image.
Image im = tk.getImage ("iss.jpg");
// Add image to MediaTracker object.
mt.addImage (im, 0);
// Force image to load.
try
{
mt.waitForID (0);
}
catch (InterruptedException e) {}
// Create a scroll pane container.
ScrollPane sp = new ScrollPane ();
// Create picture component.
Picture pic = new Picture ();
// Add picture to scroll pane.
sp.add (pic);
// Draw image.
pic.draw (im);
// Add scroll pane to frame (in center of its border layout).
add (sp);
// Set frame background (around scroll pane component/container)
// to light gray.
setBackground (Color.lightGray);
// Set frame size to 400 by 400 pixels.
setSize (400, 400);
// Ensure that frame is visible.
setVisible (true);
}
public static void main (String [] args)
{
new ScrollPaneDemo ("Scroll Pane Demo");
}
}
class Picture extends Canvas
{
private Image image; // The current image.
public void draw (Image image)
{
this.image = image; // Save the current image.
repaint (); // Draw the image (update is called).
}
public Dimension getPreferredSize ()
{
// When the layout manager calls picture's getPreferredSize ()
// method, this method will return a Dimension object that
// tells the layout manager how much room to reserve for the
// picture component. Because the iss image is 640 x 481,
// these dimensions are returned.
return new Dimension (640, 481);
}
public void paint (Graphics g)
{
// Determine the upper-left image corner (x, y) coordinates so
// that the image will be centered.
int x = (getSize ().width - image.getWidth (this)) / 2;
int y = (getSize ().height - image.getHeight (this)) / 2;
// Draw the image.
g.drawImage (image, x, y, this);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -