Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未将JPanel添加到JFrame_Java_Swing_Jframe_Jpanel_Paintcomponent - Fatal编程技术网

Java 未将JPanel添加到JFrame

Java 未将JPanel添加到JFrame,java,swing,jframe,jpanel,paintcomponent,Java,Swing,Jframe,Jpanel,Paintcomponent,当我尝试将面板添加到框架中时,没有得到任何结果(因此我尝试将面板的背景设置为红色)。此外,每当我在Vertex类中调用linkedList时,我都会收到一个错误消息:线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常。我现在的主要问题是小组 绘制面板类 @SuppressWarnings("serial") public class DrawPanel extends JPanel { Project4Test test = new P

当我尝试将面板添加到框架中时,没有得到任何结果(因此我尝试将面板的背景设置为红色)。此外,每当我在Vertex类中调用linkedList时,我都会收到一个错误消息:线程“AWT-EventQueue-0”java.lang.NullPointerException中出现异常。我现在的主要问题是小组

绘制面板类

@SuppressWarnings("serial")
public class DrawPanel extends JPanel {
Project4Test test = new Project4Test();
Graph graph;
Vertex v;

public DrawPanel() {
    setBackground(Color.red);
    setPreferredSize(new Dimension(500,500));

}

public void paintComponent(Graphics page) {
    Graphics2D page2 = (Graphics2D) page;
    page2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    page2.setColor(Color.black);

    Iterator iterator = test.hashEdge.entrySet().iterator();
    while(iterator.hasNext()) {
        Map.Entry dualEntry = (Map.Entry)iterator.next();
        Edge e = (Edge) dualEntry.getValue();
        drawEdge(page2, e.vertex1, e.vertex2);
    }

    ListIterator<Vertex> listIterator = v.linkedList.listIterator();
    while(listIterator.hasNext()) {
    drawEdge(page2, v, listIterator.next());
    }
}

public void drawEdge(Graphics2D page2, Vertex vertex1, Vertex vertex2) {
    //x1, y1, x2, y2
    page2.setColor(Color.black);
    page2.draw(new Line2D.Double(vertex1.latitude, vertex1.longitude, vertex2.latitude, vertex2.longitude));
}
}
@SuppressWarnings(“串行”)
公共类DrawPanel扩展了JPanel{
Project4Test测试=新的Project4Test();
图形;
顶点v;
公共事务委员会(){
挫折地面(颜色:红色);
设置首选尺寸(新尺寸(500500));
}
公共组件(图形页){
Graphics2D第2页=(Graphics2D)第页;
页面2.setRenderingHint(RenderingHits.KEY\u抗锯齿,RenderingHits.VALUE\u抗锯齿\u开启);
第2页设置颜色(颜色为黑色);
迭代器迭代器=test.hashEdge.entrySet().Iterator();
while(iterator.hasNext()){
Map.Entry dualEntry=(Map.Entry)迭代器.next();
Edge e=(Edge)dualEntry.getValue();
抽屉边缘(第2页,e.vertex1,e.vertex2);
}
ListIterator ListIterator=v.linkedList.ListIterator();
while(listIterator.hasNext()){
page2,v,listIterator.next());
}
}
公共空心抽屉边缘(Graphics2D第2页,顶点顶点1,顶点2){
//x1,y1,x2,y2
第2页设置颜色(颜色为黑色);
page2.绘制(新的Line2D.Double(顶点1.纬度,顶点1.经度,顶点2.纬度,顶点2.经度));
}
}
测试班

@SuppressWarnings("serial")
public class Project4Test {
static int numVertices = 0;
static Map<String, Vertex> hashVertex = new HashMap<String, Vertex>();
static Map<String, Edge> hashEdge = new HashMap<String, Edge>();

//Map with String intersectionID as the key, and Vertexes as the value
@SuppressWarnings({ "resource", "unused" })
public static void main(String[] args) {
    JFrame frame = new JFrame("Map");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(true);
    DrawPanel drawpanel = new DrawPanel();
    frame.setSize(660, 630);
    frame.add(drawpanel);

    //frame.setExtendedState(Frame.MAXIMIZED_BOTH);



    File inFile = new File(args[0]);
    //frame.setExtendedState(Frame.MAXIMIZED_BOTH);
    //DrawPanel drawpanel;
    //frame.setVisible(true);


    try {
        Scanner scan = new Scanner(inFile);
        Graph newGraph = new Graph(0, false);

        while(scan.hasNext()) {
            String temp = scan.nextLine();
            String[] info = temp.split("\\t", 4);
            String interRoad = info[0];

            //If it is an intersection
            if(info[0].equalsIgnoreCase("i")) {
                String vertexID = info[1];
                double x = Double.parseDouble(info[2]);
                double y = Double.parseDouble(info[3]);
                //System.out.println(interRoad + " " + vertexID + " " + x + " " + y);
                //Insert to vertex here
                Vertex tempVertex = new Vertex(vertexID, numVertices, x , y);
                hashVertex.put(vertexID, tempVertex);
                numVertices++;
            }//end of intersection

            //Update graph to have the number of vertices
            newGraph = new Graph(numVertices, false);

            //If it is a road
            if(info[0].equalsIgnoreCase("r")) {
                String roadID = info[1];
                String vertexID1 = info[2];
                String vertexID2 = info[3];

                Vertex vertex1 = hashVertex.get(vertexID1);
                Vertex vertex2 = hashVertex.get(vertexID2);
                //System.out.println(interRoad + " " + roadID + " " + vertexID1 + " " + vertexID2);
                Edge newEdge = new Edge(roadID, vertex1, vertex2);
                hashEdge.put(roadID, newEdge);

                //Essentially saying linkedList(v1).add(v2) because if it's a road, it's inherently connected
                vertex1.linkedList.add(vertex2);
                vertex2.linkedList.add(vertex1);
                //drawpanel = new DrawPanel(newGraph);
                frame.add(drawpanel);
            }
        }//end of while loop
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

//      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true); 
} //end of main


}
@SuppressWarnings(“串行”)
公共类项目4测试{
静态int数值=0;
静态映射hashVertex=新HashMap();
静态映射hashEdge=新HashMap();
//以字符串intersectionID为键,顶点为值的贴图
@SuppressWarnings({“资源”,“未使用的”})
公共静态void main(字符串[]args){
JFrame=新JFrame(“地图”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setresizeable(true);
DrawPanel DrawPanel=新的DrawPanel();
框架设置尺寸(660630);
添加框架(drawpanel);
//frame.setExtendedState(frame.MAXIMIZED_两者);
文件填充=新文件(args[0]);
//frame.setExtendedState(frame.MAXIMIZED_两者);
//拖板拖板;
//frame.setVisible(true);
试一试{
扫描仪扫描=新扫描仪(填充);
图形newGraph=新图形(0,false);
while(scan.hasNext()){
String temp=scan.nextLine();
字符串[]信息=临时拆分(\\t“,4);
字符串中断=信息[0];
//如果是十字路口
if(信息[0]。相等信号案例(“i”)){
字符串vertexID=info[1];
double x=double.parseDouble(信息[2]);
double y=double.parseDouble(信息[3]);
//System.out.println(interload+“”+vertexID+“”+x+“”+y);
//在此处插入到顶点
顶点温度顶点=新顶点(顶点ID、numVertices、x、y);
hashVertex.put(vertexID,tempVertex);
numVertices++;
}//交叉口终点
//更新graph以获得顶点数
newGraph=新图形(数值,false);
//如果是一条路
if(信息[0]。相等信号案例(“r”)){
字符串roadID=info[1];
字符串vertexID1=info[2];
字符串vertexID2=info[3];
VertexVertex1=hashVertex.get(vertexID1);
VertexVertex2=hashVertex.get(vertexID2);
//System.out.println(interload+“”+roadID+“”+vertexID1+“”+vertexID2);
Edge NEWDGE=新边(道路ID、顶点1、顶点2);
hashEdge.put(roadID,newEdge);
//本质上说是linkedList(v1)。add(v2),因为如果它是一条路,那么它本身就是连通的
vertex1.linkedList.add(vertex2);
vertex2.linkedList.add(vertex1);
//drawpanel=新的drawpanel(newGraph);
添加框架(drawpanel);
}
}//while循环结束
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}//主管道末端
}

首先,确保通过在覆盖中调用super的方法调用JPanel自己的
paintComponent
方法,因为这将允许JPanel绘制背景色并进行任何其他内务处理图形。换言之,改变这一点:

public void paintComponent(Graphics page) {
    Graphics2D page2 = (Graphics2D) page;
为此:

@Override  // don't forget this
protected void paintComponent(Graphics page) {  // method should be protected
    super.paintComponent(page);  // ****** key to add this and call it first
    Graphics2D page2 = (Graphics2D) page;

至于NullPointerException,您需要学习如何调试NPE(NullPointerException)的一般概念。您应该仔细检查抛出它的行,异常的stacktrace会告诉您,找出哪个变量为null,然后回溯到代码中查看原因。相信我,您会一次又一次地遇到这些问题。

首先,请务必在覆盖中调用super的方法来调用JPanel自己的
paintComponent
方法,因为这将允许JPanel绘制背景色并进行任何其他内务处理图形。换言之,改变这一点:

public void paintComponent(Graphics page) {
    Graphics2D page2 = (Graphics2D) page;
public class DrawPanel extends JPanel 
{
    Project4Test test = new Project4Test();
为此:

@Override  // don't forget this
protected void paintComponent(Graphics page) {  // method should be protected
    super.paintComponent(page);  // ****** key to add this and call it first
    Graphics2D page2 = (Graphics2D) page;
至于NullPointerException,您需要学习如何调试NPE(NullPointerException)的一般概念。您应该仔细检查抛出它的行,异常的stacktrace会告诉您,找出哪个变量为null,然后回溯到代码中查看原因。相信我,你会一次又一次地碰到这些

public class DrawPanel extends JPanel 
{
    Project4Test test = new Project4Test();
代码的整个结构都是错误的。您不应该有创建Project4Test类的代码

DrawPanel
类应该只做一件事,那就是对面板进行自定义绘制

为了完成绘画,你需要数据。因此,
DrawPanel
类需要如下方法:
setHashVertex(…)
setHashEdge(…)