我通过一些简单的应用工作,以熟悉Swing和遇到了问题。图像绘制作品的JFrame的,而不是JPanel的
我试图与按钮沿着包含的图像(在面板)的帧从图像放大/缩小。
我已经能够使添加图像的框架工作正常(尽管有一些框架尺寸问题,但这是另一回事),但是,当我调用相同的组件类将其添加到面板时,出现。我希望你们中的一位能够帮助解释这种情况。
CODE:
图像帧 - 正在按所示
class ImageFrame extends JFrame{
public ImageFrame(){
setTitle("Java Image Machine");
init();
pack();
}
public final void init(){
//ZoomPanel zoomPanel = new ZoomPanel();
//ImagePanel imagePanel = new ImagePanel();
ImageComponent component = new ImageComponent();
//this.add(zoomPanel, BorderLayout.CENTER);
this.add(component);
//this.add(imagePanel, BorderLayout.SOUTH);
}
}
但是,使用ImagePanel或直接ImageComponent呼叫同时添加ZoomPanel,不会:
class ImagePanel extends JPanel{
public ImagePanel(){
//setBorder(BorderFactory.createLineBorder(Color.black));
ImageComponent component = new ImageComponent();
add(component);
}
}
组件类:
class ImageComponent extends JComponent{
public ImageComponent(){
try{
image = ImageIO.read(new File("test1.bmp"));
}
catch (IOException e){
e.printStackTrace();
}
System.out.println("W: " + image.getWidth(this) + " H: " + image.getHeight(this));
}
public void paintComponent(Graphics g){
super.paintComponent(g);
if (image == null)
return;
width = image.getWidth(this);
height = image.getHeight(this);
//System.out.println("Image should be painted");
g.drawImage(image, 0, 0, null);
}
private Image image;
public int width;
public int height;
}
路径处于工作状态,但我觉得你的getPreferredSize倍率为我错过了什么。我无法获得框架来拾取尺寸。谢谢! – DivinusVox
+1高兴地工作 –