繁体   English   中英

JPanel 问题 - 在正常外观上调整大小时重新绘制

[英]JPanel problem - repaint on resize on normal look

我对 JPanel 有奇怪的问题。 我正在尝试展示 svg 图像(SVG_class 扩展了来自蜡染罐的 JSVGCanvas)。 问题是当我启动这个程序时,我得到了这个

在此处输入图像描述

当我用指针框架调整大小时,我会得到这样的正常图片

在此处输入图像描述

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Ellipse2D;
import java.io.*;
import java.util.LinkedList;
import java.util.List;
import javax.swing.*;
import org.apache.batik.swing.JSVGCanvas;

public class Main {

static JScrollPane scrollpane;
// The frame.
protected JFrame frame;

public static void main(String[] args) {

final JFrame f = new JFrame("frame");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(700, 500);
f.setBackground(Color.blue);

SVG_class svg = new SVG_class();
JPanel p = new JPanel();
p.setSize(new Dimension(700, 500));
p.add(svg);
p.setBackground(Color.red);
scrollpane = new JScrollPane(p);
scrollpane.setPreferredSize(new Dimension(700, 500));

Container cont = f.getContentPane();
cont.add(scrollpane);

f.setVisible(true);
}

}

public class SVG_class extends JSVGCanvas {
private List<Type> list;
private int rad;
public boolean red_dot(int iX, int iY, String sX, String sY){
boolean b;
int x,y;
x=Math.abs(iX-(int)Double.parseDouble(sX));
y=Math.abs(iY-(getSize().height-(int)Double.parseDouble(sY)));
System.out.println("iX="+iX+" iY="+iY+" sX="+sX+" sY="+sY+" x="+x+" y="+y);
b=(x<=rad) &&(y<=rad);
return b;
}

    public SVG_class(){
    try {
    this.list=CSV.getCSV("map2");
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
    }
    String fileName = "map2";
    File file = new File("C:\\Users\\Gigabyte\\Desktop\\SVG\\"+fileName+".svg");
    try {
    this.setURI(file.toURL().toString());
                this.addMouseListener(new MouseListener() {
    @Override
    public void mouseReleased(MouseEvent e) {
    //
    }
    @Override
    public void mousePressed(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseExited(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    // TODO Auto-generated method stub
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    boolean found=false;
    // System.out.println("pritisnuo si x="+e.getX()+" y="+e.getY());
    int x,y;
    for(int i=0; i<list.size() && !found;i++ ){
    found=red_dot(e.getX(), e.getY(), list.get(i).getX(), list.get(i).getY());
    }
    if (found) {
    System.out.println("pritisao!!!!");
    }
    System.out.println(" ");
    }
    });
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

有人能告诉我什么是错误吗? 我尝试使用 p.repaint 但没有帮助。

发布SVG_class的源代码可能更容易看出问题所在。

事实上,我怀疑这个问题是因为你没有明确告诉它使用什么样的布局。

如果你想让svg占据整个 window 你可以做p.setLayout(new GridLayout(1, 1)); 创建p之后。

您应该调用f.setSize(700, 500); 就在展示之前。

SVG_class可能已经在构造函数中设置了它的大小。 不要这样做并在调用构造函数后定义大小。

编辑:尝试添加

f.pack();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM