我正在尝试让JScrollPane在此程序中始终滚动。即使在单击按钮后图像缩小,我也试图让它滚动。我可以让滚动窗格仅在JFrame最大化时滚动。此外,一旦单击按钮,JScrollPane内的图像减少了一半,JScrollPane就不再出现了。我认为我可以让JScrollPane始终拥有水平和垂直滚动条,方法是使Panel保持它比容纳Panel的JFrame大得多。请参阅下面的代码并尽可能帮助调试。
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());
}
}
感谢您阅读所有这些内容。我感谢任何恼人的讽刺评论,以及任何实际的帮助,使我成为一个更好的程序员和世界各地的其他人更好的程序员,正在阅读他们最喜欢的搜索引擎后阅读。 :)