我有一個很大的問題,我無法解決它我有JPanel
在JFrame
。在這個面板中,我繪製了很多東西(如圖),但是當我滾動面板或調整框架大小時,圖畫就消失了!滾動或重新框架時,JPanel中的繪圖消失
我該如何讓繪圖留下來?
代碼:
private void full_simulation_button(java.awt.event.ActionEvent evt) {
if (jTextField1.getText().equals("")){
JOptionPane jop2 = new JOptionPane();
jop2.showMessageDialog(null, "You should enter you trace file !", "Attention", JOptionPane.WARNING_MESSAGE);
}
else {
Graphics g = jPanel5.getGraphics();
g.setColor(Color.RED);
g.drawRect(150,10,100,20);
g.fillRect(150,10,100,20);
g.drawLine(200,10 , 200, 2000);
g.setColor(Color.BLACK);
g.drawString("UE",190 ,25);
g.setColor(Color.BLUE);
g.drawRect(350,10,100,20);
g.fillRect(350,10,100,20);
g.drawLine(400,10 , 400, 2000);
g.setColor(Color.BLACK);
g.drawString("Node B",380 ,25);
g.setColor(Color.GREEN);
g.drawRect(550,10,100,20);
g.fillRect(550,10,100,20);
g.drawLine(600,10 , 600, 2000);
g.setColor(Color.BLACK);
g.drawString("RNC",590 ,25);
g.setColor(Color.YELLOW);
g.drawRect(750,10,100,20);
g.fillRect(750,10,100,20);
g.drawLine(800,10 , 800, 2000);
g.setColor(Color.BLACK);
g.drawString("CN",790 ,25);
System.out.println(new java.io.File("").getAbsolutePath());
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document document= builder.parse(new File(jTextField1.getText()));
//Affiche la version de XML
System.out.println("la version du XML est "+ document.getXmlVersion());
//Affiche l'encodage
System.out.println("l'encodage utilisé est"+document.getXmlEncoding());
//Affiche s'il s'agit d'un document standalone
System.out.println("le document est standolone:"+document.getXmlStandalone());
final Element racine = document.getDocumentElement();
System.out.println("le racime du fichier est :"+racine.getNodeName());
final NodeList racineNoeuds = racine.getChildNodes();
final int nbRacineNoeuds = racineNoeuds.getLength();
for (int i = 0; i<nbRacineNoeuds; i++) {
if(racineNoeuds.item(i).getNodeType() == Node.ELEMENT_NODE) {
final Element nd = (Element) racineNoeuds.item(i);
final Node noeud = racineNoeuds.item(i);
System.out.println(noeud.getNodeName());
final NodeList msg = nd.getElementsByTagName("PARA");
final int nbre = msg.getLength();
int marge=0;
int rang =1;
String type =null;
String direction=null;
for(int j = 0; j<nbre ; j++){
final Element message = (Element) msg.item(j);
g.setColor(Color.BLACK);
if (message.getAttribute("name").equals("Message Type")){
type = message.getAttribute("value");
// System.out.println(type);
if (type.startsWith("RRC")){
g.drawLine(200,50+marge , 600, 50+marge);
g.drawString(rang+". "+type,220 ,45+marge);
marge=marge+30;
rang=rang+1;
}
if (type.startsWith("NBAP")){
g.drawLine(400,50+marge , 600, 50+marge);
g.drawString(rang+". "+type,420 ,45+marge);
marge=marge+30;
rang=rang+1;
}
if (type.startsWith("RANAP")){
g.drawLine(600,50+marge , 800, 50+marge);
g.drawString(rang+". "+type,620 ,45+marge);
marge=marge+30;
rang=rang+1;
}
}
if (message.getAttribute("name").equals("Message Direction")){
direction = message.getAttribute("value");
//System.out.println(direction);
if ((direction.equals("From-UE"))&&(type.startsWith("RRC"))){
g.drawString(">",595, 50+marge-25);
System.out.println(type);
}
if ((direction.equals("To-UE"))&&(type.startsWith("RRC"))){
g.drawString("<",200, 50+marge-25);
System.out.println(type);
}
if ((direction.equals("From-NodeB"))&&(type.startsWith("NBAP"))){
g.drawString(">",595, 50+marge-25);
System.out.println(type);
}
if ((direction.equals("To-NodeB"))&&(type.startsWith("NBAP"))){
g.drawString("<",400, 50+marge-25);
System.out.println(type);
}
if ((direction.equals("From-CN"))&&(type.startsWith("RANAP"))){
g.drawString("<",600, 50+marge-25);
System.out.println(type);
}
if ((direction.equals("To-CN"))&&(type.startsWith("RANAP"))){
g.drawString(">",795, 50+marge-25);
System.out.println(type);
}
}
}
}
}
}
catch (final ParserConfigurationException e) {
e.printStackTrace();
}
catch (final SAXException e) {
e.printStackTrace();
}
catch (final IOException e) {
e.printStackTrace();
}
}
}
'Graphics g = jPanel5.getGraphics(); 「這是問題的根源。組件應該在請求時繪製*請參閱本教程的[執行自定義繪畫](http://docs.oracle.com/javase/uiswing/painting/)課程以獲取正確的詳細信息做法。 – 2015-02-23 13:43:18