![](/img/trans.png)
[英]How do I scroll JScrollPane viewport containing a JPanel to a specific location
[英]How do I get the jscrollpane area to scroll?
我試圖讓JScrollPane在此程序中始終滾動。 我也試圖使它滾動,即使單擊按鈕后圖像縮小。 我可以使滾動窗格僅在JFrame最大化時滾動。 同樣,單擊按鈕后,一旦JScrollPane內部的圖像縮小了一半,JScrollPane將不再顯示。 我以為我可以使JScrollPane所容納的Panel遠大於容納該Panel的JFrame,從而使JScrollPane始終具有水平和垂直滾動條。 請查看下面的代碼,並在可能的情況下幫助調試。
package Ex3;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.AWTException;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Image;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
public class Ex3MF {
JFrame jframe = new JFrame("Crappy Screen Grabber 1.0");
ImageIcon icon = new ImageIcon(this.getClass().getClassLoader().getResource("screenshot-ex3mf.png"));
JLabel jl;
JScrollPane jsp;
Container c = jframe.getContentPane();
JButton button1 = new JButton("Resize Image");
static Ex3MF e;
static Toolkit t = Toolkit.getDefaultToolkit();
static Dimension d = t.getScreenSize();
static int x_width = d.width/2;
static int x_height = d.width/2;
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
void setupJFrame() {
jframe.setSize(300, 300);
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jframe.setLocationRelativeTo(null);
button1.addActionListener(new TrimImage());
p2.setSize(200,100);
//p2.setLayout(new BorderLayout());
p2.add(button1);
jframe.add(p2, BorderLayout.NORTH);
}
void image2ScrollPane(ImageIcon ic) {
setupJFrame();
p1.setSize(3000,3000);
p1.setLayout(new BorderLayout());
if (jl == null) {
jl = new JLabel("Crappy Screencapture", ic, JLabel.CENTER);
} else {
jl.setIcon(ic);
}
if (jsp == null) {
jsp = new JScrollPane(jl);
} else {
jsp.setViewportView(jl);
}
p1.add(jsp);
//c.add(p1);
//p1.add(jl); //and the jlabel to the panel
// jsp = new JScrollPane(p1); //and the panel to the ScrollPane
// c.add(jsp); //add the JScrollPane to the Container
jframe.add(p1, BorderLayout.CENTER);
jframe.setVisible(true);
jframe.pack();
}
void createScreenCapture() {
try {
Robot ro = new Robot();
Toolkit t = Toolkit.getDefaultToolkit();
final Dimension d = t.getScreenSize();
Rectangle re = new Rectangle(d.width, d.height);
BufferedImage image = ro.createScreenCapture(re);
//write screen capture to disk
ImageIO.write(image, "png", new File("screenshot-ex3mf.png"));
} catch(IOException ioe) {
System.out.println("IOException thrown " + ioe);
}
catch(AWTException awte) {
System.out.println("AWTException thrown " + awte);
}
}
ImageIcon resizeTheCapture() {
Image image = icon.getImage();
x_width = x_width / 2;
x_height = x_height / 2;
Image newimg = image.getScaledInstance((x_width), (x_height), java.awt.Image.SCALE_SMOOTH);
return new ImageIcon(newimg);
}
public static void main(String[] args) {
e = new Ex3MF();
e.createScreenCapture();
e.image2ScrollPane(e.icon);
System.out.println("\nProgram complete...");
}
} //end class
這句話后面是控制按鈕的代碼。
package Ex3;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class TrimImage implements ActionListener {
public void actionPerformed(ActionEvent ae) {
Ex3MF ex3 = new Ex3MF();
ex3.e.image2ScrollPane(ex3.e.resizeTheCapture());
}
}
感謝您閱讀所有這些內容。 我感謝給出的任何惱人的s瑣評論,也為使我成為一個更好的程序員以及世界上其他人在他們喜歡的搜索引擎中進行閱讀后變得更好的程序員提供了實際幫助。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.