gpt4 book ai didi

java - JPanel 未添加到 JFrame

转载 作者:行者123 更新时间:2023-12-01 18:15:20 24 4
gpt4 key购买 nike

当我尝试将面板添加到框架中时,我没有得到任何结果(因此我尝试将面板的背景设置为红色)。另外,每当我在 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("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


}

最佳答案

首先,请务必通过在覆盖中调用 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) 的一般概念。您应该仔细检查抛出它的行,异常的堆栈跟踪会告诉您,找出哪个变量为空,然后追溯到您的代码以查看原因。你会一次又一次地遇到这些,相信我。

关于java - JPanel 未添加到 JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30004498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com