【问题标题】:JButtons acting up in paintComponent()在paintComponent() 中起作用的JButton
【发布时间】:2013-07-23 19:27:15
【问题描述】:

对于非描述性标题,我很抱歉,但我不知道如何传达这些问题。对于初学者来说,JButtons 不时地以循环应该创建它们的相同顺序多次创建自己。我遇到的另一个问题是,当我使用 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

【问题讨论】:

  • thisJPanel 的扩展吗?我假设它是。

标签: java swing graphics paintcomponent


【解决方案1】:

您的方法中有几个问题(用“!!!!” cmets 标记):

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 的程序流程和逻辑并重新编写一些代码。

【讨论】:

    猜你喜欢
    • 2012-02-27
    • 2017-01-08
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 2016-02-27
    • 2016-02-09
    相关资源
    最近更新 更多