我想将JPG放入框架(可滚动)。
我尝试修改此内容:http://www.roseindia.net/tutorial/java/swing/multipleFrames.html
我的代码没有将图片放入框架中。
// THIS CALLS STATIC IMPORT DECLARATIONS
// THESE THREE IMPORTS ARE NEEDED FOR BUILDING THE FRAMES AND DESK
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
// THESE IMPORTS ARE NEEDED FOR THE GRAPHICS (BUFFEREDIMAGE)
import java.awt.image.BufferedImage;
import java.awt.*;
import java.awt.Image.*;
import javax.imageio.ImageIO;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
// THIS SETS THE TITLE OF THE PROGRAM AND OPENS THE PROGRAM
class Scratch {
// THIS SETS THE MAIN METHOD
public static void main (String args []) {
// THIS LOADS THE FIRST IMAGE
BufferedImage image1 = null;
try {
image1 = ImageIO.read(new File("scan0001.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
Toolkit toolkit = Toolkit.getDefaultToolkit();
Dimension dimension = toolkit.getScreenSize();
JImagePanel imagePanel1 = new JImagePanel(image1,0,0);
imagePanel1.setPreferredSize(new Dimension(image1.getWidth(), image1.getHeight()));
JScrollPane scrollPane1 = new JScrollPane();
scrollPane1.setSize(100,100);
scrollPane1.getViewport().add(imagePanel1);
JPanel topPanel1 = new JPanel();
topPanel1.setLayout(new BorderLayout());
topPanel1.add(scrollPane1, BorderLayout.CENTER);
// THIS BUILDS THE DESKTOP IN WHICH TO PLACE THE INTERNAL FRAMES
JDesktopPane desk = new JDesktopPane();
// THIS BUILDS THE BIG (MAIN) FRAME AND SETS THE TITLE
JFrame frame = new JFrame("Big Frame");
// THIS ALLOWS BIG (MAIN) FRAME TO CLOSE AND RETURN TO CMD PROMPT
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// THIS SETS THE SIZE OF THE BIG (MAIN) FRAME
frame.setSize(600,400);
// THIS SETS THE LOCATION OF THE BIG (MAIN) FRAME ON THE SCREEN
frame.setLocation(100,100);
// THIS MAKES THE BIG (MAIN) FRAME VISIBLE
frame.setVisible(true);
// THIS BUILDS THE INTERNAL FRAMES
JInternalFrame frame1 = new JInternalFrame("Frame 1", true, true, true, true);
// THIS SETS LOCATION AND SIZE
frame1.setBounds(20,200,150,100);
// THIS MAKES THE INTERNAL FRAMES VISIBLE
frame1.setVisible(true);
// THIS PLACES THE JINTERNALFRAME ONTO THE DESKTOP
desk.add(frame1);
// THIS ADDS THE DESKTOP TO THE BIG (MAIN) FRAME
frame.add(desk);
class JImagePanel extends JPanel {
private BufferedImage image1;
int x, y;
public JImagePanel(BufferedImage image1, int x, int y){
super();
this.image1 = image1;
this.x = x;
this.y = y;
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image1, 0, 0, null);
}
}
// THESE ARE NEEDED TO CLOSE THE PROGRAM
}
}