正在覆盖javax.swing.JFrame
我正在Windows7上的NetBeans7.3.1上使用JavaSE进行开发 我的java main方法具有以下调用正在覆盖javax.swing.JFrame,java,swing,Java,Swing,我正在Windows7上的NetBeans7.3.1上使用JavaSE进行开发 我的java main方法具有以下调用 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 DisplayInputP
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();
Panel.DisplayInputPoints的定义如下
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");
}
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();
}
}
将显示第一帧,并显示坐标向量中的点。当我打电话时
DisplayPointsAcceptedByFilter();
我得到另一帧,acceptedByFilter中的点出现在两帧中。也就是说,第一帧被第二帧中的显示覆盖
阻止第一帧被第二帧中的内容覆盖的最佳方法是什么 您的代码结构似乎有些偏离,这可能是您的主要问题。比如说,
- 一方面,您不应该使用静态方法或字段。唯一的静态方法应该是主方法、关联方法和实用方法。唯一的静态字段应该是类字段,例如常量
- 您的
创建一个面板对象,该对象会导致第二个JFrame显示。将显示代码从Panel类中取出,它不属于Panel类,并且会导致您的问题DisplayPointsAcceptedByFilter
- 不要使用与Java核心类相同的名称命名类,例如Panel
- 您的Panel类甚至不是真正的OOP类,而是静态方法的持有者。这违反了所有OOP规则
- 如果这是我的代码,我会放弃它,从一开始就使用OOP原则重新编写它
编辑
考虑创建:
- 从文件中读取坐标的类。然后,它可以将坐标放入ArrayList中。它应该没有静态方法或字段
- 用于过滤坐标的类。它也应该没有静态方法或字段,也没有GUI代码
- 扩展用于显示数据的JPanel的GUI类
- GUI类接受坐标的数组列表,然后以某种方式绘制坐标
- GUI类使用setter方法接受新坐标,然后重新绘制新坐标
- 一个具有main方法的类,该类为要显示的GUI类创建JFrame,并协调所有其他类,从而启动它们
DisplayPointsAcceptedByFilter();