我正在Windows 7上使用NetBeans 7.3.1上的JavaSE进行开发。
我javamain方法有以下调用
static Vector<Point2D> acceptedByFilter, coords;
// Some code to read the coords from a file
// Some code to filter coords and produce a subset called acceptedByFilter
DisplayInputPoints();
DisplayPointsAcceptedByFilter();
这些方法定义如下
static protected void DisplayPointsAcceptedByFilter(){
Panel panel=new Panel();
panel.DisplayInputPoints(acceptedByFilter, xMin, xMax, yMin, yMax, true, "Points accepted by filter");
}
static void DisplayInputPoints(){
Panel panel=new Panel();
panel.DisplayInputPoints(coords, xMin, xMax, yMin, yMax, true, "Original Points");
}
Panel. DisplayInputPoint定义如下
import javax.swing.JFrame;
import javax.swing.JPanel;
public
class Panel extends JPanel
{
public static void DisplayInputPoints(Vector<Point2D> coords, double xMin, double xMax, double yMin,
double yMax, boolean invert, String label){
JFrame frame = new JFrame(label);
Panel panel = new Panel();
panel.setPreferredSize(new Dimension((int)Math.round(xMax-xMin) + 10,
(int)Math.round(yMax-yMin) + 10));
panel.loadPoints(coords);
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
frame.repaint();
}
}
当我呼唤
DisplayInputPoints();
出现第一帧并显示和弦向量中的点。当我调用
DisplayPointsAcceptedByFilter();
我得到另一个帧,接受的ByFilter中的点出现在两个帧中。也就是说,第一帧被第二帧中的显示覆盖。
什么是最好的方法来阻止第一帧被应该只在第二帧覆盖?
您的代码结构似乎大错特错,这可能是您的主要问题。例如,
DisplayPointsAcctedByFilter
创建了一个Panel对象,该对象导致显示第二个JFrame。从Panel类中获取显示代码,它不属于那里并且导致了您的问题。要获得更详细和更好的帮助,请考虑在您的问题中提供更多信息,并可能创建和发布一个sscce。
编辑
考虑创建:
你有没有想过让Panel成为一个抽象类,也许为了两个目的专门改变它?我的意思是,我觉得你应该展示更多的代码来正确理解问题