java - JButtons 在 paintComponent() 中起作用

标签 java swing graphics paintcomponent

我对非描述性标题感到抱歉,但我不确定如何传达这些问题。对于初学者,JButton 时不时地以循环应创建它们的相同顺序多次创建它们自己。我遇到的另一个问题是,当我使用 setLocation() 方法重新定位它们时,它会在我想要的位置创建新的 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

最佳答案

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

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) 方法。

复习你的问题:

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

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

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.

很可能您看到的是由于您没有调用 super 的 paintComponent 方法而导致的图像残留。如果不这样做,您就不会让 JPanel 重新绘制自身并删除需要替换或刷新的旧图像像素。

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

不,您几乎需要更改所有内容。

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

关于java - JButtons 在 paintComponent() 中起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17819314/

相关文章:

java - 单击了哪个按钮。 Java、Netbeans

java - 从 RGB 绘制 CMYK 颜色

c++ - 在 SDL_Surface 像素上绘制矩形

Java Keytool 在生成 CSR 时剥离 SAN

Java EE 6 - SEAM + EJB 集成/3 层架构

java - 如何将 BorderLayout 与 Netbeans Matisse 一起使用

math - 蒙特卡罗路径追踪算法这部分的目的是什么?

java - 退出按钮java

java - 如何使用 OSMDROID 创建像 Google map 一样的方向蓝点?

java - 如何向 servlet 发送 HTTP 请求