gpt4 book ai didi

java - 滚动或调整框架大小时,JPanel 中的绘图消失

转载 作者:行者123 更新时间:2023-12-02 04:56:55 26 4
gpt4 key购买 nike

我有一个无法解决的大问题,我在 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();
}
}
}

最佳答案

您永远不应该在提供的 swing 函数之外进行绘制,例如 paintComponent(Graphics g)。调整大小或滚动时,组件将被重新绘制。在此过程中,每个涉及的组件的 paintComponent 方法都会被调用。由于您的绘图发生在面板之外,因此面板不会按预期重新绘制。重写 paintComponent 方法并将所有绘图代码复制到其中。

class MyPanel extends JPanel
{
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
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 );
/* ... All drawing code ... */
}
}

关于java - 滚动或调整框架大小时,JPanel 中的绘图消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28674876/

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