JButtons在paintComponent()中起作用(JButtons acting up in paintComponent())

系统教程 行业动态 更新时间:2024-06-14 16:57:40
JButtons在paintComponent()中起作用(JButtons acting up in paintComponent())

我对非描述性标题感到抱歉,但我不确定如何沟通这些问题。 对于初学者来说,JButton每次都会按照循环创建它们的顺序多次创建自己。 我遇到的另一个问题是,当我使用setLocation()方法重新定位它时,它会创建我想要它们的新JButton,但也会保留旧的JButton。 我不知道我是否只需刷新图形或发生了什么。 谢谢您的帮助。

数组playerHand()在Player类中定义,长度为5。

public void paintComponent(java.awt.Graphics g){ setBackground(Color.GREEN); // create a Graphics2D object from the graphics object for drawing shape Graphics2D gr=(Graphics2D) g; for(int x=0;x<Player.hand.size();x++){ Card c = Player.hand.get(x); //c = current element in array c.XCenter = 30 + 140*x; c.XCord = c.XCenter - 30; c.YCord = 0; //5 pixel thick pen gr.setStroke(new java.awt.BasicStroke(3)); gr.setColor(Color.black); //sets pen color to black gr.drawRect(c.XCord, c.YCord, c.cardWidth-1, c.cardHeight-1); //draws card outline gr.setFont(new Font("Serif", Font.PLAIN, 18)); gr.drawString(c.name, c.XCord+10, c.YCord+20); gr.drawString("Atk: ", c.XCord+10, c.YCord+60); gr.drawString(""+c.attack, c.XCord+60, c.YCord+60); gr.drawString("Def: ", c.XCord+10, c.YCord+80); gr.drawString(""+c.defence, c.XCord+60, c.YCord+80); gr.drawString("HP: ", c.XCord+10, c.YCord+100); gr.drawString(""+c.health, c.XCord+60, c.YCord+100); JButton button = new JButton(c.name); button.setSize(c.cardWidth, c.cardHeight); //button.setLocation(c.XCord, c.YCord); this.add(button); repaint(); } } //end of paintComponent

I am sorry about the non-descriptive title but I am not sure how to communicate the problems. For starters the JButtons every now and again are creating themselves multiple times in the same order that the loop should create them. Another issue I am having is that when I reposition them using the setLocation() method it creates new JButtons where I want them but also leaves the old ones where they are. I don't know if I just have to refresh the graphics or what is going on. Thanks for the help.

the array playerHand()is defined in the Player class and is 5 in length.

public void paintComponent(java.awt.Graphics g){ setBackground(Color.GREEN); // create a Graphics2D object from the graphics object for drawing shape Graphics2D gr=(Graphics2D) g; for(int x=0;x<Player.hand.size();x++){ Card c = Player.hand.get(x); //c = current element in array c.XCenter = 30 + 140*x; c.XCord = c.XCenter - 30; c.YCord = 0; //5 pixel thick pen gr.setStroke(new java.awt.BasicStroke(3)); gr.setColor(Color.black); //sets pen color to black gr.drawRect(c.XCord, c.YCord, c.cardWidth-1, c.cardHeight-1); //draws card outline gr.setFont(new Font("Serif", Font.PLAIN, 18)); gr.drawString(c.name, c.XCord+10, c.YCord+20); gr.drawString("Atk: ", c.XCord+10, c.YCord+60); gr.drawString(""+c.attack, c.XCord+60, c.YCord+60); gr.drawString("Def: ", c.XCord+10, c.YCord+80); gr.drawString(""+c.defence, c.XCord+60, c.YCord+80); gr.drawString("HP: ", c.XCord+10, c.YCord+100); gr.drawString(""+c.health, c.XCord+60, c.YCord+100); JButton button = new JButton(c.name); button.setSize(c.cardWidth, c.cardHeight); //button.setLocation(c.XCord, c.YCord); this.add(button); repaint(); } } //end of paintComponent

最满意答案

您的方法有以下几个问题(标有“!!!!”注释):

public void paintComponent(java.awt.Graphics g){ setBackground(Color.GREEN); // !!!! Graphics2D gr=(Graphics2D) g; for(int x=0;x<Player.hand.size();x++){ // .... etc .... JButton button = new JButton(c.name); // !!!! yikes !!!! button.setSize(c.cardWidth, c.cardHeight); //button.setLocation(c.XCord, c.YCord); this.add(button); // !!!! yikes !!!! repaint(); // !!!! } } 不要在paintComponent(...)向GUI添加组件。 永远不要这样做。 永远。 此方法应仅用于绘图和绘图,而不应用于程序逻辑或GUI构建。 您无法完全控制何时或是否将其被调用。 它需要尽可能快,以免你的程序响应不佳。 如果可能,避免在paintComponent(...)创建对象。 避免使用此方法中的文件I / O(尽管上面的代码没有问题)。 不要在此方法中调用setBackground(...) 。 在构造函数或其他方法中执行此操作。 切勿从此方法中调用repaint() 。 不要忘记在覆盖中调用super的paintComponent(g)方法。

回顾你的问题:

对于初学者来说,JButton每次都会按照循环创建它们的顺序多次创建自己。

这是因为您无法控制paintComponent的调用时间或频率。 JVM可以响应来自操作系统的关于脏区域的信息来调用它,或者程序可以请求重新绘制,但是由于这个原因,程序逻辑和gui构建永远不应该在这种方法中。

我遇到的另一个问题是,当我使用setLocation()方法重新定位它时,它会创建我想要它们的新JButton,但也会保留旧的JButton。

你可能很难看到你没有调用super的paintComponent方法的图像残余。 如果不这样做,则表示您没有重新绘制JPanel并删除需要替换或刷新的旧图像像素。

我不知道我是否只需刷新图形或发生了什么。

不,你需要改变一切。

看起来您必须重新考虑GUI的程序流和逻辑并重新编写一些代码。

There are several problems (marked by "!!!!" comments) in your method below:

public void paintComponent(java.awt.Graphics g){ setBackground(Color.GREEN); // !!!! Graphics2D gr=(Graphics2D) g; for(int x=0;x<Player.hand.size();x++){ // .... etc .... JButton button = new JButton(c.name); // !!!! yikes !!!! button.setSize(c.cardWidth, c.cardHeight); //button.setLocation(c.XCord, c.YCord); this.add(button); // !!!! yikes !!!! repaint(); // !!!! } } Do not add components to GUI's in paintComponent(...). Never do this. Ever. This method should be for drawing and drawing only and never for program logic or GUI building. You do not have full control over when or if it will be called. It needs to be as fast as possible so as not to make your program poorly responsive. Avoid object creation within paintComponent(...) if possible. Avoid file I/O in this method (though not a problem with your code above). Don't call setBackground(...) in this method. Do that in the constructor or other method. Never call repaint() from within this method. Don't forget to call the super's paintComponent(g) method in your override.

To go over your problems:

For starters the JButtons every now and again are creating themselves multiple times in the same order that the loop should create them.

This is because you do not have control over when or how often paintComponent is called. The JVM may call it in response to information from the operating system about a dirty region, or the program may request a repaint, but for this reason, program logic and gui building should never be in this method.

Another issue I am having is that when I reposition them using the setLocation() method it creates new JButtons where I want them but also leaves the old ones where they are.

Quite likely you're seeing image remnants from your not calling the super's paintComponent method. By not doing this, you don't have the JPanel repaint itself and remove old image pixels that need to be replaced or refreshed.

I don't know if I just have to refresh the graphics or what is going on.

No, you need to change just about everything.

It looks like you have to re-think your GUI's program flow and logic and re-write a bit of code.

更多推荐

本文发布于:2023-04-13 12:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/320ff74ff968b882478b3273f24e4a47.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中起   作用   JButtons   paintComponent   acting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!